Linux-USB Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2 0/10] Symbol namespaces - RFC
       [not found] <20180716122125.175792-1-maco@android.com>
@ 2019-08-13 12:16 ` Matthias Maennich
  2019-08-13 12:16   ` [PATCH v2 01/10] module: support reading multiple values per modinfo tag Matthias Maennich
                     ` (10 more replies)
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
  2 siblings, 11 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:16 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Adrian Reber, Andrew Morton,
	Ard Biesheuvel, cocci, Dan Williams, David Howells,
	David S. Miller, Ingo Molnar, Johannes Weiner, Jonathan Cameron,
	Mauro Carvalho Chehab, Michael Ellerman, Nicolas Ferre,
	Patrick Bellasi, Richard Guy Briggs, Will Deacon

As of Linux 5.3-rc4, there are 31203 [1] exported symbols in the kernel.
That is a growth of almost 1000 symbols since 4.17 (30206 [2]).  There
seems to be some consensus amongst kernel devs that the export surface
is too large, and hard to reason about.

Generally, these symbols fall in one of these categories:
1) Symbols actually meant for drivers
2) Symbols that are only exported because functionality is split over
   multiple modules, yet they really shouldn't be used by modules outside
   of their own subsystem
3) Symbols really only meant for in-tree use

When module developers try to upstream their code, it regularly turns
out that they are using exported symbols that they really shouldn't be
using. This problem is even bigger for drivers that are currently
out-of-tree, which may be using many symbols that they shouldn't be
using, and that break when those symbols are removed or modified.

This patch allows subsystem maintainers to partition their exported
symbols into separate namespaces, and module authors to import such
namespaces only when needed.

This allows subsystem maintainers to more easily limit availability of
these namespaced symbols to other parts of the kernel. It can also be
used to partition the set of exported symbols for documentation
purposes; for example, a set of symbols that is really only used for
debugging could be in a "SUBSYSTEM_DEBUG" namespace.

I continued the work mainly done by Martijn Coenen. In this v2 the
following changes have been introduced compared to v1 of this series:

- Rather than adding and evaluating separate sections __knsimport_NS,
  use modinfo tags to declare the namespaces a module introduces.
  Adjust modpost and the module loader accordingly.
- Also add support for reading multiple modinfo values for the same tag
  to allow list-like access to modinfo tags.
- The macros in export.h have been cleaned up to avoid redundancy in the
  macro parameters (ns, nspost, nspost2).
- The introduction of relative references in the ksymtab entries caused
  a rework of the macros to accommodate that configuration as well.
- Alignment of kernel_symbol in the ksymtab needed to be fixed to allow
  growing the kernel_symbol struct.
- Modpost does now also append the namespace suffix to the symbol
  entries in Module.symvers.
- The configuration option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS allows
  relaxing the enforcement of properly declared namespace imports at module
  loading time.
- Symbols can be collectively exported into a namespace by defining
  DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile.
- The requirement for a very recent coccinelle spatch has been lifted by
  simplifying the script.
- nsdeps does now ensures MODULE_IMPORT_NS statements are sorted when
  patching the module source files.
- Some minor bugs have been addressed in nsdeps to allow it to work with
  modules that have more than one source file.
- The RFC for the usb-storage symbols has been simplified by using
  DEFAULT_SYMBOL_NAMESPACE=USB_STORAGE rather than explicitly exporting each
  and every symbol into that new namespace.

This patch series was developed against v5.3-rc4.

[1] git grep "^EXPORT_SYMBOL\w*(" v5.3-rc4 | wc -l
[2] git grep "^EXPORT_SYMBOL\w*(" v4.17    | wc -l

Matthias Maennich (10):
  module: support reading multiple values per modinfo tag
  export: explicitly align struct kernel_symbol
  module: add support for symbol namespaces.
  modpost: add support for symbol namespaces
  module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  export: allow definition default namespaces in Makefiles or sources
  modpost: add support for generating namespace dependencies
  scripts: Coccinelle script for namespace dependencies.
  usb-storage: remove single-use define for debugging
  RFC: usb-storage: export symbols in USB_STORAGE namespace

 MAINTAINERS                                 |   5 +
 Makefile                                    |  12 ++
 arch/m68k/include/asm/export.h              |   1 -
 drivers/usb/storage/Makefile                |   2 +
 drivers/usb/storage/alauda.c                |   1 +
 drivers/usb/storage/cypress_atacb.c         |   1 +
 drivers/usb/storage/datafab.c               |   1 +
 drivers/usb/storage/debug.h                 |   2 -
 drivers/usb/storage/ene_ub6250.c            |   1 +
 drivers/usb/storage/freecom.c               |   1 +
 drivers/usb/storage/isd200.c                |   1 +
 drivers/usb/storage/jumpshot.c              |   1 +
 drivers/usb/storage/karma.c                 |   1 +
 drivers/usb/storage/onetouch.c              |   1 +
 drivers/usb/storage/realtek_cr.c            |   1 +
 drivers/usb/storage/scsiglue.c              |   2 +-
 drivers/usb/storage/sddr09.c                |   1 +
 drivers/usb/storage/sddr55.c                |   1 +
 drivers/usb/storage/shuttle_usbat.c         |   1 +
 drivers/usb/storage/uas.c                   |   1 +
 include/asm-generic/export.h                |  14 +-
 include/linux/export.h                      |  92 +++++++++++--
 include/linux/module.h                      |   2 +
 init/Kconfig                                |  14 ++
 kernel/module.c                             |  67 ++++++++-
 scripts/Makefile.modpost                    |   4 +-
 scripts/coccinelle/misc/add_namespace.cocci |  23 ++++
 scripts/mod/modpost.c                       | 144 ++++++++++++++++++--
 scripts/mod/modpost.h                       |   9 ++
 scripts/nsdeps                              |  54 ++++++++
 30 files changed, 421 insertions(+), 40 deletions(-)
 create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
 create mode 100644 scripts/nsdeps

-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 01/10] module: support reading multiple values per modinfo tag
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
@ 2019-08-13 12:16   ` Matthias Maennich
  2019-08-13 12:40     ` Greg KH
  2019-08-13 12:16   ` [PATCH v2 02/10] export: explicitly align struct kernel_symbol Matthias Maennich
                     ` (9 subsequent siblings)
  10 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:16 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

Similar to modpost's get_next_modinfo(), introduce get_next_modinfo() in
kernel/module.c to acquire any further values associated with the same
modinfo tag name. That is useful for any tags that have multiple
occurrences (such as 'alias'), but is in particular introduced here as
part of the symbol namespaces patch series to read the (potentially)
multiple namespaces a module is importing.

Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Reviewed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 kernel/module.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index 5933395af9a0..a23067907169 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2481,7 +2481,8 @@ static char *next_string(char *string, unsigned long *secsize)
 	return string;
 }
 
-static char *get_modinfo(struct load_info *info, const char *tag)
+static char *get_next_modinfo(const struct load_info *info, const char *tag,
+			      char *prev)
 {
 	char *p;
 	unsigned int taglen = strlen(tag);
@@ -2492,13 +2493,25 @@ static char *get_modinfo(struct load_info *info, const char *tag)
 	 * get_modinfo() calls made before rewrite_section_headers()
 	 * must use sh_offset, as sh_addr isn't set!
 	 */
-	for (p = (char *)info->hdr + infosec->sh_offset; p; p = next_string(p, &size)) {
+	char *modinfo = (char *)info->hdr + infosec->sh_offset;
+
+	if (prev) {
+		size -= prev - modinfo;
+		modinfo = next_string(prev, &size);
+	}
+
+	for (p = modinfo; p; p = next_string(p, &size)) {
 		if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
 			return p + taglen + 1;
 	}
 	return NULL;
 }
 
+static char *get_modinfo(const struct load_info *info, const char *tag)
+{
+	return get_next_modinfo(info, tag, NULL);
+}
+
 static void setup_modinfo(struct module *mod, struct load_info *info)
 {
 	struct module_attribute *attr;
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 02/10] export: explicitly align struct kernel_symbol
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
  2019-08-13 12:16   ` [PATCH v2 01/10] module: support reading multiple values per modinfo tag Matthias Maennich
@ 2019-08-13 12:16   ` Matthias Maennich
  2019-08-13 12:41     ` Greg KH
  2019-08-13 12:17   ` [PATCH v2 03/10] module: add support for symbol namespaces Matthias Maennich
                     ` (8 subsequent siblings)
  10 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:16 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton, Nicolas Pitre,
	Michael Ellerman, Ard Biesheuvel

This change allows growing struct kernel_symbol without wasting bytes to
alignment. It also concretized the alignment of ksymtab entries if
relative references are used for ksymtab entries.

struct kernel_symbol was already implicitly being aligned to the word
size, except on x86_64 and m68k, where it is aligned to 16 and 2 bytes,
respectively.

As far as I can tell there is no requirement for aligning struct
kernel_symbol to 16 bytes on x86_64, but gcc aligns structs to their
size, and the linker aligns the custom __ksymtab sections to the largest
data type contained within, so setting KSYM_ALIGN to 16 was necessary to
stay consistent with the code generated for non-ASM EXPORT_SYMBOL(). Now
that non-ASM EXPORT_SYMBOL() explicitly aligns to word size (8),
KSYM_ALIGN is no longer necessary.

In case of relative references, the alignment has been changed
accordingly to not waste space when adding new struct members.

As for m68k, struct kernel_symbol is aligned to 2 bytes even though the
structure itself is 8 bytes; using a 4-byte alignment shouldn't hurt.

I manually verified the output of the __ksymtab sections didn't change
on x86, x86_64, arm, arm64 and m68k. As expected, the section contents
didn't change, and the ELF section alignment only changed on x86_64 and
m68k. Feedback from other archs more than welcome.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 arch/m68k/include/asm/export.h | 1 -
 include/asm-generic/export.h   | 8 +++-----
 include/linux/export.h         | 3 ++-
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/arch/m68k/include/asm/export.h b/arch/m68k/include/asm/export.h
index 0af20f48bd07..b53008b67ce1 100644
--- a/arch/m68k/include/asm/export.h
+++ b/arch/m68k/include/asm/export.h
@@ -1,3 +1,2 @@
-#define KSYM_ALIGN 2
 #define KCRC_ALIGN 2
 #include <asm-generic/export.h>
diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 294d6ae785d4..63f54907317b 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -4,15 +4,13 @@
 #ifndef KSYM_FUNC
 #define KSYM_FUNC(x) x
 #endif
-#ifdef CONFIG_64BIT
-#ifndef KSYM_ALIGN
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+#define KSYM_ALIGN 4
+#elif defined(CONFIG_64BIT)
 #define KSYM_ALIGN 8
-#endif
 #else
-#ifndef KSYM_ALIGN
 #define KSYM_ALIGN 4
 #endif
-#endif
 #ifndef KCRC_ALIGN
 #define KCRC_ALIGN 4
 #endif
diff --git a/include/linux/export.h b/include/linux/export.h
index fd8711ed9ac4..28a4d2150689 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -52,7 +52,7 @@ extern struct module __this_module;
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	__ADDRESSABLE(sym)						\
 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
-	    "	.balign	8					\n"	\
+	    "	.balign 4					\n"	\
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
@@ -66,6 +66,7 @@ struct kernel_symbol {
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	static const struct kernel_symbol __ksymtab_##sym		\
 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
+	__aligned(sizeof(void *))					\
 	= { (unsigned long)&sym, __kstrtab_##sym }
 
 struct kernel_symbol {
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 03/10] module: add support for symbol namespaces.
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
  2019-08-13 12:16   ` [PATCH v2 01/10] module: support reading multiple values per modinfo tag Matthias Maennich
  2019-08-13 12:16   ` [PATCH v2 02/10] export: explicitly align struct kernel_symbol Matthias Maennich
@ 2019-08-13 12:17   ` Matthias Maennich
  2019-08-13 15:26     ` Greg KH
  2019-08-13 12:17   ` [PATCH v2 04/10] modpost: " Matthias Maennich
                     ` (7 subsequent siblings)
  10 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:17 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton,
	Michael Ellerman, Ard Biesheuvel

The EXPORT_SYMBOL_NS() and EXPORT_SYMBOL_NS_GPL() macros can be used to
export a symbol to a specific namespace.  There are no _GPL_FUTURE and
_UNUSED variants because these are currently unused, and I'm not sure
they are necessary.

I didn't add EXPORT_SYMBOL_NS() for ASM exports; this patch sets the
namespace of ASM exports to NULL by default. In case of relative
references, it will be relocatable to NULL. If there's a need, this
should be pretty easy to add.

A module that wants to use a symbol exported to a namespace must add a
MODULE_IMPORT_NS() statement to their module code; otherwise, modpost
will complain when building the module, and the kernel module loader
will emit an error and fail when loading the module.

MODULE_IMPORT_NS() adds a modinfo tag 'import_ns' to the module. That
tag can be observed by the modinfo command, modpost and kernel/module.c
at the time of loading the module.

The ELF symbols are renamed to include the namespace with an asm label;
for example, symbol 'usb_stor_suspend' in namespace USB_STORAGE becomes
'usb_stor_suspend.USB_STORAGE'.  This allows modpost to do namespace
checking, without having to go through all the effort of parsing ELF and
relocation records just to get to the struct kernel_symbols.

On x86_64 I saw no difference in binary size (compression), but at
runtime this will require a word of memory per export to hold the
namespace. An alternative could be to store namespaced symbols in their
own section and use a separate 'struct namespaced_kernel_symbol' for
that section, at the cost of making the module loader more complex.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/asm-generic/export.h |  6 +--
 include/linux/export.h       | 85 ++++++++++++++++++++++++++++++------
 include/linux/module.h       |  2 +
 kernel/module.c              | 43 ++++++++++++++++++
 4 files changed, 120 insertions(+), 16 deletions(-)

diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 63f54907317b..e2b5d0f569d3 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -17,11 +17,11 @@
 
 .macro __put, val, name
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
-	.long	\val - ., \name - .
+	.long	\val - ., \name - ., 0 - .
 #elif defined(CONFIG_64BIT)
-	.quad	\val, \name
+	.quad	\val, \name, 0
 #else
-	.long	\val, \name
+	.long	\val, \name, 0
 #endif
 .endm
 
diff --git a/include/linux/export.h b/include/linux/export.h
index 28a4d2150689..8e12e05444d1 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -20,6 +20,8 @@ extern struct module __this_module;
 
 #ifdef CONFIG_MODULES
 
+#define NS_SEPARATOR "."
+
 #if defined(__KERNEL__) && !defined(__GENKSYMS__)
 #ifdef CONFIG_MODVERSIONS
 /* Mark the CRC weak since genksyms apparently decides not to
@@ -49,6 +51,16 @@ extern struct module __this_module;
  * absolute relocations that require runtime processing on relocatable
  * kernels.
  */
+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
+	__ADDRESSABLE(sym)						\
+	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
+	    "	.balign	4					\n"	\
+	    "__ksymtab_" #sym NS_SEPARATOR #ns ":		\n"	\
+	    "	.long	" #sym "- .				\n"	\
+	    "	.long	__kstrtab_" #sym "- .			\n"	\
+	    "	.long	__kstrtab_ns_" #sym "- .		\n"	\
+	    "	.previous					\n")
+
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	__ADDRESSABLE(sym)						\
 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
@@ -56,32 +68,53 @@ extern struct module __this_module;
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
+	    "	.long	0 - .					\n"	\
 	    "	.previous					\n")
 
 struct kernel_symbol {
 	int value_offset;
 	int name_offset;
+	int namespace_offset;
 };
 #else
+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
+	static const struct kernel_symbol __ksymtab_##sym##__##ns	\
+	asm("__ksymtab_" #sym NS_SEPARATOR #ns)				\
+	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
+	__aligned(sizeof(void *))					\
+	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym}
+
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	static const struct kernel_symbol __ksymtab_##sym		\
+	asm("__ksymtab_" #sym)						\
 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
 	__aligned(sizeof(void *))					\
-	= { (unsigned long)&sym, __kstrtab_##sym }
+	= { (unsigned long)&sym, __kstrtab_##sym, NULL }
 
 struct kernel_symbol {
 	unsigned long value;
 	const char *name;
+	const char *namespace;
 };
 #endif
 
-/* For every exported symbol, place a struct in the __ksymtab section */
-#define ___EXPORT_SYMBOL(sym, sec)					\
+#define ___export_symbol_common(sym, sec)				\
 	extern typeof(sym) sym;						\
 	__CRC_SYMBOL(sym, sec)						\
 	static const char __kstrtab_##sym[]				\
 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
-	= #sym;								\
+	= #sym								\
+
+/* For every exported symbol, place a struct in the __ksymtab section */
+#define ___EXPORT_SYMBOL_NS(sym, sec, ns)				\
+	___export_symbol_common(sym, sec);			\
+	static const char __kstrtab_ns_##sym[]				\
+	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
+	= #ns;								\
+	__KSYMTAB_ENTRY_NS(sym, sec, ns)
+
+#define ___EXPORT_SYMBOL(sym, sec)					\
+	___export_symbol_common(sym, sec);				\
 	__KSYMTAB_ENTRY(sym, sec)
 
 #if defined(__DISABLE_EXPORTS)
@@ -91,6 +124,7 @@ struct kernel_symbol {
  * be reused in other execution contexts such as the UEFI stub or the
  * decompressor.
  */
+#define __EXPORT_SYMBOL_NS(sym, sec, ns)
 #define __EXPORT_SYMBOL(sym, sec)
 
 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
@@ -117,18 +151,26 @@ struct kernel_symbol {
 #define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
 #define __cond_export_sym_0(sym, sec) /* nothing */
 
+#define __EXPORT_SYMBOL_NS(sym, sec, ns)				\
+	__ksym_marker(sym);						\
+	__cond_export_ns_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
+#define __cond_export_ns_sym(sym, sec, ns, conf)			\
+	___cond_export_ns_sym(sym, sec, ns, conf)
+#define ___cond_export_ns_sym(sym, sec, ns, enabled)			\
+	__cond_export_ns_sym_##enabled(sym, sec, ns)
+#define __cond_export_ns_sym_1(sym, sec, ns) ___EXPORT_SYMBOL_NS(sym, sec, ns)
+#define __cond_export_ns_sym_0(sym, sec, ns) /* nothing */
+
 #else
+#define __EXPORT_SYMBOL_NS ___EXPORT_SYMBOL_NS
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
 #endif
 
-#define EXPORT_SYMBOL(sym)					\
-	__EXPORT_SYMBOL(sym, "")
-
-#define EXPORT_SYMBOL_GPL(sym)					\
-	__EXPORT_SYMBOL(sym, "_gpl")
-
-#define EXPORT_SYMBOL_GPL_FUTURE(sym)				\
-	__EXPORT_SYMBOL(sym, "_gpl_future")
+#define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
+#define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
+#define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
+#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL_NS(sym, "", ns)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL_NS(sym, "_gpl", ns)
 
 #ifdef CONFIG_UNUSED_SYMBOLS
 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
@@ -138,11 +180,28 @@ struct kernel_symbol {
 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
 #endif
 
-#endif	/* __GENKSYMS__ */
+#endif	/* __KERNEL__ && !__GENKSYMS__ */
+
+#if defined(__GENKSYMS__)
+/*
+ * When we're running genksyms, ignore the namespace and make the _NS
+ * variants look like the normal ones. There are two reasons for this:
+ * 1) In the normal definition of EXPORT_SYMBOL_NS, the 'ns' macro
+ *    argument is itself not expanded because it's always tokenized or
+ *    concatenated; but when running genksyms, a blank definition of the
+ *    macro does allow the argument to be expanded; if a namespace
+ *    happens to collide with a #define, this can cause issues.
+ * 2) There's no need to modify genksyms to deal with the _NS variants
+ */
+#define EXPORT_SYMBOL_NS(sym, ns) EXPORT_SYMBOL(sym)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns) EXPORT_SYMBOL_GPL(sym)
+#endif
 
 #else /* !CONFIG_MODULES... */
 
 #define EXPORT_SYMBOL(sym)
+#define EXPORT_SYMBOL_NS(sym, ns)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns)
 #define EXPORT_SYMBOL_GPL(sym)
 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
 #define EXPORT_UNUSED_SYMBOL(sym)
diff --git a/include/linux/module.h b/include/linux/module.h
index 1455812dd325..b3611e749f72 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -280,6 +280,8 @@ struct notifier_block;
 
 #ifdef CONFIG_MODULES
 
+#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)
+
 extern int modules_disabled; /* for sysctl */
 /* Get/put a kernel symbol (calls must be symmetric) */
 void *__symbol_get(const char *symbol);
diff --git a/kernel/module.c b/kernel/module.c
index a23067907169..57e8253f2251 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -544,6 +544,15 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
 #endif
 }
 
+static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
+{
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	return offset_to_ptr(&sym->namespace_offset);
+#else
+	return sym->namespace;
+#endif
+}
+
 static int cmp_name(const void *va, const void *vb)
 {
 	const char *a;
@@ -1379,6 +1388,34 @@ static inline int same_magic(const char *amagic, const char *bmagic,
 }
 #endif /* CONFIG_MODVERSIONS */
 
+static char *get_modinfo(const struct load_info *info, const char *tag);
+static char *get_next_modinfo(const struct load_info *info, const char *tag,
+			      char *prev);
+
+static int verify_namespace_is_imported(const struct load_info *info,
+					const struct kernel_symbol *sym,
+					struct module *mod)
+{
+	const char *namespace;
+	char *imported_namespace;
+
+	namespace = kernel_symbol_namespace(sym);
+	if (namespace) {
+		imported_namespace = get_modinfo(info, "import_ns");
+		while (imported_namespace) {
+			if (strcmp(namespace, imported_namespace) == 0)
+				return 0;
+			imported_namespace = get_next_modinfo(
+				info, "import_ns", imported_namespace);
+		}
+		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
+		       mod->name, kernel_symbol_name(sym), namespace);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+
 /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
 static const struct kernel_symbol *resolve_symbol(struct module *mod,
 						  const struct load_info *info,
@@ -1413,6 +1450,12 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
 		goto getname;
 	}
 
+	err = verify_namespace_is_imported(info, sym, mod);
+	if (err) {
+		sym = ERR_PTR(err);
+		goto getname;
+	}
+
 getname:
 	/* We must make copy under the lock if we failed to get ref. */
 	strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 04/10] modpost: add support for symbol namespaces
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
                     ` (2 preceding siblings ...)
  2019-08-13 12:17   ` [PATCH v2 03/10] module: add support for symbol namespaces Matthias Maennich
@ 2019-08-13 12:17   ` " Matthias Maennich
  2019-08-13 15:27     ` Greg KH
  2019-08-13 12:17   ` [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
                     ` (6 subsequent siblings)
  10 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:17 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

Add support for symbols that are exported into namespaces. For that,
extract any namespace suffix from the symbol name. In addition, emit a
warning whenever a module refers to an exported symbol without
explicitly importing the namespace that it is defined in. This patch
consistently adds the namespace suffix to symbol names exported into
Module.symvers.

Example warning emitted by modpost in case of the above violation:

 WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
 USB_STORAGE, but does not import it.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 scripts/mod/modpost.c | 91 +++++++++++++++++++++++++++++++++++++------
 scripts/mod/modpost.h |  7 ++++
 2 files changed, 87 insertions(+), 11 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index f277e116e0eb..538bb24ffee3 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -164,6 +164,7 @@ struct symbol {
 	struct module *module;
 	unsigned int crc;
 	int crc_valid;
+	const char *namespace;
 	unsigned int weak:1;
 	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
 	unsigned int kernel:1;     /* 1 if symbol is from kernel
@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
 	return NULL;
 }
 
+static bool contains_namespace(struct namespace_list *list,
+			       const char *namespace)
+{
+	struct namespace_list *ns_entry;
+
+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
+		if (strcmp(ns_entry->namespace, namespace) == 0)
+			return true;
+
+	return false;
+}
+
+static void add_namespace(struct namespace_list **list, const char *namespace)
+{
+	struct namespace_list *ns_entry;
+
+	if (!contains_namespace(*list, namespace)) {
+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
+					 strlen(namespace) + 1));
+		strcpy(ns_entry->namespace, namespace);
+		ns_entry->next = *list;
+		*list = ns_entry;
+	}
+}
+
+static bool module_imports_namespace(struct module *module,
+				     const char *namespace)
+{
+	return contains_namespace(module->imported_namespaces, namespace);
+}
+
 static const struct {
 	const char *str;
 	enum export export;
@@ -312,6 +344,22 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
 		return export_unknown;
 }
 
+static const char *sym_extract_namespace(const char **symname)
+{
+	size_t n;
+	char *dupsymname;
+
+	n = strcspn(*symname, ".");
+	if (n < strlen(*symname) - 1) {
+		dupsymname = NOFAIL(strdup(*symname));
+		dupsymname[n] = '\0';
+		*symname = dupsymname;
+		return dupsymname + n + 1;
+	}
+
+	return NULL;
+}
+
 /**
  * Add an exported symbol - it may have already been added without a
  * CRC, in this case just update the CRC
@@ -319,16 +367,18 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
 static struct symbol *sym_add_exported(const char *name, struct module *mod,
 				       enum export export)
 {
-	struct symbol *s = find_symbol(name);
+	const char *symbol_name = name;
+	const char *namespace = sym_extract_namespace(&symbol_name);
+	struct symbol *s = find_symbol(symbol_name);
 
 	if (!s) {
-		s = new_symbol(name, mod, export);
+		s = new_symbol(symbol_name, mod, export);
+		s->namespace = namespace;
 	} else {
 		if (!s->preloaded) {
-			warn("%s: '%s' exported twice. Previous export "
-			     "was in %s%s\n", mod->name, name,
-			     s->module->name,
-			     is_vmlinux(s->module->name) ?"":".ko");
+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
+			     mod->name, symbol_name, s->module->name,
+			     is_vmlinux(s->module->name) ? "" : ".ko");
 		} else {
 			/* In case Module.symvers was out of date */
 			s->module = mod;
@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
 	const char *symname;
 	char *version;
 	char *license;
+	char *namespace;
 	struct module *mod;
 	struct elf_info info = { };
 	Elf_Sym *sym;
@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
 		license = get_next_modinfo(&info, "license", license);
 	}
 
+	namespace = get_modinfo(&info, "import_ns");
+	while (namespace) {
+		add_namespace(&mod->imported_namespaces, namespace);
+		namespace = get_next_modinfo(&info, "import_ns", namespace);
+	}
+
 	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
 		symname = remove_dot(info.strtab + sym->st_name);
 
@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
 			basename++;
 		else
 			basename = mod->name;
+
+		if (exp->namespace &&
+		    !module_imports_namespace(mod, exp->namespace)) {
+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
+			     basename, exp->name, exp->namespace);
+		}
+
 		if (!mod->gpl_compatible)
 			check_for_gpl_usage(exp->export, basename, exp->name);
 		check_for_unused(exp->export, basename, exp->name);
@@ -2395,16 +2459,21 @@ static void write_dump(const char *fname)
 {
 	struct buffer buf = { };
 	struct symbol *symbol;
+	const char *namespace;
 	int n;
 
 	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
 		symbol = symbolhash[n];
 		while (symbol) {
-			if (dump_sym(symbol))
-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
-					symbol->crc, symbol->name,
-					symbol->module->name,
-					export_str(symbol->export));
+			if (dump_sym(symbol)) {
+				namespace = symbol->namespace;
+				buf_printf(&buf, "0x%08x\t%s%s%s\t%s\t%s\n",
+					   symbol->crc, symbol->name,
+					   namespace ? "." : "",
+					   namespace ? namespace : "",
+					   symbol->module->name,
+					   export_str(symbol->export));
+			}
 			symbol = symbol->next;
 		}
 	}
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 8453d6ac2f77..9626bf3e7424 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -109,6 +109,11 @@ buf_printf(struct buffer *buf, const char *fmt, ...);
 void
 buf_write(struct buffer *buf, const char *s, int len);
 
+struct namespace_list {
+	struct namespace_list *next;
+	char namespace[0];
+};
+
 struct module {
 	struct module *next;
 	const char *name;
@@ -121,6 +126,8 @@ struct module {
 	struct buffer dev_table_buf;
 	char	     srcversion[25];
 	int is_dot_o;
+	// Actual imported namespaces
+	struct namespace_list *imported_namespaces;
 };
 
 struct elf_info {
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
                     ` (3 preceding siblings ...)
  2019-08-13 12:17   ` [PATCH v2 04/10] modpost: " Matthias Maennich
@ 2019-08-13 12:17   ` Matthias Maennich
  2019-08-13 18:17     ` Greg KH
  2019-08-13 20:15     ` Saravana Kannan
  2019-08-13 12:17   ` [PATCH v2 06/10] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
                     ` (5 subsequent siblings)
  10 siblings, 2 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:17 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton,
	Johannes Weiner, Peter Zijlstra (Intel),
	Mauro Carvalho Chehab, David Howells, Patrick Bellasi,
	Dan Williams, Adrian Reber, Richard Guy Briggs

If MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is enabled (default=n), the
requirement for modules to import all namespaces that are used by
the module is relaxed.

Enabling this option effectively allows (invalid) modules to be loaded
while only a warning is emitted.

Disabling this option keeps the enforcement at module loading time and
loading is denied if the module's imports are not satisfactory.

Reviewed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 init/Kconfig    | 14 ++++++++++++++
 kernel/module.c | 11 +++++++++--
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/init/Kconfig b/init/Kconfig
index bd7d650d4a99..b3373334cdf1 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -2119,6 +2119,20 @@ config MODULE_COMPRESS_XZ
 
 endchoice
 
+config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
+	bool "Allow loading of modules with missing namespace imports"
+	default n
+	help
+	  Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
+	  a namespace. A module that makes use of a symbol exported with such a
+	  namespace is required to import the namespace via MODULE_IMPORT_NS().
+	  This option relaxes this requirement when loading a module. While
+	  technically there is no reason to enforce correct namespace imports,
+	  it creates consistency between symbols defining namespaces and users
+	  importing namespaces they make use of.
+
+	  If unsure, say N.
+
 config TRIM_UNUSED_KSYMS
 	bool "Trim unused exported kernel symbols"
 	depends on MODULES && !UNUSED_SYMBOLS
diff --git a/kernel/module.c b/kernel/module.c
index 57e8253f2251..7c934aaae2d3 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -1408,9 +1408,16 @@ static int verify_namespace_is_imported(const struct load_info *info,
 			imported_namespace = get_next_modinfo(
 				info, "import_ns", imported_namespace);
 		}
-		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
-		       mod->name, kernel_symbol_name(sym), namespace);
+#ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
+		pr_warn(
+#else
+		pr_err(
+#endif
+			"%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
+			mod->name, kernel_symbol_name(sym), namespace);
+#ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
 		return -EINVAL;
+#endif
 	}
 	return 0;
 }
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 06/10] export: allow definition default namespaces in Makefiles or sources
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
                     ` (4 preceding siblings ...)
  2019-08-13 12:17   ` [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
@ 2019-08-13 12:17   ` Matthias Maennich
  2019-08-13 18:16     ` Greg KH
  2019-08-13 18:16     ` Greg KH
  2019-08-13 12:17   ` [PATCH v2 07/10] modpost: add support for generating namespace dependencies Matthias Maennich
                     ` (4 subsequent siblings)
  10 siblings, 2 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:17 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Ard Biesheuvel,
	Michael Ellerman

To avoid excessive usage of EXPORT_SYMBOL_NS(sym, MY_NAMESPACE), where
MY_NAMESPACE will always be the namespace we are exporting to, allow
exporting all definitions of EXPORT_SYMBOL() and friends by defining
DEFAULT_SYMBOL_NAMESPACE.

For example, to export all symbols defined in usb-common into the
namespace USB_COMMON, add a line like this to drivers/usb/common/Makefile:

  ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON

That is equivalent to changing all EXPORT_SYMBOL(sym) definitions to
EXPORT_SYMBOL_NS(sym, USB_COMMON). Subsequently all symbol namespaces
functionality will apply.

Another way of making use of this feature is to define the namespace
within source or header files similar to how TRACE_SYSTEM defines are
used:
  #undef DEFAULT_SYMBOL_NAMESPACE
  #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON

Please note that, as opposed to TRACE_SYSTEM, DEFAULT_SYMBOL_NAMESPACE
has to be defined before including include/linux/export.h.

If DEFAULT_SYMBOL_NAMESPACE is defined, a symbol can still be exported
to another namespace by using EXPORT_SYMBOL_NS() and friends with
explicitly specifying the namespace.

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/linux/export.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/export.h b/include/linux/export.h
index 8e12e05444d1..1fb243abdbc4 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -166,6 +166,12 @@ struct kernel_symbol {
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
 #endif
 
+#ifdef DEFAULT_SYMBOL_NAMESPACE
+#undef __EXPORT_SYMBOL
+#define __EXPORT_SYMBOL(sym, sec)				\
+	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
+#endif
+
 #define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
 #define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
 #define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 07/10] modpost: add support for generating namespace dependencies
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
                     ` (5 preceding siblings ...)
  2019-08-13 12:17   ` [PATCH v2 06/10] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
@ 2019-08-13 12:17   ` Matthias Maennich
  2019-08-13 18:21     ` Greg KH
  2019-08-13 12:17   ` [PATCH v2 08/10] scripts: Coccinelle script for " Matthias Maennich
                     ` (3 subsequent siblings)
  10 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:17 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

This patch adds an option to modpost to generate a <module>.ns_deps file
per module, containing the namespace dependencies for that module.

E.g. if the linked module my-module.ko would depend on the symbol
myfunc.MY_NS in the namespace MY_NS, the my-module.ns_deps file created
by modpost would contain the entry MY_NS to express the namespace
dependency of my-module imposed by using the symbol myfunc.

These files can subsequently be used by static analysis tools (like
coccinelle scripts) to address issues with missing namespace imports. A
later patch of this series will introduce such a script 'nsdeps' and a
corresponding make target to automatically add missing
MODULE_IMPORT_NS() definitions to the module's sources. For that it uses
the information provided in the generated .ns_deps files.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 scripts/mod/modpost.c | 61 +++++++++++++++++++++++++++++++++++++++----
 scripts/mod/modpost.h |  2 ++
 2 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 538bb24ffee3..833a7e1bcbb5 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -38,6 +38,8 @@ static int sec_mismatch_count = 0;
 static int sec_mismatch_fatal = 0;
 /* ignore missing files */
 static int ignore_missing_files;
+/* write namespace dependencies */
+static int write_namespace_deps;
 
 enum export {
 	export_plain,      export_unused,     export_gpl,
@@ -2176,10 +2178,15 @@ static int check_exports(struct module *mod)
 		else
 			basename = mod->name;
 
-		if (exp->namespace &&
-		    !module_imports_namespace(mod, exp->namespace)) {
-			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
-			     basename, exp->name, exp->namespace);
+		if (exp->namespace) {
+			add_namespace(&mod->required_namespaces,
+				      exp->namespace);
+
+			if (!write_namespace_deps &&
+			    !module_imports_namespace(mod, exp->namespace)) {
+				warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
+				     basename, exp->name, exp->namespace);
+			}
 		}
 
 		if (!mod->gpl_compatible)
@@ -2481,6 +2488,38 @@ static void write_dump(const char *fname)
 	free(buf.p);
 }
 
+static void write_namespace_deps_files(void)
+{
+	struct module *mod;
+	struct namespace_list *ns;
+	struct buffer ns_deps_buf = {};
+
+	for (mod = modules; mod; mod = mod->next) {
+		char fname[PATH_MAX];
+		const char *basename;
+
+		if (mod->skip)
+			continue;
+
+		ns_deps_buf.pos = 0;
+
+		for (ns = mod->required_namespaces; ns; ns = ns->next)
+			buf_printf(&ns_deps_buf, "%s\n", ns->namespace);
+
+		if (ns_deps_buf.pos == 0)
+			continue;
+
+		basename = strrchr(mod->name, '/');
+		if (basename)
+			basename++;
+		else
+			basename = mod->name;
+
+		sprintf(fname, ".tmp_versions/%s.ns_deps", basename);
+		write_if_changed(&ns_deps_buf, fname);
+	}
+}
+
 struct ext_sym_list {
 	struct ext_sym_list *next;
 	const char *file;
@@ -2497,7 +2536,7 @@ int main(int argc, char **argv)
 	struct ext_sym_list *extsym_iter;
 	struct ext_sym_list *extsym_start = NULL;
 
-	while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
+	while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awEd")) != -1) {
 		switch (opt) {
 		case 'i':
 			kernel_read = optarg;
@@ -2538,6 +2577,9 @@ int main(int argc, char **argv)
 		case 'E':
 			sec_mismatch_fatal = 1;
 			break;
+		case 'd':
+			write_namespace_deps = 1;
+			break;
 		default:
 			exit(1);
 		}
@@ -2572,6 +2614,9 @@ int main(int argc, char **argv)
 
 		err |= check_modname_len(mod);
 		err |= check_exports(mod);
+		if (write_namespace_deps)
+			continue;
+
 		add_header(&buf, mod);
 		add_intree_flag(&buf, !external_module);
 		add_retpoline(&buf);
@@ -2584,6 +2629,12 @@ int main(int argc, char **argv)
 		sprintf(fname, "%s.mod.c", mod->name);
 		write_if_changed(&buf, fname);
 	}
+
+	if (write_namespace_deps) {
+		write_namespace_deps_files();
+		return 0;
+	}
+
 	if (dump_write)
 		write_dump(dump_write);
 	if (sec_mismatch_count && sec_mismatch_fatal)
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 9626bf3e7424..92a926d375d2 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -126,6 +126,8 @@ struct module {
 	struct buffer dev_table_buf;
 	char	     srcversion[25];
 	int is_dot_o;
+	// Required namespace dependencies
+	struct namespace_list *required_namespaces;
 	// Actual imported namespaces
 	struct namespace_list *imported_namespaces;
 };
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 08/10] scripts: Coccinelle script for namespace dependencies.
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
                     ` (6 preceding siblings ...)
  2019-08-13 12:17   ` [PATCH v2 07/10] modpost: add support for generating namespace dependencies Matthias Maennich
@ 2019-08-13 12:17   ` " Matthias Maennich
  2019-08-13 12:31     ` Julia Lawall
                       ` (4 more replies)
  2019-08-13 12:17   ` [PATCH v2 09/10] usb-storage: remove single-use define for debugging Matthias Maennich
                     ` (2 subsequent siblings)
  10 siblings, 5 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:17 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Julia Lawall, Gilles Muller,
	Nicolas Palix, Mauro Carvalho Chehab, David S. Miller,
	Jonathan Cameron, Nicolas Ferre, cocci

A script that uses the '<module>.ns_deps' file generated by modpost to
automatically add the required symbol namespace dependencies to each
module.

Usage:
1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
   DEFAULT_SYMBOL_NAMESPACE
2) Run 'make' (or 'make modules') and get warnings about modules not
   importing that namespace.
3) Run 'make nsdeps' to automatically add required import statements
   to said modules.

This makes it easer for subsystem maintainers to introduce and maintain
symbol namespaces into their codebase.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 MAINTAINERS                                 |  5 ++
 Makefile                                    | 12 +++++
 scripts/Makefile.modpost                    |  4 +-
 scripts/coccinelle/misc/add_namespace.cocci | 23 +++++++++
 scripts/nsdeps                              | 54 +++++++++++++++++++++
 5 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
 create mode 100644 scripts/nsdeps

diff --git a/MAINTAINERS b/MAINTAINERS
index e81e60bd7c26..aa169070a052 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11414,6 +11414,11 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
 F:	tools/include/nolibc/
 
+NSDEPS
+M:	Matthias Maennich <maennich@google.com>
+S:	Maintained
+F:	scripts/nsdeps
+
 NTB AMD DRIVER
 M:	Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
 L:	linux-ntb@googlegroups.com
diff --git a/Makefile b/Makefile
index 1b23f95db176..c5c3356e133c 100644
--- a/Makefile
+++ b/Makefile
@@ -1500,6 +1500,9 @@ help:
 	@echo  '  headerdep       - Detect inclusion cycles in headers'
 	@echo  '  coccicheck      - Check with Coccinelle'
 	@echo  ''
+	@echo  'Tools:'
+	@echo  '  nsdeps          - Generate missing symbol namespace dependencies'
+	@echo  ''
 	@echo  'Kernel selftest:'
 	@echo  '  kselftest       - Build and run kernel selftest (run as root)'
 	@echo  '                    Build, install, and boot kernel before'
@@ -1687,6 +1690,15 @@ quiet_cmd_tags = GEN     $@
 tags TAGS cscope gtags: FORCE
 	$(call cmd,tags)
 
+# Script to generate missing namespace dependencies
+# ---------------------------------------------------------------------------
+
+PHONY += nsdeps
+
+nsdeps:
+	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost nsdeps
+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@
+
 # Scripts to check various things for consistency
 # ---------------------------------------------------------------------------
 
diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
index 26e6574ecd08..743fe3a2e885 100644
--- a/scripts/Makefile.modpost
+++ b/scripts/Makefile.modpost
@@ -56,7 +56,8 @@ MODPOST = scripts/mod/modpost						\
 	$(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS)))	\
 	$(if $(KBUILD_EXTMOD),-o $(modulesymfile))			\
 	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)			\
-	$(if $(KBUILD_MODPOST_WARN),-w)
+	$(if $(KBUILD_MODPOST_WARN),-w)					\
+	$(if $(filter nsdeps,$(MAKECMDGOALS)),-d)
 
 ifdef MODPOST_VMLINUX
 
@@ -134,6 +135,7 @@ $(modules): %.ko :%.o %.mod.o FORCE
 
 targets += $(modules)
 
+nsdeps: __modpost
 
 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
 # ---------------------------------------------------------------------------
diff --git a/scripts/coccinelle/misc/add_namespace.cocci b/scripts/coccinelle/misc/add_namespace.cocci
new file mode 100644
index 000000000000..c832bb6445a8
--- /dev/null
+++ b/scripts/coccinelle/misc/add_namespace.cocci
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+/// Adds missing MODULE_IMPORT_NS statements to source files
+///
+/// This script is usually called from scripts/nsdeps with -D ns=<namespace> to
+/// add a missing namespace tag to a module source file.
+///
+
+@has_ns_import@
+declarer name MODULE_IMPORT_NS;
+identifier virtual.ns;
+@@
+MODULE_IMPORT_NS(ns);
+
+// Add missing imports, but only adjacent to a MODULE_LICENSE statement.
+// That ensures we are adding it only to the main module source file.
+@do_import depends on !has_ns_import@
+declarer name MODULE_LICENSE;
+expression license;
+identifier virtual.ns;
+@@
+MODULE_LICENSE(license);
++ MODULE_IMPORT_NS(ns);
diff --git a/scripts/nsdeps b/scripts/nsdeps
new file mode 100644
index 000000000000..148db65a830f
--- /dev/null
+++ b/scripts/nsdeps
@@ -0,0 +1,54 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Linux kernel symbol namespace import generator
+#
+# This script requires at least spatch
+# version 1.0.4.
+SPATCH_REQ_VERSION="1.0.4"
+
+DIR="$(dirname $(readlink -f $0))/.."
+SPATCH="`which ${SPATCH:=spatch}`"
+if [ ! -x "$SPATCH" ]; then
+    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
+    exit 1
+fi
+
+SPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh)
+SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
+SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
+
+if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
+    echo 'spatch needs to be version 1.06 or higher'
+    exit 1
+fi
+
+generate_deps_for_ns() {
+    $SPATCH --very-quiet --in-place --sp-file \
+	    $srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
+}
+
+generate_deps() {
+    local mod_file=`echo $@ | sed -e 's/\.ns_deps/\.mod/'`
+    local mod_name=`cat $mod_file | sed -n 1p | sed -e 's/\/[^.]*$//'`
+    local mod_source_files=`cat $mod_file | sed -n 2p | sed -e 's/\.o/\.c/g'`
+    for ns in `cat $@`; do
+	echo "Adding namespace $ns to module $mod_name (if needed)."
+        generate_deps_for_ns $ns $mod_source_files
+	# sort the imports
+        for source_file in $mod_source_files; do
+            sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
+            offset=$(wc -l ${source_file}.tmp | awk '{print $1;}')
+            cat $source_file | grep MODULE_IMPORT_NS | sort -u >> ${source_file}.tmp
+            tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp
+            if ! diff -q ${source_file} ${source_file}.tmp; then
+                mv ${source_file}.tmp ${source_file}
+            else
+                rm ${source_file}.tmp
+            fi
+        done
+    done
+}
+
+for f in `find $srctree/.tmp_versions/ -name *.ns_deps`; do
+    generate_deps $f
+done
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 09/10] usb-storage: remove single-use define for debugging
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
                     ` (7 preceding siblings ...)
  2019-08-13 12:17   ` [PATCH v2 08/10] scripts: Coccinelle script for " Matthias Maennich
@ 2019-08-13 12:17   ` Matthias Maennich
  2019-08-13 12:42     ` Greg KH
  2019-08-13 12:17   ` [PATCH v2 10/10] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
  10 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:17 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

USB_STORAGE was defined as "usb-storage: " and used in a single location
as argument to printk. In order to be able to use the name
'USB_STORAGE', drop the definition and use the string directly for the
printk call.

Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/usb/storage/debug.h    | 2 --
 drivers/usb/storage/scsiglue.c | 2 +-
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/usb/storage/debug.h b/drivers/usb/storage/debug.h
index 6d64f342f587..16ce06039a4d 100644
--- a/drivers/usb/storage/debug.h
+++ b/drivers/usb/storage/debug.h
@@ -29,8 +29,6 @@
 
 #include <linux/kernel.h>
 
-#define USB_STORAGE "usb-storage: "
-
 #ifdef CONFIG_USB_STORAGE_DEBUG
 void usb_stor_show_command(const struct us_data *us, struct scsi_cmnd *srb);
 void usb_stor_show_sense(const struct us_data *us, unsigned char key,
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 05b80211290d..df4de8323eff 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -379,7 +379,7 @@ static int queuecommand_lck(struct scsi_cmnd *srb,
 
 	/* check for state-transition errors */
 	if (us->srb != NULL) {
-		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
+		printk(KERN_ERR "usb-storage: Error in %s: us->srb = %p\n",
 			__func__, us->srb);
 		return SCSI_MLQUEUE_HOST_BUSY;
 	}
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v2 10/10] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
                     ` (8 preceding siblings ...)
  2019-08-13 12:17   ` [PATCH v2 09/10] usb-storage: remove single-use define for debugging Matthias Maennich
@ 2019-08-13 12:17   ` Matthias Maennich
  2019-08-13 12:45     ` Greg KH
  2019-08-13 12:47     ` Greg KH
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
  10 siblings, 2 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 12:17 UTC (permalink / raw)
  To: linux-kernel, maco
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

Modules using these symbols are required to explicitly import the
namespace. This patch was generated with the following steps and serves
as a reference to use the symbol namespace feature:

 1) Define DDEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
 2) make  (see warnings during modpost about missing imports)
 3) make nsdeps

Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
variants can be used to explicitly specify the namespace. The advantage
of the method used here is that newly added symbols are automatically
exported and existing ones are exported without touching their
respective EXPORT_SYMBOL macro expansion.

Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/usb/storage/Makefile        | 2 ++
 drivers/usb/storage/alauda.c        | 1 +
 drivers/usb/storage/cypress_atacb.c | 1 +
 drivers/usb/storage/datafab.c       | 1 +
 drivers/usb/storage/ene_ub6250.c    | 1 +
 drivers/usb/storage/freecom.c       | 1 +
 drivers/usb/storage/isd200.c        | 1 +
 drivers/usb/storage/jumpshot.c      | 1 +
 drivers/usb/storage/karma.c         | 1 +
 drivers/usb/storage/onetouch.c      | 1 +
 drivers/usb/storage/realtek_cr.c    | 1 +
 drivers/usb/storage/sddr09.c        | 1 +
 drivers/usb/storage/sddr55.c        | 1 +
 drivers/usb/storage/shuttle_usbat.c | 1 +
 drivers/usb/storage/uas.c           | 1 +
 15 files changed, 16 insertions(+)

diff --git a/drivers/usb/storage/Makefile b/drivers/usb/storage/Makefile
index a67ddcbb4e24..46635fa4a340 100644
--- a/drivers/usb/storage/Makefile
+++ b/drivers/usb/storage/Makefile
@@ -8,6 +8,8 @@
 
 ccflags-y := -I $(srctree)/drivers/scsi
 
+ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_STORAGE
+
 obj-$(CONFIG_USB_UAS)		+= uas.o
 obj-$(CONFIG_USB_STORAGE)	+= usb-storage.o
 
diff --git a/drivers/usb/storage/alauda.c b/drivers/usb/storage/alauda.c
index 6b8edf6178df..ddab2cd3d2e7 100644
--- a/drivers/usb/storage/alauda.c
+++ b/drivers/usb/storage/alauda.c
@@ -36,6 +36,7 @@
 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * Status bytes
diff --git a/drivers/usb/storage/cypress_atacb.c b/drivers/usb/storage/cypress_atacb.c
index 4825902377eb..a6f3267bbef6 100644
--- a/drivers/usb/storage/cypress_atacb.c
+++ b/drivers/usb/storage/cypress_atacb.c
@@ -22,6 +22,7 @@
 MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
 MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
index 09353be199be..588818483f4b 100644
--- a/drivers/usb/storage/datafab.c
+++ b/drivers/usb/storage/datafab.c
@@ -54,6 +54,7 @@
 MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
 MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 struct datafab_info {
 	unsigned long   sectors;	/* total sector count */
diff --git a/drivers/usb/storage/ene_ub6250.c b/drivers/usb/storage/ene_ub6250.c
index c26129d5b943..8b1b73065421 100644
--- a/drivers/usb/storage/ene_ub6250.c
+++ b/drivers/usb/storage/ene_ub6250.c
@@ -26,6 +26,7 @@
 
 MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 MODULE_FIRMWARE(SD_INIT1_FIRMWARE);
 MODULE_FIRMWARE(SD_INIT2_FIRMWARE);
 MODULE_FIRMWARE(SD_RW_FIRMWARE);
diff --git a/drivers/usb/storage/freecom.c b/drivers/usb/storage/freecom.c
index 4f542df37a44..34e7eaff1174 100644
--- a/drivers/usb/storage/freecom.c
+++ b/drivers/usb/storage/freecom.c
@@ -29,6 +29,7 @@
 MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor");
 MODULE_AUTHOR("David Brown <usb-storage@davidb.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #ifdef CONFIG_USB_STORAGE_DEBUG
 static void pdump(struct us_data *us, void *ibuffer, int length);
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index 2b474d60b4db..c4da3fd6eff9 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -53,6 +53,7 @@
 MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
 MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int isd200_Initialization(struct us_data *us);
 
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
index 917f170c4124..229bf0c1afc9 100644
--- a/drivers/usb/storage/jumpshot.c
+++ b/drivers/usb/storage/jumpshot.c
@@ -51,6 +51,7 @@
 MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
 MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/karma.c b/drivers/usb/storage/karma.c
index 395cf8fb5870..05cec81dcd3f 100644
--- a/drivers/usb/storage/karma.c
+++ b/drivers/usb/storage/karma.c
@@ -23,6 +23,7 @@
 MODULE_DESCRIPTION("Driver for Rio Karma");
 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>, Keith Bennett <keith@mcs.st-and.ac.uk>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #define RIO_PREFIX "RIOP\x00"
 #define RIO_PREFIX_LEN 5
diff --git a/drivers/usb/storage/onetouch.c b/drivers/usb/storage/onetouch.c
index 39a5009a41a6..a989fe930e21 100644
--- a/drivers/usb/storage/onetouch.c
+++ b/drivers/usb/storage/onetouch.c
@@ -25,6 +25,7 @@
 MODULE_DESCRIPTION("Maxtor USB OneTouch hard drive button driver");
 MODULE_AUTHOR("Nick Sillik <n.sillik@temple.edu>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #define ONETOUCH_PKT_LEN        0x02
 #define ONETOUCH_BUTTON         KEY_PROG1
diff --git a/drivers/usb/storage/realtek_cr.c b/drivers/usb/storage/realtek_cr.c
index cc794e25a0b6..edbe419053d6 100644
--- a/drivers/usb/storage/realtek_cr.c
+++ b/drivers/usb/storage/realtek_cr.c
@@ -35,6 +35,7 @@
 MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
 MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int auto_delink_en = 1;
 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
diff --git a/drivers/usb/storage/sddr09.c b/drivers/usb/storage/sddr09.c
index bc9da736bdfc..51bcd4a43690 100644
--- a/drivers/usb/storage/sddr09.c
+++ b/drivers/usb/storage/sddr09.c
@@ -47,6 +47,7 @@
 MODULE_DESCRIPTION("Driver for SanDisk SDDR-09 SmartMedia reader");
 MODULE_AUTHOR("Andries Brouwer <aeb@cwi.nl>, Robert Baruch <autophile@starband.net>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int usb_stor_sddr09_dpcm_init(struct us_data *us);
 static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us);
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index b8527c55335b..ba955d65eb0e 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -29,6 +29,7 @@
 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
 MODULE_AUTHOR("Simon Munton");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 854498e1012c..54aa1392c9ca 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -48,6 +48,7 @@
 MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /* Supported device types */
 #define USBAT_DEV_HP8200	0x01
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index 047c5922618f..bf80d6f81f58 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -1219,5 +1219,6 @@ static struct usb_driver uas_driver = {
 module_usb_driver(uas_driver);
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 MODULE_AUTHOR(
 	"Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* Re: [PATCH v2 08/10] scripts: Coccinelle script for namespace dependencies.
  2019-08-13 12:17   ` [PATCH v2 08/10] scripts: Coccinelle script for " Matthias Maennich
@ 2019-08-13 12:31     ` Julia Lawall
  2019-08-13 12:44     ` Greg KH
                       ` (3 subsequent siblings)
  4 siblings, 0 replies; 105+ messages in thread
From: Julia Lawall @ 2019-08-13 12:31 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, gregkh, hpa, jeyu,
	joel, kstewart, linux-arch, linux-kbuild, linux-m68k,
	linux-modules, linux-scsi, linux-usb, lucas.de.marchi, maco,
	michal.lkml, mingo, oneukum, pombredanne, sam, sboyd, sspatil,
	stern, tglx, usb-storage, x86, yamada.masahiro, Julia Lawall,
	Gilles Muller, Nicolas Palix, Mauro Carvalho Chehab,
	David S. Miller, Jonathan Cameron, Nicolas Ferre, cocci



On Tue, 13 Aug 2019, Matthias Maennich wrote:

> A script that uses the '<module>.ns_deps' file generated by modpost to
> automatically add the required symbol namespace dependencies to each
> module.
>
> Usage:
> 1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
>    DEFAULT_SYMBOL_NAMESPACE
> 2) Run 'make' (or 'make modules') and get warnings about modules not
>    importing that namespace.
> 3) Run 'make nsdeps' to automatically add required import statements
>    to said modules.
>
> This makes it easer for subsystem maintainers to introduce and maintain
> symbol namespaces into their codebase.
>
> Co-developed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>

Acked-by: Julia Lawall <julia.lawall@lip6.fr>


> ---
>  MAINTAINERS                                 |  5 ++
>  Makefile                                    | 12 +++++
>  scripts/Makefile.modpost                    |  4 +-
>  scripts/coccinelle/misc/add_namespace.cocci | 23 +++++++++
>  scripts/nsdeps                              | 54 +++++++++++++++++++++
>  5 files changed, 97 insertions(+), 1 deletion(-)
>  create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
>  create mode 100644 scripts/nsdeps
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e81e60bd7c26..aa169070a052 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -11414,6 +11414,11 @@ S:	Maintained
>  T:	git git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
>  F:	tools/include/nolibc/
>
> +NSDEPS
> +M:	Matthias Maennich <maennich@google.com>
> +S:	Maintained
> +F:	scripts/nsdeps
> +
>  NTB AMD DRIVER
>  M:	Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
>  L:	linux-ntb@googlegroups.com
> diff --git a/Makefile b/Makefile
> index 1b23f95db176..c5c3356e133c 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1500,6 +1500,9 @@ help:
>  	@echo  '  headerdep       - Detect inclusion cycles in headers'
>  	@echo  '  coccicheck      - Check with Coccinelle'
>  	@echo  ''
> +	@echo  'Tools:'
> +	@echo  '  nsdeps          - Generate missing symbol namespace dependencies'
> +	@echo  ''
>  	@echo  'Kernel selftest:'
>  	@echo  '  kselftest       - Build and run kernel selftest (run as root)'
>  	@echo  '                    Build, install, and boot kernel before'
> @@ -1687,6 +1690,15 @@ quiet_cmd_tags = GEN     $@
>  tags TAGS cscope gtags: FORCE
>  	$(call cmd,tags)
>
> +# Script to generate missing namespace dependencies
> +# ---------------------------------------------------------------------------
> +
> +PHONY += nsdeps
> +
> +nsdeps:
> +	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost nsdeps
> +	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@
> +
>  # Scripts to check various things for consistency
>  # ---------------------------------------------------------------------------
>
> diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
> index 26e6574ecd08..743fe3a2e885 100644
> --- a/scripts/Makefile.modpost
> +++ b/scripts/Makefile.modpost
> @@ -56,7 +56,8 @@ MODPOST = scripts/mod/modpost						\
>  	$(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS)))	\
>  	$(if $(KBUILD_EXTMOD),-o $(modulesymfile))			\
>  	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)			\
> -	$(if $(KBUILD_MODPOST_WARN),-w)
> +	$(if $(KBUILD_MODPOST_WARN),-w)					\
> +	$(if $(filter nsdeps,$(MAKECMDGOALS)),-d)
>
>  ifdef MODPOST_VMLINUX
>
> @@ -134,6 +135,7 @@ $(modules): %.ko :%.o %.mod.o FORCE
>
>  targets += $(modules)
>
> +nsdeps: __modpost
>
>  # Add FORCE to the prequisites of a target to force it to be always rebuilt.
>  # ---------------------------------------------------------------------------
> diff --git a/scripts/coccinelle/misc/add_namespace.cocci b/scripts/coccinelle/misc/add_namespace.cocci
> new file mode 100644
> index 000000000000..c832bb6445a8
> --- /dev/null
> +++ b/scripts/coccinelle/misc/add_namespace.cocci
> @@ -0,0 +1,23 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +//
> +/// Adds missing MODULE_IMPORT_NS statements to source files
> +///
> +/// This script is usually called from scripts/nsdeps with -D ns=<namespace> to
> +/// add a missing namespace tag to a module source file.
> +///
> +
> +@has_ns_import@
> +declarer name MODULE_IMPORT_NS;
> +identifier virtual.ns;
> +@@
> +MODULE_IMPORT_NS(ns);
> +
> +// Add missing imports, but only adjacent to a MODULE_LICENSE statement.
> +// That ensures we are adding it only to the main module source file.
> +@do_import depends on !has_ns_import@
> +declarer name MODULE_LICENSE;
> +expression license;
> +identifier virtual.ns;
> +@@
> +MODULE_LICENSE(license);
> ++ MODULE_IMPORT_NS(ns);
> diff --git a/scripts/nsdeps b/scripts/nsdeps
> new file mode 100644
> index 000000000000..148db65a830f
> --- /dev/null
> +++ b/scripts/nsdeps
> @@ -0,0 +1,54 @@
> +#!/bin/bash
> +# SPDX-License-Identifier: GPL-2.0
> +# Linux kernel symbol namespace import generator
> +#
> +# This script requires at least spatch
> +# version 1.0.4.
> +SPATCH_REQ_VERSION="1.0.4"
> +
> +DIR="$(dirname $(readlink -f $0))/.."
> +SPATCH="`which ${SPATCH:=spatch}`"
> +if [ ! -x "$SPATCH" ]; then
> +    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
> +    exit 1
> +fi
> +
> +SPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh)
> +SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
> +SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
> +
> +if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
> +    echo 'spatch needs to be version 1.06 or higher'
> +    exit 1
> +fi
> +
> +generate_deps_for_ns() {
> +    $SPATCH --very-quiet --in-place --sp-file \
> +	    $srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
> +}
> +
> +generate_deps() {
> +    local mod_file=`echo $@ | sed -e 's/\.ns_deps/\.mod/'`
> +    local mod_name=`cat $mod_file | sed -n 1p | sed -e 's/\/[^.]*$//'`
> +    local mod_source_files=`cat $mod_file | sed -n 2p | sed -e 's/\.o/\.c/g'`
> +    for ns in `cat $@`; do
> +	echo "Adding namespace $ns to module $mod_name (if needed)."
> +        generate_deps_for_ns $ns $mod_source_files
> +	# sort the imports
> +        for source_file in $mod_source_files; do
> +            sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
> +            offset=$(wc -l ${source_file}.tmp | awk '{print $1;}')
> +            cat $source_file | grep MODULE_IMPORT_NS | sort -u >> ${source_file}.tmp
> +            tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp
> +            if ! diff -q ${source_file} ${source_file}.tmp; then
> +                mv ${source_file}.tmp ${source_file}
> +            else
> +                rm ${source_file}.tmp
> +            fi
> +        done
> +    done
> +}
> +
> +for f in `find $srctree/.tmp_versions/ -name *.ns_deps`; do
> +    generate_deps $f
> +done
> --
> 2.23.0.rc1.153.gdeed80330f-goog
>
>

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

* Re: [PATCH v2 01/10] module: support reading multiple values per modinfo tag
  2019-08-13 12:16   ` [PATCH v2 01/10] module: support reading multiple values per modinfo tag Matthias Maennich
@ 2019-08-13 12:40     ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 12:40 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Tue, Aug 13, 2019 at 01:16:58PM +0100, Matthias Maennich wrote:
> Similar to modpost's get_next_modinfo(), introduce get_next_modinfo() in
> kernel/module.c to acquire any further values associated with the same
> modinfo tag name. That is useful for any tags that have multiple
> occurrences (such as 'alias'), but is in particular introduced here as
> part of the symbol namespaces patch series to read the (potentially)
> multiple namespaces a module is importing.
> 
> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> Reviewed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2 02/10] export: explicitly align struct kernel_symbol
  2019-08-13 12:16   ` [PATCH v2 02/10] export: explicitly align struct kernel_symbol Matthias Maennich
@ 2019-08-13 12:41     ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 12:41 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton, Nicolas Pitre,
	Michael Ellerman, Ard Biesheuvel

On Tue, Aug 13, 2019 at 01:16:59PM +0100, Matthias Maennich wrote:
> This change allows growing struct kernel_symbol without wasting bytes to
> alignment. It also concretized the alignment of ksymtab entries if
> relative references are used for ksymtab entries.
> 
> struct kernel_symbol was already implicitly being aligned to the word
> size, except on x86_64 and m68k, where it is aligned to 16 and 2 bytes,
> respectively.
> 
> As far as I can tell there is no requirement for aligning struct
> kernel_symbol to 16 bytes on x86_64, but gcc aligns structs to their
> size, and the linker aligns the custom __ksymtab sections to the largest
> data type contained within, so setting KSYM_ALIGN to 16 was necessary to
> stay consistent with the code generated for non-ASM EXPORT_SYMBOL(). Now
> that non-ASM EXPORT_SYMBOL() explicitly aligns to word size (8),
> KSYM_ALIGN is no longer necessary.
> 
> In case of relative references, the alignment has been changed
> accordingly to not waste space when adding new struct members.
> 
> As for m68k, struct kernel_symbol is aligned to 2 bytes even though the
> structure itself is 8 bytes; using a 4-byte alignment shouldn't hurt.
> 
> I manually verified the output of the __ksymtab sections didn't change
> on x86, x86_64, arm, arm64 and m68k. As expected, the section contents
> didn't change, and the ELF section alignment only changed on x86_64 and
> m68k. Feedback from other archs more than welcome.
> 
> Co-developed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>

Ick, messy, nice fix.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2 09/10] usb-storage: remove single-use define for debugging
  2019-08-13 12:17   ` [PATCH v2 09/10] usb-storage: remove single-use define for debugging Matthias Maennich
@ 2019-08-13 12:42     ` Greg KH
  2019-08-13 13:12       ` Greg KH
  0 siblings, 1 reply; 105+ messages in thread
From: Greg KH @ 2019-08-13 12:42 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Tue, Aug 13, 2019 at 01:17:06PM +0100, Matthias Maennich wrote:
> USB_STORAGE was defined as "usb-storage: " and used in a single location
> as argument to printk. In order to be able to use the name
> 'USB_STORAGE', drop the definition and use the string directly for the
> printk call.
> 
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---
>  drivers/usb/storage/debug.h    | 2 --
>  drivers/usb/storage/scsiglue.c | 2 +-
>  2 files changed, 1 insertion(+), 3 deletions(-)

I'll go take this today.  The module really should just be using
dev_err() there.  It needs to be cleaned up :(

thanks,

greg k-h

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

* Re: [PATCH v2 08/10] scripts: Coccinelle script for namespace dependencies.
  2019-08-13 12:17   ` [PATCH v2 08/10] scripts: Coccinelle script for " Matthias Maennich
  2019-08-13 12:31     ` Julia Lawall
@ 2019-08-13 12:44     ` Greg KH
  2019-08-14  6:36     ` [Cocci] " Himanshu Jha
                       ` (2 subsequent siblings)
  4 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 12:44 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Julia Lawall, Gilles Muller,
	Nicolas Palix, Mauro Carvalho Chehab, David S. Miller,
	Jonathan Cameron, Nicolas Ferre, cocci

On Tue, Aug 13, 2019 at 01:17:05PM +0100, Matthias Maennich wrote:
> A script that uses the '<module>.ns_deps' file generated by modpost to
> automatically add the required symbol namespace dependencies to each
> module.
> 
> Usage:
> 1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
>    DEFAULT_SYMBOL_NAMESPACE
> 2) Run 'make' (or 'make modules') and get warnings about modules not
>    importing that namespace.
> 3) Run 'make nsdeps' to automatically add required import statements
>    to said modules.
> 
> This makes it easer for subsystem maintainers to introduce and maintain
> symbol namespaces into their codebase.
> 
> Co-developed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---

I really can't express just how cool this patch is.  I was amazed when I
first saw it in action a long time ago, and still am.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2 10/10] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-08-13 12:17   ` [PATCH v2 10/10] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
@ 2019-08-13 12:45     ` Greg KH
  2019-08-13 12:47     ` Greg KH
  1 sibling, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 12:45 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Tue, Aug 13, 2019 at 01:17:07PM +0100, Matthias Maennich wrote:
> Modules using these symbols are required to explicitly import the
> namespace. This patch was generated with the following steps and serves
> as a reference to use the symbol namespace feature:
> 
>  1) Define DDEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
>  2) make  (see warnings during modpost about missing imports)
>  3) make nsdeps
> 
> Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
> variants can be used to explicitly specify the namespace. The advantage
> of the method used here is that newly added symbols are automatically
> exported and existing ones are exported without touching their
> respective EXPORT_SYMBOL macro expansion.
> 
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---
>  drivers/usb/storage/Makefile        | 2 ++
>  drivers/usb/storage/alauda.c        | 1 +
>  drivers/usb/storage/cypress_atacb.c | 1 +
>  drivers/usb/storage/datafab.c       | 1 +
>  drivers/usb/storage/ene_ub6250.c    | 1 +
>  drivers/usb/storage/freecom.c       | 1 +
>  drivers/usb/storage/isd200.c        | 1 +
>  drivers/usb/storage/jumpshot.c      | 1 +
>  drivers/usb/storage/karma.c         | 1 +
>  drivers/usb/storage/onetouch.c      | 1 +
>  drivers/usb/storage/realtek_cr.c    | 1 +
>  drivers/usb/storage/sddr09.c        | 1 +
>  drivers/usb/storage/sddr55.c        | 1 +
>  drivers/usb/storage/shuttle_usbat.c | 1 +
>  drivers/usb/storage/uas.c           | 1 +
>  15 files changed, 16 insertions(+)
> 
> diff --git a/drivers/usb/storage/Makefile b/drivers/usb/storage/Makefile
> index a67ddcbb4e24..46635fa4a340 100644
> --- a/drivers/usb/storage/Makefile
> +++ b/drivers/usb/storage/Makefile
> @@ -8,6 +8,8 @@
>  
>  ccflags-y := -I $(srctree)/drivers/scsi
>  
> +ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_STORAGE

Wait, we have to do this for every subsystem?  I thought there was a
macro we could use in the code itself for this.  What changed from
earlier versions, or was this always here?

thanks,

greg k-h

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

* Re: [PATCH v2 10/10] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-08-13 12:17   ` [PATCH v2 10/10] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
  2019-08-13 12:45     ` Greg KH
@ 2019-08-13 12:47     ` Greg KH
  2019-08-13 15:02       ` Matthias Maennich
  1 sibling, 1 reply; 105+ messages in thread
From: Greg KH @ 2019-08-13 12:47 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Tue, Aug 13, 2019 at 01:17:07PM +0100, Matthias Maennich wrote:
> Modules using these symbols are required to explicitly import the
> namespace. This patch was generated with the following steps and serves
> as a reference to use the symbol namespace feature:
> 
>  1) Define DDEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
>  2) make  (see warnings during modpost about missing imports)
>  3) make nsdeps
> 
> Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
> variants can be used to explicitly specify the namespace. The advantage
> of the method used here is that newly added symbols are automatically
> exported and existing ones are exported without touching their
> respective EXPORT_SYMBOL macro expansion.

Ok, I can't read text, this answers my previous question.

But, as an example, shouldn't we also have some code here that uses the
EXPORT_SYMBOL_NS() macro to ensure that it actually works?

thanks,

greg k-h

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

* Re: [PATCH v2 09/10] usb-storage: remove single-use define for debugging
  2019-08-13 12:42     ` Greg KH
@ 2019-08-13 13:12       ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 13:12 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Tue, Aug 13, 2019 at 02:42:59PM +0200, Greg KH wrote:
> On Tue, Aug 13, 2019 at 01:17:06PM +0100, Matthias Maennich wrote:
> > USB_STORAGE was defined as "usb-storage: " and used in a single location
> > as argument to printk. In order to be able to use the name
> > 'USB_STORAGE', drop the definition and use the string directly for the
> > printk call.
> > 
> > Signed-off-by: Matthias Maennich <maennich@google.com>
> > ---
> >  drivers/usb/storage/debug.h    | 2 --
> >  drivers/usb/storage/scsiglue.c | 2 +-
> >  2 files changed, 1 insertion(+), 3 deletions(-)
> 
> I'll go take this today.  The module really should just be using
> dev_err() there.  It needs to be cleaned up :(

Now applied to the usb git tree, thanks.

greg k-h

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

* Re: [PATCH v2 10/10] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-08-13 12:47     ` Greg KH
@ 2019-08-13 15:02       ` Matthias Maennich
  0 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-13 15:02 UTC (permalink / raw)
  To: Greg KH
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Tue, Aug 13, 2019 at 02:47:08PM +0200, Greg KH wrote:
>On Tue, Aug 13, 2019 at 01:17:07PM +0100, Matthias Maennich wrote:
>> Modules using these symbols are required to explicitly import the
>> namespace. This patch was generated with the following steps and serves
>> as a reference to use the symbol namespace feature:
>>
>>  1) Define DDEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
>>  2) make  (see warnings during modpost about missing imports)
>>  3) make nsdeps
>>
>> Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
>> variants can be used to explicitly specify the namespace. The advantage
>> of the method used here is that newly added symbols are automatically
>> exported and existing ones are exported without touching their
>> respective EXPORT_SYMBOL macro expansion.
>
>Ok, I can't read text, this answers my previous question.
>
>But, as an example, shouldn't we also have some code here that uses the
>EXPORT_SYMBOL_NS() macro to ensure that it actually works?
>
I will create another patch for a different subsystem where the use of
the macros is more appropriate. Then we have both use cases covered.

Cheers,
Matthias


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

* Re: [PATCH v2 03/10] module: add support for symbol namespaces.
  2019-08-13 12:17   ` [PATCH v2 03/10] module: add support for symbol namespaces Matthias Maennich
@ 2019-08-13 15:26     ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 15:26 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton,
	Michael Ellerman, Ard Biesheuvel

On Tue, Aug 13, 2019 at 01:17:00PM +0100, Matthias Maennich wrote:
> The EXPORT_SYMBOL_NS() and EXPORT_SYMBOL_NS_GPL() macros can be used to
> export a symbol to a specific namespace.  There are no _GPL_FUTURE and
> _UNUSED variants because these are currently unused, and I'm not sure
> they are necessary.
> 
> I didn't add EXPORT_SYMBOL_NS() for ASM exports; this patch sets the
> namespace of ASM exports to NULL by default. In case of relative
> references, it will be relocatable to NULL. If there's a need, this
> should be pretty easy to add.
> 
> A module that wants to use a symbol exported to a namespace must add a
> MODULE_IMPORT_NS() statement to their module code; otherwise, modpost
> will complain when building the module, and the kernel module loader
> will emit an error and fail when loading the module.
> 
> MODULE_IMPORT_NS() adds a modinfo tag 'import_ns' to the module. That
> tag can be observed by the modinfo command, modpost and kernel/module.c
> at the time of loading the module.
> 
> The ELF symbols are renamed to include the namespace with an asm label;
> for example, symbol 'usb_stor_suspend' in namespace USB_STORAGE becomes
> 'usb_stor_suspend.USB_STORAGE'.  This allows modpost to do namespace
> checking, without having to go through all the effort of parsing ELF and
> relocation records just to get to the struct kernel_symbols.
> 
> On x86_64 I saw no difference in binary size (compression), but at
> runtime this will require a word of memory per export to hold the
> namespace. An alternative could be to store namespaced symbols in their
> own section and use a separate 'struct namespaced_kernel_symbol' for
> that section, at the cost of making the module loader more complex.
> 
> Co-developed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2 04/10] modpost: add support for symbol namespaces
  2019-08-13 12:17   ` [PATCH v2 04/10] modpost: " Matthias Maennich
@ 2019-08-13 15:27     ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 15:27 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Tue, Aug 13, 2019 at 01:17:01PM +0100, Matthias Maennich wrote:
> Add support for symbols that are exported into namespaces. For that,
> extract any namespace suffix from the symbol name. In addition, emit a
> warning whenever a module refers to an exported symbol without
> explicitly importing the namespace that it is defined in. This patch
> consistently adds the namespace suffix to symbol names exported into
> Module.symvers.
> 
> Example warning emitted by modpost in case of the above violation:
> 
>  WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
>  USB_STORAGE, but does not import it.
> 
> Co-developed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Martijn Coenen <maco@android.com>
> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---
>  scripts/mod/modpost.c | 91 +++++++++++++++++++++++++++++++++++++------
>  scripts/mod/modpost.h |  7 ++++
>  2 files changed, 87 insertions(+), 11 deletions(-)

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2 06/10] export: allow definition default namespaces in Makefiles or sources
  2019-08-13 12:17   ` [PATCH v2 06/10] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
@ 2019-08-13 18:16     ` Greg KH
  2019-08-13 18:16     ` Greg KH
  1 sibling, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 18:16 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Ard Biesheuvel,
	Michael Ellerman

On Tue, Aug 13, 2019 at 01:17:03PM +0100, Matthias Maennich wrote:
> To avoid excessive usage of EXPORT_SYMBOL_NS(sym, MY_NAMESPACE), where
> MY_NAMESPACE will always be the namespace we are exporting to, allow
> exporting all definitions of EXPORT_SYMBOL() and friends by defining
> DEFAULT_SYMBOL_NAMESPACE.
> 
> For example, to export all symbols defined in usb-common into the
> namespace USB_COMMON, add a line like this to drivers/usb/common/Makefile:
> 
>   ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON

I thought we were trying to get away from cflags :(

> 
> That is equivalent to changing all EXPORT_SYMBOL(sym) definitions to
> EXPORT_SYMBOL_NS(sym, USB_COMMON). Subsequently all symbol namespaces
> functionality will apply.
> 
> Another way of making use of this feature is to define the namespace
> within source or header files similar to how TRACE_SYSTEM defines are
> used:
>   #undef DEFAULT_SYMBOL_NAMESPACE
>   #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON
> 
> Please note that, as opposed to TRACE_SYSTEM, DEFAULT_SYMBOL_NAMESPACE
> has to be defined before including include/linux/export.h.
> 
> If DEFAULT_SYMBOL_NAMESPACE is defined, a symbol can still be exported
> to another namespace by using EXPORT_SYMBOL_NS() and friends with
> explicitly specifying the namespace.

Ok, good, hopefully the cflags stuff will not be the default for people.

thanks,

greg k-h

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

* Re: [PATCH v2 06/10] export: allow definition default namespaces in Makefiles or sources
  2019-08-13 12:17   ` [PATCH v2 06/10] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
  2019-08-13 18:16     ` Greg KH
@ 2019-08-13 18:16     ` Greg KH
  1 sibling, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 18:16 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Ard Biesheuvel,
	Michael Ellerman

On Tue, Aug 13, 2019 at 01:17:03PM +0100, Matthias Maennich wrote:
> To avoid excessive usage of EXPORT_SYMBOL_NS(sym, MY_NAMESPACE), where
> MY_NAMESPACE will always be the namespace we are exporting to, allow
> exporting all definitions of EXPORT_SYMBOL() and friends by defining
> DEFAULT_SYMBOL_NAMESPACE.
> 
> For example, to export all symbols defined in usb-common into the
> namespace USB_COMMON, add a line like this to drivers/usb/common/Makefile:
> 
>   ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON
> 
> That is equivalent to changing all EXPORT_SYMBOL(sym) definitions to
> EXPORT_SYMBOL_NS(sym, USB_COMMON). Subsequently all symbol namespaces
> functionality will apply.
> 
> Another way of making use of this feature is to define the namespace
> within source or header files similar to how TRACE_SYSTEM defines are
> used:
>   #undef DEFAULT_SYMBOL_NAMESPACE
>   #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON
> 
> Please note that, as opposed to TRACE_SYSTEM, DEFAULT_SYMBOL_NAMESPACE
> has to be defined before including include/linux/export.h.
> 
> If DEFAULT_SYMBOL_NAMESPACE is defined, a symbol can still be exported
> to another namespace by using EXPORT_SYMBOL_NS() and friends with
> explicitly specifying the namespace.
> 
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Reviewed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---
>  include/linux/export.h | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/include/linux/export.h b/include/linux/export.h
> index 8e12e05444d1..1fb243abdbc4 100644
> --- a/include/linux/export.h
> +++ b/include/linux/export.h
> @@ -166,6 +166,12 @@ struct kernel_symbol {
>  #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
>  #endif
>  
> +#ifdef DEFAULT_SYMBOL_NAMESPACE
> +#undef __EXPORT_SYMBOL
> +#define __EXPORT_SYMBOL(sym, sec)				\
> +	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
> +#endif
> +
>  #define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
>  #define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
>  #define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
> -- 
> 2.23.0.rc1.153.gdeed80330f-goog
> 

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  2019-08-13 12:17   ` [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
@ 2019-08-13 18:17     ` Greg KH
  2019-08-13 20:15     ` Saravana Kannan
  1 sibling, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 18:17 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton,
	Johannes Weiner, Peter Zijlstra (Intel),
	Mauro Carvalho Chehab, David Howells, Patrick Bellasi,
	Dan Williams, Adrian Reber, Richard Guy Briggs

On Tue, Aug 13, 2019 at 01:17:02PM +0100, Matthias Maennich wrote:
> If MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is enabled (default=n), the
> requirement for modules to import all namespaces that are used by
> the module is relaxed.
> 
> Enabling this option effectively allows (invalid) modules to be loaded
> while only a warning is emitted.
> 
> Disabling this option keeps the enforcement at module loading time and
> loading is denied if the module's imports are not satisfactory.
> 
> Reviewed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---
>  init/Kconfig    | 14 ++++++++++++++
>  kernel/module.c | 11 +++++++++--
>  2 files changed, 23 insertions(+), 2 deletions(-)
> 
> diff --git a/init/Kconfig b/init/Kconfig
> index bd7d650d4a99..b3373334cdf1 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -2119,6 +2119,20 @@ config MODULE_COMPRESS_XZ
>  
>  endchoice
>  
> +config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
> +	bool "Allow loading of modules with missing namespace imports"
> +	default n

the default for config options is always N, no need to list it here.

> +	help
> +	  Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
> +	  a namespace. A module that makes use of a symbol exported with such a
> +	  namespace is required to import the namespace via MODULE_IMPORT_NS().
> +	  This option relaxes this requirement when loading a module. While
> +	  technically there is no reason to enforce correct namespace imports,
> +	  it creates consistency between symbols defining namespaces and users
> +	  importing namespaces they make use of.
> +
> +	  If unsure, say N.
> +
>  config TRIM_UNUSED_KSYMS
>  	bool "Trim unused exported kernel symbols"
>  	depends on MODULES && !UNUSED_SYMBOLS
> diff --git a/kernel/module.c b/kernel/module.c
> index 57e8253f2251..7c934aaae2d3 100644
> --- a/kernel/module.c
> +++ b/kernel/module.c
> @@ -1408,9 +1408,16 @@ static int verify_namespace_is_imported(const struct load_info *info,
>  			imported_namespace = get_next_modinfo(
>  				info, "import_ns", imported_namespace);
>  		}
> -		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
> -		       mod->name, kernel_symbol_name(sym), namespace);
> +#ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
> +		pr_warn(
> +#else
> +		pr_err(
> +#endif
> +			"%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
> +			mod->name, kernel_symbol_name(sym), namespace);
> +#ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
>  		return -EINVAL;
> +#endif

This #ifdef mess is a hack, but oh well :)

If you drop the above default line, feel free to add:

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2 07/10] modpost: add support for generating namespace dependencies
  2019-08-13 12:17   ` [PATCH v2 07/10] modpost: add support for generating namespace dependencies Matthias Maennich
@ 2019-08-13 18:21     ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-13 18:21 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Tue, Aug 13, 2019 at 01:17:04PM +0100, Matthias Maennich wrote:
> This patch adds an option to modpost to generate a <module>.ns_deps file
> per module, containing the namespace dependencies for that module.
> 
> E.g. if the linked module my-module.ko would depend on the symbol
> myfunc.MY_NS in the namespace MY_NS, the my-module.ns_deps file created
> by modpost would contain the entry MY_NS to express the namespace
> dependency of my-module imposed by using the symbol myfunc.
> 
> These files can subsequently be used by static analysis tools (like
> coccinelle scripts) to address issues with missing namespace imports. A
> later patch of this series will introduce such a script 'nsdeps' and a
> corresponding make target to automatically add missing
> MODULE_IMPORT_NS() definitions to the module's sources. For that it uses
> the information provided in the generated .ns_deps files.
> 
> Co-developed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---
>  scripts/mod/modpost.c | 61 +++++++++++++++++++++++++++++++++++++++----
>  scripts/mod/modpost.h |  2 ++
>  2 files changed, 58 insertions(+), 5 deletions(-)

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  2019-08-13 12:17   ` [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
  2019-08-13 18:17     ` Greg KH
@ 2019-08-13 20:15     ` Saravana Kannan
  2019-08-14 12:54       ` Matthias Maennich
  1 sibling, 1 reply; 105+ messages in thread
From: Saravana Kannan @ 2019-08-13 20:15 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: LKML, maco, Android Kernel Team, arnd, geert, Greg Kroah-Hartman,
	hpa, jeyu, Joel Fernandes (Google),
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, sboyd, Sandeep Patil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton,
	Johannes Weiner, Peter Zijlstra (Intel),
	Mauro Carvalho Chehab, David Howells, Patrick Bellasi,
	Dan Williams, Adrian Reber, Richard Guy Briggs

On Tue, Aug 13, 2019 at 5:19 AM 'Matthias Maennich' via kernel-team
<kernel-team@android.com> wrote:
>
> If MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is enabled (default=n), the
> requirement for modules to import all namespaces that are used by
> the module is relaxed.
>
> Enabling this option effectively allows (invalid) modules to be loaded
> while only a warning is emitted.
>
> Disabling this option keeps the enforcement at module loading time and
> loading is denied if the module's imports are not satisfactory.
>
> Reviewed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---
>  init/Kconfig    | 14 ++++++++++++++
>  kernel/module.c | 11 +++++++++--
>  2 files changed, 23 insertions(+), 2 deletions(-)
>
> diff --git a/init/Kconfig b/init/Kconfig
> index bd7d650d4a99..b3373334cdf1 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -2119,6 +2119,20 @@ config MODULE_COMPRESS_XZ
>
>  endchoice
>
> +config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
> +       bool "Allow loading of modules with missing namespace imports"
> +       default n
> +       help
> +         Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
> +         a namespace. A module that makes use of a symbol exported with such a
> +         namespace is required to import the namespace via MODULE_IMPORT_NS().
> +         This option relaxes this requirement when loading a module.

> While
> +         technically there is no reason to enforce correct namespace imports,
> +         it creates consistency between symbols defining namespaces and users
> +         importing namespaces they make use of.

I'm confused by this sentence. It sounds like it's the opposite of
what the config is doing? Can you please reword it for clarify?

-Saravana

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

* Re: [Cocci] [PATCH v2 08/10] scripts: Coccinelle script for namespace dependencies.
  2019-08-13 12:17   ` [PATCH v2 08/10] scripts: Coccinelle script for " Matthias Maennich
  2019-08-13 12:31     ` Julia Lawall
  2019-08-13 12:44     ` Greg KH
@ 2019-08-14  6:36     ` " Himanshu Jha
  2019-08-14  8:03       ` Matthias Maennich
  2019-08-14 12:00     ` [v2 " Markus Elfring
  2019-08-15 13:50     ` Markus Elfring
  4 siblings, 1 reply; 105+ messages in thread
From: Himanshu Jha @ 2019-08-14  6:36 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, maco, kstewart, linux-m68k, oneukum,
	Mauro Carvalho Chehab, usb-storage, hpa, joel, sam, cocci,
	linux-arch, linux-scsi, x86, lucas.de.marchi, mingo, geert,
	stern, kernel-team, sspatil, arnd, linux-kbuild, Nicolas Palix,
	jeyu, Jonathan Cameron, tglx, michal.lkml, gregkh, linux-usb,
	sboyd, Nicolas Ferre, maco, pombredanne, David S. Miller,
	linux-modules

On Tue, Aug 13, 2019 at 01:17:05PM +0100, Matthias Maennich wrote:
> A script that uses the '<module>.ns_deps' file generated by modpost to
> automatically add the required symbol namespace dependencies to each
> module.
> 
> Usage:
> 1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
>    DEFAULT_SYMBOL_NAMESPACE
> 2) Run 'make' (or 'make modules') and get warnings about modules not
>    importing that namespace.
> 3) Run 'make nsdeps' to automatically add required import statements
>    to said modules.
> 
> This makes it easer for subsystem maintainers to introduce and maintain
> symbol namespaces into their codebase.
> 
> Co-developed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---

[]

>  MAINTAINERS                                 |  5 ++
>  Makefile                                    | 12 +++++
>  scripts/Makefile.modpost                    |  4 +-
>  scripts/coccinelle/misc/add_namespace.cocci | 23 +++++++++
>  scripts/nsdeps                              | 54 +++++++++++++++++++++
>  5 files changed, 97 insertions(+), 1 deletion(-)
>  create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
>  create mode 100644 scripts/nsdeps

[]

> +if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
> +    echo 'spatch needs to be version 1.06 or higher'

Nitpick: 1.0.6

> +    exit 1
> +fi


-- 
Himanshu Jha
Undergraduate Student
Department of Electronics & Communication
Guru Tegh Bahadur Institute of Technology

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

* Re: [Cocci] [PATCH v2 08/10] scripts: Coccinelle script for namespace dependencies.
  2019-08-14  6:36     ` [Cocci] " Himanshu Jha
@ 2019-08-14  8:03       ` Matthias Maennich
  0 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-14  8:03 UTC (permalink / raw)
  To: Himanshu Jha
  Cc: linux-kernel, maco, kstewart, linux-m68k, oneukum,
	Mauro Carvalho Chehab, usb-storage, hpa, joel, sam, cocci,
	linux-arch, linux-scsi, x86, lucas.de.marchi, mingo, geert,
	stern, kernel-team, sspatil, arnd, linux-kbuild, Nicolas Palix,
	jeyu, Jonathan Cameron, tglx, michal.lkml, gregkh, linux-usb,
	Nicolas Ferre, maco, pombredanne, David S. Miller, linux-modules

On Wed, Aug 14, 2019 at 12:06:11PM +0530, Himanshu Jha wrote:
>On Tue, Aug 13, 2019 at 01:17:05PM +0100, Matthias Maennich wrote:
>> A script that uses the '<module>.ns_deps' file generated by modpost to
>> automatically add the required symbol namespace dependencies to each
>> module.
>>
>> Usage:
>> 1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
>>    DEFAULT_SYMBOL_NAMESPACE
>> 2) Run 'make' (or 'make modules') and get warnings about modules not
>>    importing that namespace.
>> 3) Run 'make nsdeps' to automatically add required import statements
>>    to said modules.
>>
>> This makes it easer for subsystem maintainers to introduce and maintain
>> symbol namespaces into their codebase.
>>
>> Co-developed-by: Martijn Coenen <maco@android.com>
>> Signed-off-by: Martijn Coenen <maco@android.com>
>> Signed-off-by: Matthias Maennich <maennich@google.com>
>> ---
>
>[]
>
>>  MAINTAINERS                                 |  5 ++
>>  Makefile                                    | 12 +++++
>>  scripts/Makefile.modpost                    |  4 +-
>>  scripts/coccinelle/misc/add_namespace.cocci | 23 +++++++++
>>  scripts/nsdeps                              | 54 +++++++++++++++++++++
>>  5 files changed, 97 insertions(+), 1 deletion(-)
>>  create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
>>  create mode 100644 scripts/nsdeps
>
>[]
>
>> +if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
>> +    echo 'spatch needs to be version 1.06 or higher'
>
>Nitpick: 1.0.6
>

Good catch! Thanks!
Actually it needs to be even 1.0.4, so I will just use
$SPATCH_REQ_VERSION from above.

>> +    exit 1
>> +fi
>
>
>-- 
>Himanshu Jha
>Undergraduate Student
>Department of Electronics & Communication
>Guru Tegh Bahadur Institute of Technology

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

* Re: [v2 08/10] scripts: Coccinelle script for namespace dependencies
  2019-08-13 12:17   ` [PATCH v2 08/10] scripts: Coccinelle script for " Matthias Maennich
                       ` (2 preceding siblings ...)
  2019-08-14  6:36     ` [Cocci] " Himanshu Jha
@ 2019-08-14 12:00     ` " Markus Elfring
  2019-08-15 13:50     ` Markus Elfring
  4 siblings, 0 replies; 105+ messages in thread
From: Markus Elfring @ 2019-08-14 12:00 UTC (permalink / raw)
  To: Matthias Maennich, Greg Kroah-Hartman, Julia Lawall,
	Martijn Coenen, Himanshu Jha, cocci, kernel-janitors
  Cc: linux-arch, linux-kbuild, ,linux-kernel, linux-m68k,
	linux-modules, linux-scsi, linux-usb, kernel-team, usb-storage,
	x86, Alan Stern, Arnd Bergmann, David S. Miller,
	Geert Uytterhoeven, Gilles Muller, H. Peter Anvin, Ingo Molnar,
	Jessica Yu, Joel Fernandes, Jonathan Cameron, Kate Stewart,
	Lucas De Marchi, Martijn Coenen, Masahiro Yamada,
	Mauro Carvalho Chehab, Michal Marek, Nicolas Ferre,
	Nicolas Palix, Oliver Neukum, Philippe Ombredanne, Sam Ravnborg,
	Sandeep Patil, Stephen Boyd, Thomas Gleixner

+# This script requires at least spatch
+# version 1.0.4.

How do you think about to avoid the duplicate specification of this identification?

Regards,
Markus

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

* Re: [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  2019-08-13 20:15     ` Saravana Kannan
@ 2019-08-14 12:54       ` Matthias Maennich
  2019-08-14 17:34         ` Saravana Kannan
  0 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-14 12:54 UTC (permalink / raw)
  To: Saravana Kannan
  Cc: LKML, maco, Android Kernel Team, arnd, geert, Greg Kroah-Hartman,
	hpa, jeyu, Joel Fernandes (Google),
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, michal.lkml, mingo,
	oneukum, pombredanne, sam, Sandeep Patil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton,
	Johannes Weiner, Peter Zijlstra (Intel),
	Mauro Carvalho Chehab, David Howells, Patrick Bellasi,
	Dan Williams, Adrian Reber, Richard Guy Briggs

On Tue, Aug 13, 2019 at 01:15:44PM -0700, Saravana Kannan wrote:
>On Tue, Aug 13, 2019 at 5:19 AM 'Matthias Maennich' via kernel-team
><kernel-team@android.com> wrote:
>>
>> If MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is enabled (default=n), the
>> requirement for modules to import all namespaces that are used by
>> the module is relaxed.
>>
>> Enabling this option effectively allows (invalid) modules to be loaded
>> while only a warning is emitted.
>>
>> Disabling this option keeps the enforcement at module loading time and
>> loading is denied if the module's imports are not satisfactory.
>>
>> Reviewed-by: Martijn Coenen <maco@android.com>
>> Signed-off-by: Matthias Maennich <maennich@google.com>
>> ---
>>  init/Kconfig    | 14 ++++++++++++++
>>  kernel/module.c | 11 +++++++++--
>>  2 files changed, 23 insertions(+), 2 deletions(-)
>>
>> diff --git a/init/Kconfig b/init/Kconfig
>> index bd7d650d4a99..b3373334cdf1 100644
>> --- a/init/Kconfig
>> +++ b/init/Kconfig
>> @@ -2119,6 +2119,20 @@ config MODULE_COMPRESS_XZ
>>
>>  endchoice
>>
>> +config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
>> +       bool "Allow loading of modules with missing namespace imports"
>> +       default n
>> +       help
>> +         Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
>> +         a namespace. A module that makes use of a symbol exported with such a
>> +         namespace is required to import the namespace via MODULE_IMPORT_NS().
>> +         This option relaxes this requirement when loading a module.
>
>> While
>> +         technically there is no reason to enforce correct namespace imports,
>> +         it creates consistency between symbols defining namespaces and users
>> +         importing namespaces they make use of.
>
>I'm confused by this sentence. It sounds like it's the opposite of
>what the config is doing? Can you please reword it for clarify?

How about:

  Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
  a namespace. A module that makes use of a symbol exported with such a
  namespace is required to import the namespace via MODULE_IMPORT_NS().
  There is no technical reason to enforce correct namespace imports,
  but it creates consistency between symbols defining namespaces and
  users importing namespaces they make use of. This option relaxes this
  requirement and lifts the enforcement when loading a module.

-- 
Cheers,
Matthias

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

* Re: [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  2019-08-14 12:54       ` Matthias Maennich
@ 2019-08-14 17:34         ` Saravana Kannan
  0 siblings, 0 replies; 105+ messages in thread
From: Saravana Kannan @ 2019-08-14 17:34 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: LKML, maco, Android Kernel Team, arnd, geert, Greg Kroah-Hartman,
	hpa, jeyu, Joel Fernandes (Google),
	Kate Stewart, linux-arch, linux-kbuild, linux-m68k,
	linux-modules, linux-scsi, linux-usb, lucas.de.marchi,
	Martijn Coenen, michal.lkml, mingo, oneukum, Philippe Ombredanne,
	sam, Sandeep Patil, stern, tglx, usb-storage, x86,
	yamada.masahiro, Andrew Morton, Johannes Weiner,
	Peter Zijlstra (Intel),
	Mauro Carvalho Chehab, David Howells, Patrick Bellasi,
	Dan Williams, Adrian Reber, Richard Guy Briggs

On Wed, Aug 14, 2019 at 5:54 AM 'Matthias Maennich' via kernel-team
<kernel-team@android.com> wrote:
>
> On Tue, Aug 13, 2019 at 01:15:44PM -0700, Saravana Kannan wrote:
> >On Tue, Aug 13, 2019 at 5:19 AM 'Matthias Maennich' via kernel-team
> ><kernel-team@android.com> wrote:
> >>
> >> If MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is enabled (default=n), the
> >> requirement for modules to import all namespaces that are used by
> >> the module is relaxed.
> >>
> >> Enabling this option effectively allows (invalid) modules to be loaded
> >> while only a warning is emitted.
> >>
> >> Disabling this option keeps the enforcement at module loading time and
> >> loading is denied if the module's imports are not satisfactory.
> >>
> >> Reviewed-by: Martijn Coenen <maco@android.com>
> >> Signed-off-by: Matthias Maennich <maennich@google.com>
> >> ---
> >>  init/Kconfig    | 14 ++++++++++++++
> >>  kernel/module.c | 11 +++++++++--
> >>  2 files changed, 23 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/init/Kconfig b/init/Kconfig
> >> index bd7d650d4a99..b3373334cdf1 100644
> >> --- a/init/Kconfig
> >> +++ b/init/Kconfig
> >> @@ -2119,6 +2119,20 @@ config MODULE_COMPRESS_XZ
> >>
> >>  endchoice
> >>
> >> +config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
> >> +       bool "Allow loading of modules with missing namespace imports"
> >> +       default n
> >> +       help
> >> +         Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
> >> +         a namespace. A module that makes use of a symbol exported with such a
> >> +         namespace is required to import the namespace via MODULE_IMPORT_NS().
> >> +         This option relaxes this requirement when loading a module.
> >
> >> While
> >> +         technically there is no reason to enforce correct namespace imports,
> >> +         it creates consistency between symbols defining namespaces and users
> >> +         importing namespaces they make use of.
> >
> >I'm confused by this sentence. It sounds like it's the opposite of
> >what the config is doing? Can you please reword it for clarify?
>
> How about:
>
>   Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
>   a namespace. A module that makes use of a symbol exported with such a
>   namespace is required to import the namespace via MODULE_IMPORT_NS().
>   There is no technical reason to enforce correct namespace imports,
>   but it creates consistency between symbols defining namespaces and
>   users importing namespaces they make use of. This option relaxes this
>   requirement and lifts the enforcement when loading a module.

That's a lot better. Especially moving the "This option relaxes..." to
the bottom. Thanks.

-Saravana

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

* Re: [v2 08/10] scripts: Coccinelle script for namespace dependencies
  2019-08-13 12:17   ` [PATCH v2 08/10] scripts: Coccinelle script for " Matthias Maennich
                       ` (3 preceding siblings ...)
  2019-08-14 12:00     ` [v2 " Markus Elfring
@ 2019-08-15 13:50     ` Markus Elfring
  2019-08-22  9:18       ` Matthias Maennich
  4 siblings, 1 reply; 105+ messages in thread
From: Markus Elfring @ 2019-08-15 13:50 UTC (permalink / raw)
  To: Matthias Maennich, Greg Kroah-Hartman, Julia Lawall,
	Martijn Coenen, cocci, kernel-janitors
  Cc: linux-arch, linux-kbuild, linux-kernel, linux-m68k,
	linux-modules, linux-scsi, linux-usb, kernel-team, usb-storage,
	x86, Alan Stern, Arnd Bergmann, David S. Miller,
	Geert Uytterhoeven, Gilles Muller, H. Peter Anvin, Ingo Molnar,
	Jessica Yu, Joel Fernandes, Jonathan Cameron, Kate Stewart,
	Lucas De Marchi, Martijn Coenen, Masahiro Yamada,
	Mauro Carvalho Chehab, Michal Marek, Nicolas Ferre,
	Nicolas Palix, Oliver Neukum, Philippe Ombredanne, Sam Ravnborg,
	Sandeep Patil, Stephen Boyd, Thomas Gleixner

> +generate_deps_for_ns() {
> +    $SPATCH --very-quiet --in-place --sp-file \
> +	    $srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
> +}

* Where will the variable “srctree” be set for the file “scripts/nsdeps”?

* Would you like to support a separate build directory for desired adjustments?

* How do you think about to check error handling around such commands?


> +generate_deps() {
> +        for source_file in $mod_source_files; do
> +            sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
…

I suggest to assign the name for the temporary file to a variable
which should be used by subsequent commands.

Regards,
Markus

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

* [PATCH v3 01/11] module: support reading multiple values per modinfo tag
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
@ 2019-08-21 11:49     ` Matthias Maennich
  2019-08-21 11:49     ` [PATCH v3 02/11] export: explicitly align struct kernel_symbol Matthias Maennich
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

Similar to modpost's get_next_modinfo(), introduce get_next_modinfo() in
kernel/module.c to acquire any further values associated with the same
modinfo tag name. That is useful for any tags that have multiple
occurrences (such as 'alias'), but is in particular introduced here as
part of the symbol namespaces patch series to read the (potentially)
multiple namespaces a module is importing.

Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 kernel/module.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index 5933395af9a0..a23067907169 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2481,7 +2481,8 @@ static char *next_string(char *string, unsigned long *secsize)
 	return string;
 }
 
-static char *get_modinfo(struct load_info *info, const char *tag)
+static char *get_next_modinfo(const struct load_info *info, const char *tag,
+			      char *prev)
 {
 	char *p;
 	unsigned int taglen = strlen(tag);
@@ -2492,13 +2493,25 @@ static char *get_modinfo(struct load_info *info, const char *tag)
 	 * get_modinfo() calls made before rewrite_section_headers()
 	 * must use sh_offset, as sh_addr isn't set!
 	 */
-	for (p = (char *)info->hdr + infosec->sh_offset; p; p = next_string(p, &size)) {
+	char *modinfo = (char *)info->hdr + infosec->sh_offset;
+
+	if (prev) {
+		size -= prev - modinfo;
+		modinfo = next_string(prev, &size);
+	}
+
+	for (p = modinfo; p; p = next_string(p, &size)) {
 		if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
 			return p + taglen + 1;
 	}
 	return NULL;
 }
 
+static char *get_modinfo(const struct load_info *info, const char *tag)
+{
+	return get_next_modinfo(info, tag, NULL);
+}
+
 static void setup_modinfo(struct module *mod, struct load_info *info)
 {
 	struct module_attribute *attr;
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 02/11] export: explicitly align struct kernel_symbol
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
  2019-08-21 11:49     ` [PATCH v3 01/11] module: support reading multiple values per modinfo tag Matthias Maennich
@ 2019-08-21 11:49     ` Matthias Maennich
  2019-08-21 11:49     ` [PATCH v3 03/11] module: add support for symbol namespaces Matthias Maennich
                       ` (7 subsequent siblings)
  9 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Ingo Molnar, Andrew Morton,
	Ard Biesheuvel

This change allows growing struct kernel_symbol without wasting bytes to
alignment. It also concretized the alignment of ksymtab entries if
relative references are used for ksymtab entries.

struct kernel_symbol was already implicitly being aligned to the word
size, except on x86_64 and m68k, where it is aligned to 16 and 2 bytes,
respectively.

As far as I can tell there is no requirement for aligning struct
kernel_symbol to 16 bytes on x86_64, but gcc aligns structs to their
size, and the linker aligns the custom __ksymtab sections to the largest
data type contained within, so setting KSYM_ALIGN to 16 was necessary to
stay consistent with the code generated for non-ASM EXPORT_SYMBOL(). Now
that non-ASM EXPORT_SYMBOL() explicitly aligns to word size (8),
KSYM_ALIGN is no longer necessary.

In case of relative references, the alignment has been changed
accordingly to not waste space when adding new struct members.

As for m68k, struct kernel_symbol is aligned to 2 bytes even though the
structure itself is 8 bytes; using a 4-byte alignment shouldn't hurt.

I manually verified the output of the __ksymtab sections didn't change
on x86, x86_64, arm, arm64 and m68k. As expected, the section contents
didn't change, and the ELF section alignment only changed on x86_64 and
m68k. Feedback from other archs more than welcome.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 arch/m68k/include/asm/export.h | 1 -
 include/asm-generic/export.h   | 8 +++-----
 include/linux/export.h         | 3 ++-
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/arch/m68k/include/asm/export.h b/arch/m68k/include/asm/export.h
index 0af20f48bd07..b53008b67ce1 100644
--- a/arch/m68k/include/asm/export.h
+++ b/arch/m68k/include/asm/export.h
@@ -1,3 +1,2 @@
-#define KSYM_ALIGN 2
 #define KCRC_ALIGN 2
 #include <asm-generic/export.h>
diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 294d6ae785d4..63f54907317b 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -4,15 +4,13 @@
 #ifndef KSYM_FUNC
 #define KSYM_FUNC(x) x
 #endif
-#ifdef CONFIG_64BIT
-#ifndef KSYM_ALIGN
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+#define KSYM_ALIGN 4
+#elif defined(CONFIG_64BIT)
 #define KSYM_ALIGN 8
-#endif
 #else
-#ifndef KSYM_ALIGN
 #define KSYM_ALIGN 4
 #endif
-#endif
 #ifndef KCRC_ALIGN
 #define KCRC_ALIGN 4
 #endif
diff --git a/include/linux/export.h b/include/linux/export.h
index fd8711ed9ac4..28a4d2150689 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -52,7 +52,7 @@ extern struct module __this_module;
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	__ADDRESSABLE(sym)						\
 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
-	    "	.balign	8					\n"	\
+	    "	.balign 4					\n"	\
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
@@ -66,6 +66,7 @@ struct kernel_symbol {
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	static const struct kernel_symbol __ksymtab_##sym		\
 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
+	__aligned(sizeof(void *))					\
 	= { (unsigned long)&sym, __kstrtab_##sym }
 
 struct kernel_symbol {
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 03/11] module: add support for symbol namespaces.
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
  2019-08-21 11:49     ` [PATCH v3 01/11] module: support reading multiple values per modinfo tag Matthias Maennich
  2019-08-21 11:49     ` [PATCH v3 02/11] export: explicitly align struct kernel_symbol Matthias Maennich
@ 2019-08-21 11:49     ` Matthias Maennich
  2019-08-27 15:37       ` Jessica Yu
  2019-08-21 11:49     ` [PATCH v3 04/11] modpost: " Matthias Maennich
                       ` (6 subsequent siblings)
  9 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Michael Ellerman,
	Andrew Morton, Ard Biesheuvel

The EXPORT_SYMBOL_NS() and EXPORT_SYMBOL_NS_GPL() macros can be used to
export a symbol to a specific namespace.  There are no _GPL_FUTURE and
_UNUSED variants because these are currently unused, and I'm not sure
they are necessary.

I didn't add EXPORT_SYMBOL_NS() for ASM exports; this patch sets the
namespace of ASM exports to NULL by default. In case of relative
references, it will be relocatable to NULL. If there's a need, this
should be pretty easy to add.

A module that wants to use a symbol exported to a namespace must add a
MODULE_IMPORT_NS() statement to their module code; otherwise, modpost
will complain when building the module, and the kernel module loader
will emit an error and fail when loading the module.

MODULE_IMPORT_NS() adds a modinfo tag 'import_ns' to the module. That
tag can be observed by the modinfo command, modpost and kernel/module.c
at the time of loading the module.

The ELF symbols are renamed to include the namespace with an asm label;
for example, symbol 'usb_stor_suspend' in namespace USB_STORAGE becomes
'usb_stor_suspend.USB_STORAGE'.  This allows modpost to do namespace
checking, without having to go through all the effort of parsing ELF and
relocation records just to get to the struct kernel_symbols.

On x86_64 I saw no difference in binary size (compression), but at
runtime this will require a word of memory per export to hold the
namespace. An alternative could be to store namespaced symbols in their
own section and use a separate 'struct namespaced_kernel_symbol' for
that section, at the cost of making the module loader more complex.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/asm-generic/export.h |  6 +--
 include/linux/export.h       | 85 ++++++++++++++++++++++++++++++------
 include/linux/module.h       |  2 +
 kernel/module.c              | 43 ++++++++++++++++++
 4 files changed, 120 insertions(+), 16 deletions(-)

diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 63f54907317b..e2b5d0f569d3 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -17,11 +17,11 @@
 
 .macro __put, val, name
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
-	.long	\val - ., \name - .
+	.long	\val - ., \name - ., 0 - .
 #elif defined(CONFIG_64BIT)
-	.quad	\val, \name
+	.quad	\val, \name, 0
 #else
-	.long	\val, \name
+	.long	\val, \name, 0
 #endif
 .endm
 
diff --git a/include/linux/export.h b/include/linux/export.h
index 28a4d2150689..8e12e05444d1 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -20,6 +20,8 @@ extern struct module __this_module;
 
 #ifdef CONFIG_MODULES
 
+#define NS_SEPARATOR "."
+
 #if defined(__KERNEL__) && !defined(__GENKSYMS__)
 #ifdef CONFIG_MODVERSIONS
 /* Mark the CRC weak since genksyms apparently decides not to
@@ -49,6 +51,16 @@ extern struct module __this_module;
  * absolute relocations that require runtime processing on relocatable
  * kernels.
  */
+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
+	__ADDRESSABLE(sym)						\
+	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
+	    "	.balign	4					\n"	\
+	    "__ksymtab_" #sym NS_SEPARATOR #ns ":		\n"	\
+	    "	.long	" #sym "- .				\n"	\
+	    "	.long	__kstrtab_" #sym "- .			\n"	\
+	    "	.long	__kstrtab_ns_" #sym "- .		\n"	\
+	    "	.previous					\n")
+
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	__ADDRESSABLE(sym)						\
 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
@@ -56,32 +68,53 @@ extern struct module __this_module;
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
+	    "	.long	0 - .					\n"	\
 	    "	.previous					\n")
 
 struct kernel_symbol {
 	int value_offset;
 	int name_offset;
+	int namespace_offset;
 };
 #else
+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
+	static const struct kernel_symbol __ksymtab_##sym##__##ns	\
+	asm("__ksymtab_" #sym NS_SEPARATOR #ns)				\
+	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
+	__aligned(sizeof(void *))					\
+	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym}
+
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	static const struct kernel_symbol __ksymtab_##sym		\
+	asm("__ksymtab_" #sym)						\
 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
 	__aligned(sizeof(void *))					\
-	= { (unsigned long)&sym, __kstrtab_##sym }
+	= { (unsigned long)&sym, __kstrtab_##sym, NULL }
 
 struct kernel_symbol {
 	unsigned long value;
 	const char *name;
+	const char *namespace;
 };
 #endif
 
-/* For every exported symbol, place a struct in the __ksymtab section */
-#define ___EXPORT_SYMBOL(sym, sec)					\
+#define ___export_symbol_common(sym, sec)				\
 	extern typeof(sym) sym;						\
 	__CRC_SYMBOL(sym, sec)						\
 	static const char __kstrtab_##sym[]				\
 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
-	= #sym;								\
+	= #sym								\
+
+/* For every exported symbol, place a struct in the __ksymtab section */
+#define ___EXPORT_SYMBOL_NS(sym, sec, ns)				\
+	___export_symbol_common(sym, sec);			\
+	static const char __kstrtab_ns_##sym[]				\
+	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
+	= #ns;								\
+	__KSYMTAB_ENTRY_NS(sym, sec, ns)
+
+#define ___EXPORT_SYMBOL(sym, sec)					\
+	___export_symbol_common(sym, sec);				\
 	__KSYMTAB_ENTRY(sym, sec)
 
 #if defined(__DISABLE_EXPORTS)
@@ -91,6 +124,7 @@ struct kernel_symbol {
  * be reused in other execution contexts such as the UEFI stub or the
  * decompressor.
  */
+#define __EXPORT_SYMBOL_NS(sym, sec, ns)
 #define __EXPORT_SYMBOL(sym, sec)
 
 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
@@ -117,18 +151,26 @@ struct kernel_symbol {
 #define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
 #define __cond_export_sym_0(sym, sec) /* nothing */
 
+#define __EXPORT_SYMBOL_NS(sym, sec, ns)				\
+	__ksym_marker(sym);						\
+	__cond_export_ns_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
+#define __cond_export_ns_sym(sym, sec, ns, conf)			\
+	___cond_export_ns_sym(sym, sec, ns, conf)
+#define ___cond_export_ns_sym(sym, sec, ns, enabled)			\
+	__cond_export_ns_sym_##enabled(sym, sec, ns)
+#define __cond_export_ns_sym_1(sym, sec, ns) ___EXPORT_SYMBOL_NS(sym, sec, ns)
+#define __cond_export_ns_sym_0(sym, sec, ns) /* nothing */
+
 #else
+#define __EXPORT_SYMBOL_NS ___EXPORT_SYMBOL_NS
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
 #endif
 
-#define EXPORT_SYMBOL(sym)					\
-	__EXPORT_SYMBOL(sym, "")
-
-#define EXPORT_SYMBOL_GPL(sym)					\
-	__EXPORT_SYMBOL(sym, "_gpl")
-
-#define EXPORT_SYMBOL_GPL_FUTURE(sym)				\
-	__EXPORT_SYMBOL(sym, "_gpl_future")
+#define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
+#define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
+#define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
+#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL_NS(sym, "", ns)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL_NS(sym, "_gpl", ns)
 
 #ifdef CONFIG_UNUSED_SYMBOLS
 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
@@ -138,11 +180,28 @@ struct kernel_symbol {
 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
 #endif
 
-#endif	/* __GENKSYMS__ */
+#endif	/* __KERNEL__ && !__GENKSYMS__ */
+
+#if defined(__GENKSYMS__)
+/*
+ * When we're running genksyms, ignore the namespace and make the _NS
+ * variants look like the normal ones. There are two reasons for this:
+ * 1) In the normal definition of EXPORT_SYMBOL_NS, the 'ns' macro
+ *    argument is itself not expanded because it's always tokenized or
+ *    concatenated; but when running genksyms, a blank definition of the
+ *    macro does allow the argument to be expanded; if a namespace
+ *    happens to collide with a #define, this can cause issues.
+ * 2) There's no need to modify genksyms to deal with the _NS variants
+ */
+#define EXPORT_SYMBOL_NS(sym, ns) EXPORT_SYMBOL(sym)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns) EXPORT_SYMBOL_GPL(sym)
+#endif
 
 #else /* !CONFIG_MODULES... */
 
 #define EXPORT_SYMBOL(sym)
+#define EXPORT_SYMBOL_NS(sym, ns)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns)
 #define EXPORT_SYMBOL_GPL(sym)
 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
 #define EXPORT_UNUSED_SYMBOL(sym)
diff --git a/include/linux/module.h b/include/linux/module.h
index 1455812dd325..b3611e749f72 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -280,6 +280,8 @@ struct notifier_block;
 
 #ifdef CONFIG_MODULES
 
+#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)
+
 extern int modules_disabled; /* for sysctl */
 /* Get/put a kernel symbol (calls must be symmetric) */
 void *__symbol_get(const char *symbol);
diff --git a/kernel/module.c b/kernel/module.c
index a23067907169..57e8253f2251 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -544,6 +544,15 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
 #endif
 }
 
+static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
+{
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	return offset_to_ptr(&sym->namespace_offset);
+#else
+	return sym->namespace;
+#endif
+}
+
 static int cmp_name(const void *va, const void *vb)
 {
 	const char *a;
@@ -1379,6 +1388,34 @@ static inline int same_magic(const char *amagic, const char *bmagic,
 }
 #endif /* CONFIG_MODVERSIONS */
 
+static char *get_modinfo(const struct load_info *info, const char *tag);
+static char *get_next_modinfo(const struct load_info *info, const char *tag,
+			      char *prev);
+
+static int verify_namespace_is_imported(const struct load_info *info,
+					const struct kernel_symbol *sym,
+					struct module *mod)
+{
+	const char *namespace;
+	char *imported_namespace;
+
+	namespace = kernel_symbol_namespace(sym);
+	if (namespace) {
+		imported_namespace = get_modinfo(info, "import_ns");
+		while (imported_namespace) {
+			if (strcmp(namespace, imported_namespace) == 0)
+				return 0;
+			imported_namespace = get_next_modinfo(
+				info, "import_ns", imported_namespace);
+		}
+		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
+		       mod->name, kernel_symbol_name(sym), namespace);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+
 /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
 static const struct kernel_symbol *resolve_symbol(struct module *mod,
 						  const struct load_info *info,
@@ -1413,6 +1450,12 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
 		goto getname;
 	}
 
+	err = verify_namespace_is_imported(info, sym, mod);
+	if (err) {
+		sym = ERR_PTR(err);
+		goto getname;
+	}
+
 getname:
 	/* We must make copy under the lock if we failed to get ref. */
 	strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 04/11] modpost: add support for symbol namespaces
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
                       ` (2 preceding siblings ...)
  2019-08-21 11:49     ` [PATCH v3 03/11] module: add support for symbol namespaces Matthias Maennich
@ 2019-08-21 11:49     ` " Matthias Maennich
  2019-08-26 16:21       ` Jessica Yu
  2019-08-21 11:49     ` [PATCH v3 05/11] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
                       ` (5 subsequent siblings)
  9 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

Add support for symbols that are exported into namespaces. For that,
extract any namespace suffix from the symbol name. In addition, emit a
warning whenever a module refers to an exported symbol without
explicitly importing the namespace that it is defined in. This patch
consistently adds the namespace suffix to symbol names exported into
Module.symvers.

Example warning emitted by modpost in case of the above violation:

 WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
 USB_STORAGE, but does not import it.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 scripts/mod/modpost.c | 91 +++++++++++++++++++++++++++++++++++++------
 scripts/mod/modpost.h |  7 ++++
 2 files changed, 87 insertions(+), 11 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index f277e116e0eb..538bb24ffee3 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -164,6 +164,7 @@ struct symbol {
 	struct module *module;
 	unsigned int crc;
 	int crc_valid;
+	const char *namespace;
 	unsigned int weak:1;
 	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
 	unsigned int kernel:1;     /* 1 if symbol is from kernel
@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
 	return NULL;
 }
 
+static bool contains_namespace(struct namespace_list *list,
+			       const char *namespace)
+{
+	struct namespace_list *ns_entry;
+
+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
+		if (strcmp(ns_entry->namespace, namespace) == 0)
+			return true;
+
+	return false;
+}
+
+static void add_namespace(struct namespace_list **list, const char *namespace)
+{
+	struct namespace_list *ns_entry;
+
+	if (!contains_namespace(*list, namespace)) {
+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
+					 strlen(namespace) + 1));
+		strcpy(ns_entry->namespace, namespace);
+		ns_entry->next = *list;
+		*list = ns_entry;
+	}
+}
+
+static bool module_imports_namespace(struct module *module,
+				     const char *namespace)
+{
+	return contains_namespace(module->imported_namespaces, namespace);
+}
+
 static const struct {
 	const char *str;
 	enum export export;
@@ -312,6 +344,22 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
 		return export_unknown;
 }
 
+static const char *sym_extract_namespace(const char **symname)
+{
+	size_t n;
+	char *dupsymname;
+
+	n = strcspn(*symname, ".");
+	if (n < strlen(*symname) - 1) {
+		dupsymname = NOFAIL(strdup(*symname));
+		dupsymname[n] = '\0';
+		*symname = dupsymname;
+		return dupsymname + n + 1;
+	}
+
+	return NULL;
+}
+
 /**
  * Add an exported symbol - it may have already been added without a
  * CRC, in this case just update the CRC
@@ -319,16 +367,18 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
 static struct symbol *sym_add_exported(const char *name, struct module *mod,
 				       enum export export)
 {
-	struct symbol *s = find_symbol(name);
+	const char *symbol_name = name;
+	const char *namespace = sym_extract_namespace(&symbol_name);
+	struct symbol *s = find_symbol(symbol_name);
 
 	if (!s) {
-		s = new_symbol(name, mod, export);
+		s = new_symbol(symbol_name, mod, export);
+		s->namespace = namespace;
 	} else {
 		if (!s->preloaded) {
-			warn("%s: '%s' exported twice. Previous export "
-			     "was in %s%s\n", mod->name, name,
-			     s->module->name,
-			     is_vmlinux(s->module->name) ?"":".ko");
+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
+			     mod->name, symbol_name, s->module->name,
+			     is_vmlinux(s->module->name) ? "" : ".ko");
 		} else {
 			/* In case Module.symvers was out of date */
 			s->module = mod;
@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
 	const char *symname;
 	char *version;
 	char *license;
+	char *namespace;
 	struct module *mod;
 	struct elf_info info = { };
 	Elf_Sym *sym;
@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
 		license = get_next_modinfo(&info, "license", license);
 	}
 
+	namespace = get_modinfo(&info, "import_ns");
+	while (namespace) {
+		add_namespace(&mod->imported_namespaces, namespace);
+		namespace = get_next_modinfo(&info, "import_ns", namespace);
+	}
+
 	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
 		symname = remove_dot(info.strtab + sym->st_name);
 
@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
 			basename++;
 		else
 			basename = mod->name;
+
+		if (exp->namespace &&
+		    !module_imports_namespace(mod, exp->namespace)) {
+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
+			     basename, exp->name, exp->namespace);
+		}
+
 		if (!mod->gpl_compatible)
 			check_for_gpl_usage(exp->export, basename, exp->name);
 		check_for_unused(exp->export, basename, exp->name);
@@ -2395,16 +2459,21 @@ static void write_dump(const char *fname)
 {
 	struct buffer buf = { };
 	struct symbol *symbol;
+	const char *namespace;
 	int n;
 
 	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
 		symbol = symbolhash[n];
 		while (symbol) {
-			if (dump_sym(symbol))
-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
-					symbol->crc, symbol->name,
-					symbol->module->name,
-					export_str(symbol->export));
+			if (dump_sym(symbol)) {
+				namespace = symbol->namespace;
+				buf_printf(&buf, "0x%08x\t%s%s%s\t%s\t%s\n",
+					   symbol->crc, symbol->name,
+					   namespace ? "." : "",
+					   namespace ? namespace : "",
+					   symbol->module->name,
+					   export_str(symbol->export));
+			}
 			symbol = symbol->next;
 		}
 	}
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 8453d6ac2f77..9626bf3e7424 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -109,6 +109,11 @@ buf_printf(struct buffer *buf, const char *fmt, ...);
 void
 buf_write(struct buffer *buf, const char *s, int len);
 
+struct namespace_list {
+	struct namespace_list *next;
+	char namespace[0];
+};
+
 struct module {
 	struct module *next;
 	const char *name;
@@ -121,6 +126,8 @@ struct module {
 	struct buffer dev_table_buf;
 	char	     srcversion[25];
 	int is_dot_o;
+	// Actual imported namespaces
+	struct namespace_list *imported_namespaces;
 };
 
 struct elf_info {
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 05/11] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
                       ` (3 preceding siblings ...)
  2019-08-21 11:49     ` [PATCH v3 04/11] modpost: " Matthias Maennich
@ 2019-08-21 11:49     ` Matthias Maennich
  2019-08-21 11:49     ` [PATCH v3 06/11] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
                       ` (4 subsequent siblings)
  9 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Andrew Morton,
	Johannes Weiner, Mauro Carvalho Chehab, David Howells,
	Patrick Bellasi, Dan Williams, Adrian Reber, Richard Guy Briggs

If MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is enabled (default=n), the
requirement for modules to import all namespaces that are used by
the module is relaxed.

Enabling this option effectively allows (invalid) modules to be loaded
while only a warning is emitted.

Disabling this option keeps the enforcement at module loading time and
loading is denied if the module's imports are not satisfactory.

Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 init/Kconfig    | 13 +++++++++++++
 kernel/module.c | 11 +++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/init/Kconfig b/init/Kconfig
index bd7d650d4a99..cc28561288a7 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -2119,6 +2119,19 @@ config MODULE_COMPRESS_XZ
 
 endchoice
 
+config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
+	bool "Allow loading of modules with missing namespace imports"
+	help
+	  Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
+	  a namespace. A module that makes use of a symbol exported with such a
+	  namespace is required to import the namespace via MODULE_IMPORT_NS().
+	  There is no technical reason to enforce correct namespace imports,
+	  but it creates consistency between symbols defining namespaces and
+	  users importing namespaces they make use of. This option relaxes this
+	  requirement and lifts the enforcement when loading a module.
+
+	  If unsure, say N.
+
 config TRIM_UNUSED_KSYMS
 	bool "Trim unused exported kernel symbols"
 	depends on MODULES && !UNUSED_SYMBOLS
diff --git a/kernel/module.c b/kernel/module.c
index 57e8253f2251..7c934aaae2d3 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -1408,9 +1408,16 @@ static int verify_namespace_is_imported(const struct load_info *info,
 			imported_namespace = get_next_modinfo(
 				info, "import_ns", imported_namespace);
 		}
-		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
-		       mod->name, kernel_symbol_name(sym), namespace);
+#ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
+		pr_warn(
+#else
+		pr_err(
+#endif
+			"%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
+			mod->name, kernel_symbol_name(sym), namespace);
+#ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
 		return -EINVAL;
+#endif
 	}
 	return 0;
 }
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 06/11] export: allow definition default namespaces in Makefiles or sources
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
                       ` (4 preceding siblings ...)
  2019-08-21 11:49     ` [PATCH v3 05/11] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
@ 2019-08-21 11:49     ` Matthias Maennich
  2019-08-28 10:49       ` Jessica Yu
  2019-08-21 11:49     ` [PATCH v3 07/11] modpost: add support for generating namespace dependencies Matthias Maennich
                       ` (3 subsequent siblings)
  9 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Michael Ellerman, Ingo Molnar,
	Ard Biesheuvel

To avoid excessive usage of EXPORT_SYMBOL_NS(sym, MY_NAMESPACE), where
MY_NAMESPACE will always be the namespace we are exporting to, allow
exporting all definitions of EXPORT_SYMBOL() and friends by defining
DEFAULT_SYMBOL_NAMESPACE.

For example, to export all symbols defined in usb-common into the
namespace USB_COMMON, add a line like this to drivers/usb/common/Makefile:

  ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON

That is equivalent to changing all EXPORT_SYMBOL(sym) definitions to
EXPORT_SYMBOL_NS(sym, USB_COMMON). Subsequently all symbol namespaces
functionality will apply.

Another way of making use of this feature is to define the namespace
within source or header files similar to how TRACE_SYSTEM defines are
used:
  #undef DEFAULT_SYMBOL_NAMESPACE
  #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON

Please note that, as opposed to TRACE_SYSTEM, DEFAULT_SYMBOL_NAMESPACE
has to be defined before including include/linux/export.h.

If DEFAULT_SYMBOL_NAMESPACE is defined, a symbol can still be exported
to another namespace by using EXPORT_SYMBOL_NS() and friends with
explicitly specifying the namespace.

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/linux/export.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/export.h b/include/linux/export.h
index 8e12e05444d1..1fb243abdbc4 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -166,6 +166,12 @@ struct kernel_symbol {
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
 #endif
 
+#ifdef DEFAULT_SYMBOL_NAMESPACE
+#undef __EXPORT_SYMBOL
+#define __EXPORT_SYMBOL(sym, sec)				\
+	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
+#endif
+
 #define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
 #define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
 #define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 07/11] modpost: add support for generating namespace dependencies
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
                       ` (5 preceding siblings ...)
  2019-08-21 11:49     ` [PATCH v3 06/11] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
@ 2019-08-21 11:49     ` Matthias Maennich
  2019-08-21 11:49     ` [PATCH v3 08/11] scripts: Coccinelle script for " Matthias Maennich
                       ` (2 subsequent siblings)
  9 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Jani Nikula, Rob Herring,
	Toru Komatsu, Alexey Gladkov

This patch adds an option to modpost to generate a <module>.ns_deps file
per module, containing the namespace dependencies for that module.

E.g. if the linked module my-module.ko would depend on the symbol
myfunc.MY_NS in the namespace MY_NS, the my-module.ns_deps file created
by modpost would contain the entry MY_NS to express the namespace
dependency of my-module imposed by using the symbol myfunc.

These files can subsequently be used by static analysis tools (like
coccinelle scripts) to address issues with missing namespace imports. A
later patch of this series will introduce such a script 'nsdeps' and a
corresponding make target to automatically add missing
MODULE_IMPORT_NS() definitions to the module's sources. For that it uses
the information provided in the generated .ns_deps files.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 .gitignore            |  1 +
 Makefile              |  2 +-
 scripts/mod/modpost.c | 54 +++++++++++++++++++++++++++++++++++++++----
 scripts/mod/modpost.h |  2 ++
 4 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2030c7a4d2f8..9ee63aa2a3fb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,6 +32,7 @@
 *.lzo
 *.mod
 *.mod.c
+*.ns_deps
 *.o
 *.o.*
 *.order
diff --git a/Makefile b/Makefile
index 9fa18613566f..a89870188c09 100644
--- a/Makefile
+++ b/Makefile
@@ -1669,7 +1669,7 @@ clean: $(clean-dirs)
 		-o -name '*.ko.*' \
 		-o -name '*.dtb' -o -name '*.dtb.S' -o -name '*.dt.yaml' \
 		-o -name '*.dwo' -o -name '*.lst' \
-		-o -name '*.su' -o -name '*.mod' \
+		-o -name '*.su' -o -name '*.mod' -o -name '*.ns_deps' \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
 		-o -name '*.lex.c' -o -name '*.tab.[ch]' \
 		-o -name '*.asn1.[ch]' \
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 538bb24ffee3..81eeec063709 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -38,6 +38,8 @@ static int sec_mismatch_count = 0;
 static int sec_mismatch_fatal = 0;
 /* ignore missing files */
 static int ignore_missing_files;
+/* write namespace dependencies */
+static int write_namespace_deps;
 
 enum export {
 	export_plain,      export_unused,     export_gpl,
@@ -2176,10 +2178,15 @@ static int check_exports(struct module *mod)
 		else
 			basename = mod->name;
 
-		if (exp->namespace &&
-		    !module_imports_namespace(mod, exp->namespace)) {
-			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
-			     basename, exp->name, exp->namespace);
+		if (exp->namespace) {
+			add_namespace(&mod->required_namespaces,
+				      exp->namespace);
+
+			if (!write_namespace_deps &&
+			    !module_imports_namespace(mod, exp->namespace)) {
+				warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
+				     basename, exp->name, exp->namespace);
+			}
 		}
 
 		if (!mod->gpl_compatible)
@@ -2481,6 +2488,31 @@ static void write_dump(const char *fname)
 	free(buf.p);
 }
 
+static void write_namespace_deps_files(void)
+{
+	struct module *mod;
+	struct namespace_list *ns;
+	struct buffer ns_deps_buf = {};
+
+	for (mod = modules; mod; mod = mod->next) {
+		char fname[PATH_MAX];
+
+		if (mod->skip)
+			continue;
+
+		ns_deps_buf.pos = 0;
+
+		for (ns = mod->required_namespaces; ns; ns = ns->next)
+			buf_printf(&ns_deps_buf, "%s\n", ns->namespace);
+
+		if (ns_deps_buf.pos == 0)
+			continue;
+
+		sprintf(fname, "%s.ns_deps", mod->name);
+		write_if_changed(&ns_deps_buf, fname);
+	}
+}
+
 struct ext_sym_list {
 	struct ext_sym_list *next;
 	const char *file;
@@ -2497,7 +2529,7 @@ int main(int argc, char **argv)
 	struct ext_sym_list *extsym_iter;
 	struct ext_sym_list *extsym_start = NULL;
 
-	while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
+	while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awEd")) != -1) {
 		switch (opt) {
 		case 'i':
 			kernel_read = optarg;
@@ -2538,6 +2570,9 @@ int main(int argc, char **argv)
 		case 'E':
 			sec_mismatch_fatal = 1;
 			break;
+		case 'd':
+			write_namespace_deps = 1;
+			break;
 		default:
 			exit(1);
 		}
@@ -2572,6 +2607,9 @@ int main(int argc, char **argv)
 
 		err |= check_modname_len(mod);
 		err |= check_exports(mod);
+		if (write_namespace_deps)
+			continue;
+
 		add_header(&buf, mod);
 		add_intree_flag(&buf, !external_module);
 		add_retpoline(&buf);
@@ -2584,6 +2622,12 @@ int main(int argc, char **argv)
 		sprintf(fname, "%s.mod.c", mod->name);
 		write_if_changed(&buf, fname);
 	}
+
+	if (write_namespace_deps) {
+		write_namespace_deps_files();
+		return 0;
+	}
+
 	if (dump_write)
 		write_dump(dump_write);
 	if (sec_mismatch_count && sec_mismatch_fatal)
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 9626bf3e7424..92a926d375d2 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -126,6 +126,8 @@ struct module {
 	struct buffer dev_table_buf;
 	char	     srcversion[25];
 	int is_dot_o;
+	// Required namespace dependencies
+	struct namespace_list *required_namespaces;
 	// Actual imported namespaces
 	struct namespace_list *imported_namespaces;
 };
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 08/11] scripts: Coccinelle script for namespace dependencies.
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
                       ` (6 preceding siblings ...)
  2019-08-21 11:49     ` [PATCH v3 07/11] modpost: add support for generating namespace dependencies Matthias Maennich
@ 2019-08-21 11:49     ` " Matthias Maennich
  2019-08-22  6:09       ` [v3 " Markus Elfring
  2019-08-29 12:13       ` [PATCH v3 " Jessica Yu
  2019-08-21 11:49     ` [PATCH v3 09/11] usb-storage: remove single-use define for debugging Matthias Maennich
  2019-08-21 11:49     ` [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
  9 siblings, 2 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Julia Lawall, Julia Lawall,
	Gilles Muller, Nicolas Palix, Mauro Carvalho Chehab,
	David S. Miller, Nicolas Ferre, cocci

A script that uses the '<module>.ns_deps' files generated by modpost to
automatically add the required symbol namespace dependencies to each
module.

Usage:
1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
   DEFAULT_SYMBOL_NAMESPACE
2) Run 'make' (or 'make modules') and get warnings about modules not
   importing that namespace.
3) Run 'make nsdeps' to automatically add required import statements
   to said modules.

This makes it easer for subsystem maintainers to introduce and maintain
symbol namespaces into their codebase.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 MAINTAINERS                                 |  5 ++
 Makefile                                    | 12 +++++
 scripts/Makefile.modpost                    |  4 +-
 scripts/coccinelle/misc/add_namespace.cocci | 23 +++++++++
 scripts/nsdeps                              | 56 +++++++++++++++++++++
 5 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
 create mode 100644 scripts/nsdeps

diff --git a/MAINTAINERS b/MAINTAINERS
index 08176d64eed5..dd5b37b49a07 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11428,6 +11428,11 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
 F:	tools/include/nolibc/
 
+NSDEPS
+M:	Matthias Maennich <maennich@google.com>
+S:	Maintained
+F:	scripts/nsdeps
+
 NTB AMD DRIVER
 M:	Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
 L:	linux-ntb@googlegroups.com
diff --git a/Makefile b/Makefile
index a89870188c09..40311f583ee1 100644
--- a/Makefile
+++ b/Makefile
@@ -1500,6 +1500,9 @@ help:
 	@echo  '  headerdep       - Detect inclusion cycles in headers'
 	@echo  '  coccicheck      - Check with Coccinelle'
 	@echo  ''
+	@echo  'Tools:'
+	@echo  '  nsdeps          - Generate missing symbol namespace dependencies'
+	@echo  ''
 	@echo  'Kernel selftest:'
 	@echo  '  kselftest       - Build and run kernel selftest (run as root)'
 	@echo  '                    Build, install, and boot kernel before'
@@ -1687,6 +1690,15 @@ quiet_cmd_tags = GEN     $@
 tags TAGS cscope gtags: FORCE
 	$(call cmd,tags)
 
+# Script to generate missing namespace dependencies
+# ---------------------------------------------------------------------------
+
+PHONY += nsdeps
+
+nsdeps:
+	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost nsdeps
+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@
+
 # Scripts to check various things for consistency
 # ---------------------------------------------------------------------------
 
diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
index 26e6574ecd08..743fe3a2e885 100644
--- a/scripts/Makefile.modpost
+++ b/scripts/Makefile.modpost
@@ -56,7 +56,8 @@ MODPOST = scripts/mod/modpost						\
 	$(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS)))	\
 	$(if $(KBUILD_EXTMOD),-o $(modulesymfile))			\
 	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)			\
-	$(if $(KBUILD_MODPOST_WARN),-w)
+	$(if $(KBUILD_MODPOST_WARN),-w)					\
+	$(if $(filter nsdeps,$(MAKECMDGOALS)),-d)
 
 ifdef MODPOST_VMLINUX
 
@@ -134,6 +135,7 @@ $(modules): %.ko :%.o %.mod.o FORCE
 
 targets += $(modules)
 
+nsdeps: __modpost
 
 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
 # ---------------------------------------------------------------------------
diff --git a/scripts/coccinelle/misc/add_namespace.cocci b/scripts/coccinelle/misc/add_namespace.cocci
new file mode 100644
index 000000000000..c832bb6445a8
--- /dev/null
+++ b/scripts/coccinelle/misc/add_namespace.cocci
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+/// Adds missing MODULE_IMPORT_NS statements to source files
+///
+/// This script is usually called from scripts/nsdeps with -D ns=<namespace> to
+/// add a missing namespace tag to a module source file.
+///
+
+@has_ns_import@
+declarer name MODULE_IMPORT_NS;
+identifier virtual.ns;
+@@
+MODULE_IMPORT_NS(ns);
+
+// Add missing imports, but only adjacent to a MODULE_LICENSE statement.
+// That ensures we are adding it only to the main module source file.
+@do_import depends on !has_ns_import@
+declarer name MODULE_LICENSE;
+expression license;
+identifier virtual.ns;
+@@
+MODULE_LICENSE(license);
++ MODULE_IMPORT_NS(ns);
diff --git a/scripts/nsdeps b/scripts/nsdeps
new file mode 100644
index 000000000000..3b5995a61e65
--- /dev/null
+++ b/scripts/nsdeps
@@ -0,0 +1,56 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Linux kernel symbol namespace import generator
+#
+# This script requires a minimum spatch version.
+SPATCH_REQ_VERSION="1.0.4"
+
+DIR="$(dirname $(readlink -f $0))/.."
+SPATCH="`which ${SPATCH:=spatch}`"
+if [ ! -x "$SPATCH" ]; then
+	echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
+	exit 1
+fi
+
+SPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh)
+SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
+SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
+
+if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
+	echo "spatch needs to be version $SPATCH_REQ_VERSION or higher"
+	exit 1
+fi
+
+generate_deps_for_ns() {
+	$SPATCH --very-quiet --in-place --sp-file \
+		$srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
+}
+
+generate_deps() {
+	local mod_name=`basename $@ .ko`
+	local mod_file=`echo $@ | sed -e 's/\.ko/\.mod/'`
+	local ns_deps_file=`echo $@ | sed -e 's/\.ko/\.ns_deps/'`
+	if [ ! -f "$ns_deps_file" ]; then return; fi
+	local mod_source_files=`cat $mod_file | sed -n 1p | sed -e 's/\.o/\.c/g'`
+	for ns in `cat $ns_deps_file`; do
+		echo "Adding namespace $ns to module $mod_name (if needed)."
+		generate_deps_for_ns $ns $mod_source_files
+		# sort the imports
+		for source_file in $mod_source_files; do
+			sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
+			offset=$(wc -l ${source_file}.tmp | awk '{print $1;}')
+			cat $source_file | grep MODULE_IMPORT_NS | sort -u >> ${source_file}.tmp
+			tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp
+			if ! diff -q ${source_file} ${source_file}.tmp; then
+				mv ${source_file}.tmp ${source_file}
+			else
+				rm ${source_file}.tmp
+			fi
+		done
+	done
+}
+
+for f in `cat $srctree/modules.order`; do
+	generate_deps $f
+done
+
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 09/11] usb-storage: remove single-use define for debugging
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
                       ` (7 preceding siblings ...)
  2019-08-21 11:49     ` [PATCH v3 08/11] scripts: Coccinelle script for " Matthias Maennich
@ 2019-08-21 11:49     ` Matthias Maennich
  2019-08-21 12:37       ` Greg KH
  2019-08-21 13:21       ` Thomas Gleixner
  2019-08-21 11:49     ` [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
  9 siblings, 2 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

USB_STORAGE was defined as "usb-storage: " and used in a single location
as argument to printk. In order to be able to use the name
'USB_STORAGE', drop the definition and use the string directly for the
printk call.

Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/usb/storage/debug.h    | 2 --
 drivers/usb/storage/scsiglue.c | 2 +-
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/usb/storage/debug.h b/drivers/usb/storage/debug.h
index 6d64f342f587..16ce06039a4d 100644
--- a/drivers/usb/storage/debug.h
+++ b/drivers/usb/storage/debug.h
@@ -29,8 +29,6 @@
 
 #include <linux/kernel.h>
 
-#define USB_STORAGE "usb-storage: "
-
 #ifdef CONFIG_USB_STORAGE_DEBUG
 void usb_stor_show_command(const struct us_data *us, struct scsi_cmnd *srb);
 void usb_stor_show_sense(const struct us_data *us, unsigned char key,
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 05b80211290d..df4de8323eff 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -379,7 +379,7 @@ static int queuecommand_lck(struct scsi_cmnd *srb,
 
 	/* check for state-transition errors */
 	if (us->srb != NULL) {
-		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
+		printk(KERN_ERR "usb-storage: Error in %s: us->srb = %p\n",
 			__func__, us->srb);
 		return SCSI_MLQUEUE_HOST_BUSY;
 	}
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace
       [not found]   ` <20190821114955.12788-1-maennich@google.com>
                       ` (8 preceding siblings ...)
  2019-08-21 11:49     ` [PATCH v3 09/11] usb-storage: remove single-use define for debugging Matthias Maennich
@ 2019-08-21 11:49     ` Matthias Maennich
  2019-08-21 12:38       ` Greg KH
  2019-08-21 23:13       ` Christoph Hellwig
  9 siblings, 2 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-21 11:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

Modules using these symbols are required to explicitly import the
namespace. This patch was generated with the following steps and serves
as a reference to use the symbol namespace feature:

 1) Define DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
 2) make  (see warnings during modpost about missing imports)
 3) make nsdeps

Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
variants can be used to explicitly specify the namespace. The advantage
of the method used here is that newly added symbols are automatically
exported and existing ones are exported without touching their
respective EXPORT_SYMBOL macro expansion.

Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/usb/storage/Makefile        | 2 ++
 drivers/usb/storage/alauda.c        | 1 +
 drivers/usb/storage/cypress_atacb.c | 1 +
 drivers/usb/storage/datafab.c       | 1 +
 drivers/usb/storage/ene_ub6250.c    | 1 +
 drivers/usb/storage/freecom.c       | 1 +
 drivers/usb/storage/isd200.c        | 1 +
 drivers/usb/storage/jumpshot.c      | 1 +
 drivers/usb/storage/karma.c         | 1 +
 drivers/usb/storage/onetouch.c      | 1 +
 drivers/usb/storage/realtek_cr.c    | 1 +
 drivers/usb/storage/sddr09.c        | 1 +
 drivers/usb/storage/sddr55.c        | 1 +
 drivers/usb/storage/shuttle_usbat.c | 1 +
 drivers/usb/storage/uas.c           | 1 +
 15 files changed, 16 insertions(+)

diff --git a/drivers/usb/storage/Makefile b/drivers/usb/storage/Makefile
index a67ddcbb4e24..46635fa4a340 100644
--- a/drivers/usb/storage/Makefile
+++ b/drivers/usb/storage/Makefile
@@ -8,6 +8,8 @@
 
 ccflags-y := -I $(srctree)/drivers/scsi
 
+ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_STORAGE
+
 obj-$(CONFIG_USB_UAS)		+= uas.o
 obj-$(CONFIG_USB_STORAGE)	+= usb-storage.o
 
diff --git a/drivers/usb/storage/alauda.c b/drivers/usb/storage/alauda.c
index 6b8edf6178df..ddab2cd3d2e7 100644
--- a/drivers/usb/storage/alauda.c
+++ b/drivers/usb/storage/alauda.c
@@ -36,6 +36,7 @@
 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * Status bytes
diff --git a/drivers/usb/storage/cypress_atacb.c b/drivers/usb/storage/cypress_atacb.c
index 4825902377eb..a6f3267bbef6 100644
--- a/drivers/usb/storage/cypress_atacb.c
+++ b/drivers/usb/storage/cypress_atacb.c
@@ -22,6 +22,7 @@
 MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
 MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
index 09353be199be..588818483f4b 100644
--- a/drivers/usb/storage/datafab.c
+++ b/drivers/usb/storage/datafab.c
@@ -54,6 +54,7 @@
 MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
 MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 struct datafab_info {
 	unsigned long   sectors;	/* total sector count */
diff --git a/drivers/usb/storage/ene_ub6250.c b/drivers/usb/storage/ene_ub6250.c
index c26129d5b943..8b1b73065421 100644
--- a/drivers/usb/storage/ene_ub6250.c
+++ b/drivers/usb/storage/ene_ub6250.c
@@ -26,6 +26,7 @@
 
 MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 MODULE_FIRMWARE(SD_INIT1_FIRMWARE);
 MODULE_FIRMWARE(SD_INIT2_FIRMWARE);
 MODULE_FIRMWARE(SD_RW_FIRMWARE);
diff --git a/drivers/usb/storage/freecom.c b/drivers/usb/storage/freecom.c
index 4f542df37a44..34e7eaff1174 100644
--- a/drivers/usb/storage/freecom.c
+++ b/drivers/usb/storage/freecom.c
@@ -29,6 +29,7 @@
 MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor");
 MODULE_AUTHOR("David Brown <usb-storage@davidb.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #ifdef CONFIG_USB_STORAGE_DEBUG
 static void pdump(struct us_data *us, void *ibuffer, int length);
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index 2b474d60b4db..c4da3fd6eff9 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -53,6 +53,7 @@
 MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
 MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int isd200_Initialization(struct us_data *us);
 
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
index 917f170c4124..229bf0c1afc9 100644
--- a/drivers/usb/storage/jumpshot.c
+++ b/drivers/usb/storage/jumpshot.c
@@ -51,6 +51,7 @@
 MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
 MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/karma.c b/drivers/usb/storage/karma.c
index 395cf8fb5870..05cec81dcd3f 100644
--- a/drivers/usb/storage/karma.c
+++ b/drivers/usb/storage/karma.c
@@ -23,6 +23,7 @@
 MODULE_DESCRIPTION("Driver for Rio Karma");
 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>, Keith Bennett <keith@mcs.st-and.ac.uk>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #define RIO_PREFIX "RIOP\x00"
 #define RIO_PREFIX_LEN 5
diff --git a/drivers/usb/storage/onetouch.c b/drivers/usb/storage/onetouch.c
index 39a5009a41a6..a989fe930e21 100644
--- a/drivers/usb/storage/onetouch.c
+++ b/drivers/usb/storage/onetouch.c
@@ -25,6 +25,7 @@
 MODULE_DESCRIPTION("Maxtor USB OneTouch hard drive button driver");
 MODULE_AUTHOR("Nick Sillik <n.sillik@temple.edu>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #define ONETOUCH_PKT_LEN        0x02
 #define ONETOUCH_BUTTON         KEY_PROG1
diff --git a/drivers/usb/storage/realtek_cr.c b/drivers/usb/storage/realtek_cr.c
index cc794e25a0b6..edbe419053d6 100644
--- a/drivers/usb/storage/realtek_cr.c
+++ b/drivers/usb/storage/realtek_cr.c
@@ -35,6 +35,7 @@
 MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
 MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int auto_delink_en = 1;
 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
diff --git a/drivers/usb/storage/sddr09.c b/drivers/usb/storage/sddr09.c
index bc9da736bdfc..51bcd4a43690 100644
--- a/drivers/usb/storage/sddr09.c
+++ b/drivers/usb/storage/sddr09.c
@@ -47,6 +47,7 @@
 MODULE_DESCRIPTION("Driver for SanDisk SDDR-09 SmartMedia reader");
 MODULE_AUTHOR("Andries Brouwer <aeb@cwi.nl>, Robert Baruch <autophile@starband.net>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int usb_stor_sddr09_dpcm_init(struct us_data *us);
 static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us);
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index b8527c55335b..ba955d65eb0e 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -29,6 +29,7 @@
 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
 MODULE_AUTHOR("Simon Munton");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 854498e1012c..54aa1392c9ca 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -48,6 +48,7 @@
 MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /* Supported device types */
 #define USBAT_DEV_HP8200	0x01
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index 047c5922618f..bf80d6f81f58 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -1219,5 +1219,6 @@ static struct usb_driver uas_driver = {
 module_usb_driver(uas_driver);
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 MODULE_AUTHOR(
 	"Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");
-- 
2.23.0.rc1.153.gdeed80330f-goog


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

* Re: [PATCH v3 09/11] usb-storage: remove single-use define for debugging
  2019-08-21 11:49     ` [PATCH v3 09/11] usb-storage: remove single-use define for debugging Matthias Maennich
@ 2019-08-21 12:37       ` Greg KH
  2019-08-21 13:21       ` Thomas Gleixner
  1 sibling, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-21 12:37 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Wed, Aug 21, 2019 at 12:49:24PM +0100, Matthias Maennich wrote:
> USB_STORAGE was defined as "usb-storage: " and used in a single location
> as argument to printk. In order to be able to use the name
> 'USB_STORAGE', drop the definition and use the string directly for the
> printk call.
> 
> Signed-off-by: Matthias Maennich <maennich@google.com>

As you know, this patch is already in the usb-next tree, and will be in
the 5.4-rc1 merge.

But, as this series will end up going through a different tree than the
usb tree, here's my reviewed-by so that it can be included with the rest
of these patches:

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-08-21 11:49     ` [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
@ 2019-08-21 12:38       ` Greg KH
  2019-08-21 14:36         ` Jessica Yu
  2019-08-21 23:13       ` Christoph Hellwig
  1 sibling, 1 reply; 105+ messages in thread
From: Greg KH @ 2019-08-21 12:38 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Wed, Aug 21, 2019 at 12:49:25PM +0100, Matthias Maennich wrote:
> Modules using these symbols are required to explicitly import the
> namespace. This patch was generated with the following steps and serves
> as a reference to use the symbol namespace feature:
> 
>  1) Define DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
>  2) make  (see warnings during modpost about missing imports)
>  3) make nsdeps
> 
> Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
> variants can be used to explicitly specify the namespace. The advantage
> of the method used here is that newly added symbols are automatically
> exported and existing ones are exported without touching their
> respective EXPORT_SYMBOL macro expansion.
> 
> Signed-off-by: Matthias Maennich <maennich@google.com>

This looks good to me.  This can be included with the rest of this
series when/if it goes through the kbuild or module tree:

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Actually, which tree will this be going through?

thanks,

greg k-h

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

* Re: [PATCH v3 09/11] usb-storage: remove single-use define for debugging
  2019-08-21 11:49     ` [PATCH v3 09/11] usb-storage: remove single-use define for debugging Matthias Maennich
  2019-08-21 12:37       ` Greg KH
@ 2019-08-21 13:21       ` Thomas Gleixner
  2019-08-21 13:32         ` Greg KH
  1 sibling, 1 reply; 105+ messages in thread
From: Thomas Gleixner @ 2019-08-21 13:21 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, usb-storage,
	x86, yamada.masahiro

On Wed, 21 Aug 2019, Matthias Maennich wrote:

> USB_STORAGE was defined as "usb-storage: " and used in a single location
> as argument to printk. In order to be able to use the name
> 'USB_STORAGE', drop the definition and use the string directly for the
> printk call.
> 
> Signed-off-by: Matthias Maennich <maennich@google.com>
> ---
>  drivers/usb/storage/debug.h    | 2 --
>  drivers/usb/storage/scsiglue.c | 2 +-
>  2 files changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/storage/debug.h b/drivers/usb/storage/debug.h
> index 6d64f342f587..16ce06039a4d 100644
> --- a/drivers/usb/storage/debug.h
> +++ b/drivers/usb/storage/debug.h
> @@ -29,8 +29,6 @@
>  
>  #include <linux/kernel.h>
>  
> -#define USB_STORAGE "usb-storage: "
> -
>  #ifdef CONFIG_USB_STORAGE_DEBUG
>  void usb_stor_show_command(const struct us_data *us, struct scsi_cmnd *srb);
>  void usb_stor_show_sense(const struct us_data *us, unsigned char key,
> diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
> index 05b80211290d..df4de8323eff 100644
> --- a/drivers/usb/storage/scsiglue.c
> +++ b/drivers/usb/storage/scsiglue.c
> @@ -379,7 +379,7 @@ static int queuecommand_lck(struct scsi_cmnd *srb,
>  
>  	/* check for state-transition errors */
>  	if (us->srb != NULL) {
> -		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
> +		printk(KERN_ERR "usb-storage: Error in %s: us->srb = %p\n",
>  			__func__, us->srb);

The proper fix for this is to use pr_fmt and convert the printk to pr_err().

Thanks,

	tglx

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

* Re: [PATCH v3 09/11] usb-storage: remove single-use define for debugging
  2019-08-21 13:21       ` Thomas Gleixner
@ 2019-08-21 13:32         ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-08-21 13:32 UTC (permalink / raw)
  To: Thomas Gleixner
  Cc: Matthias Maennich, linux-kernel, kernel-team, arnd, geert, hpa,
	jeyu, joel, kstewart, linux-arch, linux-kbuild, linux-m68k,
	linux-modules, linux-scsi, linux-usb, lucas.de.marchi, maco,
	maco, michal.lkml, mingo, oneukum, pombredanne, sam, sspatil,
	stern, usb-storage, x86, yamada.masahiro

On Wed, Aug 21, 2019 at 03:21:22PM +0200, Thomas Gleixner wrote:
> On Wed, 21 Aug 2019, Matthias Maennich wrote:
> 
> > USB_STORAGE was defined as "usb-storage: " and used in a single location
> > as argument to printk. In order to be able to use the name
> > 'USB_STORAGE', drop the definition and use the string directly for the
> > printk call.
> > 
> > Signed-off-by: Matthias Maennich <maennich@google.com>
> > ---
> >  drivers/usb/storage/debug.h    | 2 --
> >  drivers/usb/storage/scsiglue.c | 2 +-
> >  2 files changed, 1 insertion(+), 3 deletions(-)
> > 
> > diff --git a/drivers/usb/storage/debug.h b/drivers/usb/storage/debug.h
> > index 6d64f342f587..16ce06039a4d 100644
> > --- a/drivers/usb/storage/debug.h
> > +++ b/drivers/usb/storage/debug.h
> > @@ -29,8 +29,6 @@
> >  
> >  #include <linux/kernel.h>
> >  
> > -#define USB_STORAGE "usb-storage: "
> > -
> >  #ifdef CONFIG_USB_STORAGE_DEBUG
> >  void usb_stor_show_command(const struct us_data *us, struct scsi_cmnd *srb);
> >  void usb_stor_show_sense(const struct us_data *us, unsigned char key,
> > diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
> > index 05b80211290d..df4de8323eff 100644
> > --- a/drivers/usb/storage/scsiglue.c
> > +++ b/drivers/usb/storage/scsiglue.c
> > @@ -379,7 +379,7 @@ static int queuecommand_lck(struct scsi_cmnd *srb,
> >  
> >  	/* check for state-transition errors */
> >  	if (us->srb != NULL) {
> > -		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
> > +		printk(KERN_ERR "usb-storage: Error in %s: us->srb = %p\n",
> >  			__func__, us->srb);
> 
> The proper fix for this is to use pr_fmt and convert the printk to pr_err().

Yeah, that's the correct long-term fix, I think someone already sent
that in for the usb tree, where I have taken this patch already.

thanks,

greg k-h

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

* Re: [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-08-21 12:38       ` Greg KH
@ 2019-08-21 14:36         ` Jessica Yu
  0 siblings, 0 replies; 105+ messages in thread
From: Jessica Yu @ 2019-08-21 14:36 UTC (permalink / raw)
  To: Greg KH
  Cc: Matthias Maennich, linux-kernel, kernel-team, arnd, geert, hpa,
	joel, kstewart, linux-arch, linux-kbuild, linux-m68k,
	linux-modules, linux-scsi, linux-usb, lucas.de.marchi, maco,
	maco, michal.lkml, mingo, oneukum, pombredanne, sam, sspatil,
	stern, tglx, usb-storage, x86, yamada.masahiro

+++ Greg KH [21/08/19 05:38 -0700]:
>On Wed, Aug 21, 2019 at 12:49:25PM +0100, Matthias Maennich wrote:
>> Modules using these symbols are required to explicitly import the
>> namespace. This patch was generated with the following steps and serves
>> as a reference to use the symbol namespace feature:
>>
>>  1) Define DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
>>  2) make  (see warnings during modpost about missing imports)
>>  3) make nsdeps
>>
>> Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
>> variants can be used to explicitly specify the namespace. The advantage
>> of the method used here is that newly added symbols are automatically
>> exported and existing ones are exported without touching their
>> respective EXPORT_SYMBOL macro expansion.
>>
>> Signed-off-by: Matthias Maennich <maennich@google.com>
>
>This looks good to me.  This can be included with the rest of this
>series when/if it goes through the kbuild or module tree:
>
>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>
>Actually, which tree will this be going through?

I would be happy to take the patchset through the modules tree once it
collects the appropriate ack/reviewed-by's and once I get a chance to
sit down and review/test it next week :)

Thanks!

Jessica

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

* Re: [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-08-21 11:49     ` [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
  2019-08-21 12:38       ` Greg KH
@ 2019-08-21 23:13       ` Christoph Hellwig
  2019-08-22  8:32         ` Matthias Maennich
  1 sibling, 1 reply; 105+ messages in thread
From: Christoph Hellwig @ 2019-08-21 23:13 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Wed, Aug 21, 2019 at 12:49:25PM +0100, Matthias Maennich wrote:
> Modules using these symbols are required to explicitly import the
> namespace. This patch was generated with the following steps and serves
> as a reference to use the symbol namespace feature:
> 
>  1) Define DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
>  2) make  (see warnings during modpost about missing imports)
>  3) make nsdeps
> 
> Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
> variants can be used to explicitly specify the namespace. The advantage
> of the method used here is that newly added symbols are automatically
> exported and existing ones are exported without touching their
> respective EXPORT_SYMBOL macro expansion.

So what is USB_STORAGE here?  It isn't a C string, so where does it
come from?  To me using a C string would seem like the nicer interface
vs a random cpp symbol that gets injected somewhere.

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

* Re: [v3 08/11] scripts: Coccinelle script for namespace dependencies
  2019-08-21 11:49     ` [PATCH v3 08/11] scripts: Coccinelle script for " Matthias Maennich
@ 2019-08-22  6:09       ` " Markus Elfring
  2019-08-29 12:13       ` [PATCH v3 " Jessica Yu
  1 sibling, 0 replies; 105+ messages in thread
From: Markus Elfring @ 2019-08-22  6:09 UTC (permalink / raw)
  To: Matthias Maennich, Greg Kroah-Hartman, Julia Lawall,
	Martijn Coenen, Himanshu Jha, cocci, kernel-janitors
  Cc: linux-arch, linux-kbuild, linux-kernel, linux-m68k,
	linux-modules, linux-scsi, linux-usb, kernel-team, usb-storage,
	x86, Alan Stern, Arnd Bergmann, David S. Miller,
	Geert Uytterhoeven, Gilles Muller, H. Peter Anvin, Ingo Molnar,
	Jessica Yu, Joel Fernandes, Jonathan Cameron, Kate Stewart,
	Lucas De Marchi, Martijn Coenen, Masahiro Yamada,
	Mauro Carvalho Chehab, Michal Marek, Nicolas Ferre,
	Nicolas Palix, Oliver Neukum, Philippe Ombredanne, Sam Ravnborg,
	Sandeep Patil, Stephen Boyd, Thomas Gleixner

> +generate_deps_for_ns() {
> +	$SPATCH --very-quiet --in-place --sp-file \
> +		$srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
> +}

I would appreciate more constructive answers for specific software development concerns.
https://lore.kernel.org/lkml/1c4420f4-361c-7358-49d9-87d8a51f7920@web.de/
https://lkml.org/lkml/2019/8/15/515

Regards,
Markus

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

* Re: [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-08-21 23:13       ` Christoph Hellwig
@ 2019-08-22  8:32         ` Matthias Maennich
  0 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-22  8:32 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, jeyu, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Wed, Aug 21, 2019 at 04:13:29PM -0700, Christoph Hellwig wrote:
>On Wed, Aug 21, 2019 at 12:49:25PM +0100, Matthias Maennich wrote:
>> Modules using these symbols are required to explicitly import the
>> namespace. This patch was generated with the following steps and serves
>> as a reference to use the symbol namespace feature:
>>
>>  1) Define DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
>>  2) make  (see warnings during modpost about missing imports)
>>  3) make nsdeps
>>
>> Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
>> variants can be used to explicitly specify the namespace. The advantage
>> of the method used here is that newly added symbols are automatically
>> exported and existing ones are exported without touching their
>> respective EXPORT_SYMBOL macro expansion.
>
>So what is USB_STORAGE here?  It isn't a C string, so where does it
>come from?  To me using a C string would seem like the nicer interface
>vs a random cpp symbol that gets injected somewhere.

To be honest, I would also prefer an interface that uses C strings or
literals for the new EXPORT_SYMBOLS* macros:

  EXPORT_SYMBOL_NS(mysym, "USB_STORAGE");

  or

  const char USB_STORAGE_NS[] = "USB_STORAGE";
  EXPORT_SYMBOL_NS(mysym, USB_STORAGE_NS);

The DEFAULT_SYMBOL_NAMESPACE define within Makefiles would get a bit
more verbose in that case to express the literal:
  ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE="\"USB_STORAGE\""


The main reason against that, is, that in the expansion of
EXPORT_SYMBOL_NS, we define the ksymtab entry, which name is
constructed partly by the name of the namespace:

   static const struct kernel_symbol __ksymtab_##sym##__##ns  ...
                                                        ^^^^

For that we depend on a cpp symbol to construct the name. I am not sure
there is a reasonable way of getting rid of that without ending up
constructing the ksymtab entries completely in asm as it is already done
in case of PREL32_RELOCATIONS. But I am happy to be corrected.

For reference that is done in patch 03/11 of this series:
https://lore.kernel.org/lkml/20190821114955.12788-4-maennich@google.com/

Cheers,
Matthias

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

* Re: [v2 08/10] scripts: Coccinelle script for namespace dependencies
  2019-08-15 13:50     ` Markus Elfring
@ 2019-08-22  9:18       ` Matthias Maennich
  2019-08-22 11:00         ` Markus Elfring
  0 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-22  9:18 UTC (permalink / raw)
  To: Markus Elfring
  Cc: Greg Kroah-Hartman, Julia Lawall, Martijn Coenen, cocci,
	kernel-janitors, linux-arch, linux-kbuild, linux-kernel,
	linux-m68k, linux-modules, linux-scsi, linux-usb, kernel-team,
	usb-storage, x86, Alan Stern, Arnd Bergmann, David S. Miller,
	Geert Uytterhoeven, Gilles Muller, H. Peter Anvin, Ingo Molnar,
	Jessica Yu, Joel Fernandes, Jonathan Cameron, Kate Stewart,
	Lucas De Marchi, Martijn Coenen, Masahiro Yamada,
	Mauro Carvalho Chehab, Michal Marek, Nicolas Ferre,
	Nicolas Palix, Oliver Neukum, Philippe Ombredanne, Sam Ravnborg,
	Sandeep Patil, Stephen Boyd, Thomas Gleixner

On Thu, Aug 15, 2019 at 03:50:38PM +0200, Markus Elfring wrote:
>> +generate_deps_for_ns() {
>> +    $SPATCH --very-quiet --in-place --sp-file \
>> +	    $srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
>> +}
>
>* Where will the variable “srctree” be set for the file “scripts/nsdeps”?
>

$srctree is defined by kbuild in the toplevel Makefile.

>* Would you like to support a separate build directory for desired adjustments?
>

No, as the purpose of this script is to directly patch the kernel
sources where applicable.

>* How do you think about to check error handling around such commands?
>
>

spatch emits a descriptive message on error. I will add a 'set
-e' to the script so that it aborts on errors.

>> +generate_deps() {
>…
>> +        for source_file in $mod_source_files; do
>> +            sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
>…
>
>I suggest to assign the name for the temporary file to a variable
>which should be used by subsequent commands.

I somehow don't agree that this is an improvement to the code as the
variable would likely be something like ${source_file_tmp}. Sticking to
${source_file}.tmp does express the intent of a temporary file next to
the original source file and the reader of the code does not need to
reason about the value of ${source_file_tmp}.

Cheers,
Matthias

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

* Re: [v2 08/10] scripts: Coccinelle script for namespace dependencies
  2019-08-22  9:18       ` Matthias Maennich
@ 2019-08-22 11:00         ` Markus Elfring
  0 siblings, 0 replies; 105+ messages in thread
From: Markus Elfring @ 2019-08-22 11:00 UTC (permalink / raw)
  To: Matthias Maennich, cocci, kernel-janitors
  Cc: Greg Kroah-Hartman, Julia Lawall, Martijn Coenen, linux-arch,
	linux-kbuild, linux-kernel, linux-m68k, linux-modules,
	linux-scsi, linux-usb, kernel-team, usb-storage, x86, Alan Stern,
	Arnd Bergmann, David S. Miller, Geert Uytterhoeven,
	Gilles Muller, H. Peter Anvin, Ingo Molnar, Jessica Yu,
	Joel Fernandes, Jonathan Cameron, Kate Stewart, Lucas De Marchi,
	Martijn Coenen, Masahiro Yamada, Mauro Carvalho Chehab,
	Michal Marek, Nicolas Ferre, Nicolas Palix, Oliver Neukum,
	Philippe Ombredanne, Sam Ravnborg, Sandeep Patil, Stephen Boyd,
	Thomas Gleixner

> $srctree is defined by kbuild in the toplevel Makefile.

How is this variable passed to the file “scripts/nsdeps”?


>> * Would you like to support a separate build directory for desired adjustments?
>
> No, as the purpose of this script is to directly patch the kernel
> sources where applicable.

Will there occasionally be a need to provide a generated patch
(without in-place file modification)?


>> I suggest to assign the name for the temporary file to a variable
>> which should be used by subsequent commands.
>
> I somehow don't agree that this is an improvement to the code as the
> variable would likely be something like ${source_file_tmp}.

Would you dare to choose a shorter variable name?


> ${source_file}.tmp does express the intent of a temporary file next to
> the original source file and the reader of the code does not need to
> reason about the value of ${source_file_tmp}.

I would find a code variant with less suffix repetition nicer.

Regards,
Markus

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

* Re: [PATCH v3 04/11] modpost: add support for symbol namespaces
  2019-08-21 11:49     ` [PATCH v3 04/11] modpost: " Matthias Maennich
@ 2019-08-26 16:21       ` Jessica Yu
  2019-08-27 14:41         ` Matthias Maennich
  0 siblings, 1 reply; 105+ messages in thread
From: Jessica Yu @ 2019-08-26 16:21 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

+++ Matthias Maennich [21/08/19 12:49 +0100]:
>Add support for symbols that are exported into namespaces. For that,
>extract any namespace suffix from the symbol name. In addition, emit a
>warning whenever a module refers to an exported symbol without
>explicitly importing the namespace that it is defined in. This patch
>consistently adds the namespace suffix to symbol names exported into
>Module.symvers.
>
>Example warning emitted by modpost in case of the above violation:
>
> WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
> USB_STORAGE, but does not import it.
>
>Co-developed-by: Martijn Coenen <maco@android.com>
>Signed-off-by: Martijn Coenen <maco@android.com>
>Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>Signed-off-by: Matthias Maennich <maennich@google.com>
>---
> scripts/mod/modpost.c | 91 +++++++++++++++++++++++++++++++++++++------
> scripts/mod/modpost.h |  7 ++++
> 2 files changed, 87 insertions(+), 11 deletions(-)
>
>diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
>index f277e116e0eb..538bb24ffee3 100644
>--- a/scripts/mod/modpost.c
>+++ b/scripts/mod/modpost.c
>@@ -164,6 +164,7 @@ struct symbol {
> 	struct module *module;
> 	unsigned int crc;
> 	int crc_valid;
>+	const char *namespace;
> 	unsigned int weak:1;
> 	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
> 	unsigned int kernel:1;     /* 1 if symbol is from kernel
>@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
> 	return NULL;
> }
>
>+static bool contains_namespace(struct namespace_list *list,
>+			       const char *namespace)
>+{
>+	struct namespace_list *ns_entry;
>+
>+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
>+		if (strcmp(ns_entry->namespace, namespace) == 0)
>+			return true;
>+
>+	return false;
>+}
>+
>+static void add_namespace(struct namespace_list **list, const char *namespace)
>+{
>+	struct namespace_list *ns_entry;
>+
>+	if (!contains_namespace(*list, namespace)) {
>+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
>+					 strlen(namespace) + 1));
>+		strcpy(ns_entry->namespace, namespace);
>+		ns_entry->next = *list;
>+		*list = ns_entry;
>+	}
>+}
>+
>+static bool module_imports_namespace(struct module *module,
>+				     const char *namespace)
>+{
>+	return contains_namespace(module->imported_namespaces, namespace);
>+}
>+
> static const struct {
> 	const char *str;
> 	enum export export;
>@@ -312,6 +344,22 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
> 		return export_unknown;
> }
>
>+static const char *sym_extract_namespace(const char **symname)
>+{
>+	size_t n;
>+	char *dupsymname;
>+
>+	n = strcspn(*symname, ".");
>+	if (n < strlen(*symname) - 1) {
>+		dupsymname = NOFAIL(strdup(*symname));
>+		dupsymname[n] = '\0';
>+		*symname = dupsymname;
>+		return dupsymname + n + 1;
>+	}
>+
>+	return NULL;
>+}
>+
> /**
>  * Add an exported symbol - it may have already been added without a
>  * CRC, in this case just update the CRC
>@@ -319,16 +367,18 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
> static struct symbol *sym_add_exported(const char *name, struct module *mod,
> 				       enum export export)
> {
>-	struct symbol *s = find_symbol(name);
>+	const char *symbol_name = name;
>+	const char *namespace = sym_extract_namespace(&symbol_name);
>+	struct symbol *s = find_symbol(symbol_name);
>
> 	if (!s) {
>-		s = new_symbol(name, mod, export);
>+		s = new_symbol(symbol_name, mod, export);
>+		s->namespace = namespace;
> 	} else {
> 		if (!s->preloaded) {
>-			warn("%s: '%s' exported twice. Previous export "
>-			     "was in %s%s\n", mod->name, name,
>-			     s->module->name,
>-			     is_vmlinux(s->module->name) ?"":".ko");
>+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
>+			     mod->name, symbol_name, s->module->name,
>+			     is_vmlinux(s->module->name) ? "" : ".ko");
> 		} else {
> 			/* In case Module.symvers was out of date */
> 			s->module = mod;
>@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
> 	const char *symname;
> 	char *version;
> 	char *license;
>+	char *namespace;
> 	struct module *mod;
> 	struct elf_info info = { };
> 	Elf_Sym *sym;
>@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
> 		license = get_next_modinfo(&info, "license", license);
> 	}
>
>+	namespace = get_modinfo(&info, "import_ns");
>+	while (namespace) {
>+		add_namespace(&mod->imported_namespaces, namespace);
>+		namespace = get_next_modinfo(&info, "import_ns", namespace);
>+	}
>+
> 	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
> 		symname = remove_dot(info.strtab + sym->st_name);
>
>@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
> 			basename++;
> 		else
> 			basename = mod->name;
>+
>+		if (exp->namespace &&
>+		    !module_imports_namespace(mod, exp->namespace)) {
>+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
>+			     basename, exp->name, exp->namespace);
>+		}
>+
> 		if (!mod->gpl_compatible)
> 			check_for_gpl_usage(exp->export, basename, exp->name);
> 		check_for_unused(exp->export, basename, exp->name);
>@@ -2395,16 +2459,21 @@ static void write_dump(const char *fname)
> {
> 	struct buffer buf = { };
> 	struct symbol *symbol;
>+	const char *namespace;
> 	int n;
>
> 	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
> 		symbol = symbolhash[n];
> 		while (symbol) {
>-			if (dump_sym(symbol))
>-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
>-					symbol->crc, symbol->name,
>-					symbol->module->name,
>-					export_str(symbol->export));
>+			if (dump_sym(symbol)) {
>+				namespace = symbol->namespace;
>+				buf_printf(&buf, "0x%08x\t%s%s%s\t%s\t%s\n",
>+					   symbol->crc, symbol->name,
>+					   namespace ? "." : "",
>+					   namespace ? namespace : "",

I think it might be cleaner to just have namespace be a separate
field in Module.symvers, rather than appending a dot and the
namespace at the end of a symbol name. Maybe something like

    <crc> <symbol_name> <namespace> <module>

For symbols without a namespace, we could just have "", with all
fields delimited by tabs. This is just a stylistic suggestion, what do
you think?

Regardless of the chosen format, I think we need to document how
namespaces are represented in Documentation/kbuild/modules.rst, where
it describes the Module.symvers format.

Thanks!

Jessica

>+					   symbol->module->name,
>+					   export_str(symbol->export));
>+			}
> 			symbol = symbol->next;
> 		}
> 	}
>diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
>index 8453d6ac2f77..9626bf3e7424 100644
>--- a/scripts/mod/modpost.h
>+++ b/scripts/mod/modpost.h
>@@ -109,6 +109,11 @@ buf_printf(struct buffer *buf, const char *fmt, ...);
> void
> buf_write(struct buffer *buf, const char *s, int len);
>
>+struct namespace_list {
>+	struct namespace_list *next;
>+	char namespace[0];
>+};
>+
> struct module {
> 	struct module *next;
> 	const char *name;
>@@ -121,6 +126,8 @@ struct module {
> 	struct buffer dev_table_buf;
> 	char	     srcversion[25];
> 	int is_dot_o;
>+	// Actual imported namespaces
>+	struct namespace_list *imported_namespaces;
> };
>
> struct elf_info {
>-- 
>2.23.0.rc1.153.gdeed80330f-goog
>

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

* Re: [PATCH v3 04/11] modpost: add support for symbol namespaces
  2019-08-26 16:21       ` Jessica Yu
@ 2019-08-27 14:41         ` Matthias Maennich
  2019-08-28  9:43           ` Jessica Yu
  0 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-27 14:41 UTC (permalink / raw)
  To: Jessica Yu
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Mon, Aug 26, 2019 at 06:21:38PM +0200, Jessica Yu wrote:
>+++ Matthias Maennich [21/08/19 12:49 +0100]:
>>Add support for symbols that are exported into namespaces. For that,
>>extract any namespace suffix from the symbol name. In addition, emit a
>>warning whenever a module refers to an exported symbol without
>>explicitly importing the namespace that it is defined in. This patch
>>consistently adds the namespace suffix to symbol names exported into
>>Module.symvers.
>>
>>Example warning emitted by modpost in case of the above violation:
>>
>>WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
>>USB_STORAGE, but does not import it.
>>
>>Co-developed-by: Martijn Coenen <maco@android.com>
>>Signed-off-by: Martijn Coenen <maco@android.com>
>>Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
>>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>Signed-off-by: Matthias Maennich <maennich@google.com>
>>---
>>scripts/mod/modpost.c | 91 +++++++++++++++++++++++++++++++++++++------
>>scripts/mod/modpost.h |  7 ++++
>>2 files changed, 87 insertions(+), 11 deletions(-)
>>
>>diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
>>index f277e116e0eb..538bb24ffee3 100644
>>--- a/scripts/mod/modpost.c
>>+++ b/scripts/mod/modpost.c
>>@@ -164,6 +164,7 @@ struct symbol {
>>	struct module *module;
>>	unsigned int crc;
>>	int crc_valid;
>>+	const char *namespace;
>>	unsigned int weak:1;
>>	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
>>	unsigned int kernel:1;     /* 1 if symbol is from kernel
>>@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
>>	return NULL;
>>}
>>
>>+static bool contains_namespace(struct namespace_list *list,
>>+			       const char *namespace)
>>+{
>>+	struct namespace_list *ns_entry;
>>+
>>+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
>>+		if (strcmp(ns_entry->namespace, namespace) == 0)
>>+			return true;
>>+
>>+	return false;
>>+}
>>+
>>+static void add_namespace(struct namespace_list **list, const char *namespace)
>>+{
>>+	struct namespace_list *ns_entry;
>>+
>>+	if (!contains_namespace(*list, namespace)) {
>>+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
>>+					 strlen(namespace) + 1));
>>+		strcpy(ns_entry->namespace, namespace);
>>+		ns_entry->next = *list;
>>+		*list = ns_entry;
>>+	}
>>+}
>>+
>>+static bool module_imports_namespace(struct module *module,
>>+				     const char *namespace)
>>+{
>>+	return contains_namespace(module->imported_namespaces, namespace);
>>+}
>>+
>>static const struct {
>>	const char *str;
>>	enum export export;
>>@@ -312,6 +344,22 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
>>		return export_unknown;
>>}
>>
>>+static const char *sym_extract_namespace(const char **symname)
>>+{
>>+	size_t n;
>>+	char *dupsymname;
>>+
>>+	n = strcspn(*symname, ".");
>>+	if (n < strlen(*symname) - 1) {
>>+		dupsymname = NOFAIL(strdup(*symname));
>>+		dupsymname[n] = '\0';
>>+		*symname = dupsymname;
>>+		return dupsymname + n + 1;
>>+	}
>>+
>>+	return NULL;
>>+}
>>+
>>/**
>> * Add an exported symbol - it may have already been added without a
>> * CRC, in this case just update the CRC
>>@@ -319,16 +367,18 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
>>static struct symbol *sym_add_exported(const char *name, struct module *mod,
>>				       enum export export)
>>{
>>-	struct symbol *s = find_symbol(name);
>>+	const char *symbol_name = name;
>>+	const char *namespace = sym_extract_namespace(&symbol_name);
>>+	struct symbol *s = find_symbol(symbol_name);
>>
>>	if (!s) {
>>-		s = new_symbol(name, mod, export);
>>+		s = new_symbol(symbol_name, mod, export);
>>+		s->namespace = namespace;
>>	} else {
>>		if (!s->preloaded) {
>>-			warn("%s: '%s' exported twice. Previous export "
>>-			     "was in %s%s\n", mod->name, name,
>>-			     s->module->name,
>>-			     is_vmlinux(s->module->name) ?"":".ko");
>>+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
>>+			     mod->name, symbol_name, s->module->name,
>>+			     is_vmlinux(s->module->name) ? "" : ".ko");
>>		} else {
>>			/* In case Module.symvers was out of date */
>>			s->module = mod;
>>@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
>>	const char *symname;
>>	char *version;
>>	char *license;
>>+	char *namespace;
>>	struct module *mod;
>>	struct elf_info info = { };
>>	Elf_Sym *sym;
>>@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
>>		license = get_next_modinfo(&info, "license", license);
>>	}
>>
>>+	namespace = get_modinfo(&info, "import_ns");
>>+	while (namespace) {
>>+		add_namespace(&mod->imported_namespaces, namespace);
>>+		namespace = get_next_modinfo(&info, "import_ns", namespace);
>>+	}
>>+
>>	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
>>		symname = remove_dot(info.strtab + sym->st_name);
>>
>>@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
>>			basename++;
>>		else
>>			basename = mod->name;
>>+
>>+		if (exp->namespace &&
>>+		    !module_imports_namespace(mod, exp->namespace)) {
>>+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
>>+			     basename, exp->name, exp->namespace);
>>+		}
>>+
>>		if (!mod->gpl_compatible)
>>			check_for_gpl_usage(exp->export, basename, exp->name);
>>		check_for_unused(exp->export, basename, exp->name);
>>@@ -2395,16 +2459,21 @@ static void write_dump(const char *fname)
>>{
>>	struct buffer buf = { };
>>	struct symbol *symbol;
>>+	const char *namespace;
>>	int n;
>>
>>	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
>>		symbol = symbolhash[n];
>>		while (symbol) {
>>-			if (dump_sym(symbol))
>>-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
>>-					symbol->crc, symbol->name,
>>-					symbol->module->name,
>>-					export_str(symbol->export));
>>+			if (dump_sym(symbol)) {
>>+				namespace = symbol->namespace;
>>+				buf_printf(&buf, "0x%08x\t%s%s%s\t%s\t%s\n",
>>+					   symbol->crc, symbol->name,
>>+					   namespace ? "." : "",
>>+					   namespace ? namespace : "",
>
>I think it might be cleaner to just have namespace be a separate
>field in Module.symvers, rather than appending a dot and the
>namespace at the end of a symbol name. Maybe something like
>
>   <crc> <symbol_name> <namespace> <module>
>
>For symbols without a namespace, we could just have "", with all
>fields delimited by tabs. This is just a stylistic suggestion, what do
>you think?

I thought of something like that initially, but did not do it to not
break users of this file. But as I am anyway breaking users by changing
the symbol name into symbol.NS, I might as well do it as you suggested.
Since read_dump() also knew already how to extract the namespaces from
symbol.NS, it had already worked without a change to the reading code
of modpost. Are there any other consumers of Module.symvers that we
should be aware of?

>Regardless of the chosen format, I think we need to document how
>namespaces are represented in Documentation/kbuild/modules.rst, where
>it describes the Module.symvers format.

Agreed. I will update the documentation. It anyway needs an update.

Cheers,
Matthias

>>+					   symbol->module->name,
>>+					   export_str(symbol->export));
>>+			}
>>			symbol = symbol->next;
>>		}
>>	}
>>diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
>>index 8453d6ac2f77..9626bf3e7424 100644
>>--- a/scripts/mod/modpost.h
>>+++ b/scripts/mod/modpost.h
>>@@ -109,6 +109,11 @@ buf_printf(struct buffer *buf, const char *fmt, ...);
>>void
>>buf_write(struct buffer *buf, const char *s, int len);
>>
>>+struct namespace_list {
>>+	struct namespace_list *next;
>>+	char namespace[0];
>>+};
>>+
>>struct module {
>>	struct module *next;
>>	const char *name;
>>@@ -121,6 +126,8 @@ struct module {
>>	struct buffer dev_table_buf;
>>	char	     srcversion[25];
>>	int is_dot_o;
>>+	// Actual imported namespaces
>>+	struct namespace_list *imported_namespaces;
>>};
>>
>>struct elf_info {
>>-- 
>>2.23.0.rc1.153.gdeed80330f-goog
>>

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

* Re: [PATCH v3 03/11] module: add support for symbol namespaces.
  2019-08-21 11:49     ` [PATCH v3 03/11] module: add support for symbol namespaces Matthias Maennich
@ 2019-08-27 15:37       ` Jessica Yu
  2019-08-27 16:04         ` Matthias Maennich
  0 siblings, 1 reply; 105+ messages in thread
From: Jessica Yu @ 2019-08-27 15:37 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Michael Ellerman,
	Andrew Morton, Ard Biesheuvel

+++ Matthias Maennich [21/08/19 12:49 +0100]:
>The EXPORT_SYMBOL_NS() and EXPORT_SYMBOL_NS_GPL() macros can be used to
>export a symbol to a specific namespace.  There are no _GPL_FUTURE and
>_UNUSED variants because these are currently unused, and I'm not sure
>they are necessary.
>
>I didn't add EXPORT_SYMBOL_NS() for ASM exports; this patch sets the
>namespace of ASM exports to NULL by default. In case of relative
>references, it will be relocatable to NULL. If there's a need, this
>should be pretty easy to add.
>
>A module that wants to use a symbol exported to a namespace must add a
>MODULE_IMPORT_NS() statement to their module code; otherwise, modpost
>will complain when building the module, and the kernel module loader
>will emit an error and fail when loading the module.
>
>MODULE_IMPORT_NS() adds a modinfo tag 'import_ns' to the module. That
>tag can be observed by the modinfo command, modpost and kernel/module.c
>at the time of loading the module.
>
>The ELF symbols are renamed to include the namespace with an asm label;
>for example, symbol 'usb_stor_suspend' in namespace USB_STORAGE becomes
>'usb_stor_suspend.USB_STORAGE'.  This allows modpost to do namespace
>checking, without having to go through all the effort of parsing ELF and
>relocation records just to get to the struct kernel_symbols.
>
>On x86_64 I saw no difference in binary size (compression), but at
>runtime this will require a word of memory per export to hold the
>namespace. An alternative could be to store namespaced symbols in their
>own section and use a separate 'struct namespaced_kernel_symbol' for
>that section, at the cost of making the module loader more complex.
>
>Co-developed-by: Martijn Coenen <maco@android.com>
>Signed-off-by: Martijn Coenen <maco@android.com>
>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>Signed-off-by: Matthias Maennich <maennich@google.com>
>---
> include/asm-generic/export.h |  6 +--
> include/linux/export.h       | 85 ++++++++++++++++++++++++++++++------
> include/linux/module.h       |  2 +
> kernel/module.c              | 43 ++++++++++++++++++
> 4 files changed, 120 insertions(+), 16 deletions(-)
>
>diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
>index 63f54907317b..e2b5d0f569d3 100644
>--- a/include/asm-generic/export.h
>+++ b/include/asm-generic/export.h
>@@ -17,11 +17,11 @@
>
> .macro __put, val, name
> #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
>-	.long	\val - ., \name - .
>+	.long	\val - ., \name - ., 0 - .
> #elif defined(CONFIG_64BIT)
>-	.quad	\val, \name
>+	.quad	\val, \name, 0
> #else
>-	.long	\val, \name
>+	.long	\val, \name, 0
> #endif
> .endm
>
>diff --git a/include/linux/export.h b/include/linux/export.h
>index 28a4d2150689..8e12e05444d1 100644
>--- a/include/linux/export.h
>+++ b/include/linux/export.h
>@@ -20,6 +20,8 @@ extern struct module __this_module;
>
> #ifdef CONFIG_MODULES
>
>+#define NS_SEPARATOR "."
>+
> #if defined(__KERNEL__) && !defined(__GENKSYMS__)
> #ifdef CONFIG_MODVERSIONS
> /* Mark the CRC weak since genksyms apparently decides not to
>@@ -49,6 +51,16 @@ extern struct module __this_module;
>  * absolute relocations that require runtime processing on relocatable
>  * kernels.
>  */
>+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
>+	__ADDRESSABLE(sym)						\
>+	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
>+	    "	.balign	4					\n"	\
>+	    "__ksymtab_" #sym NS_SEPARATOR #ns ":		\n"	\
>+	    "	.long	" #sym "- .				\n"	\
>+	    "	.long	__kstrtab_" #sym "- .			\n"	\
>+	    "	.long	__kstrtab_ns_" #sym "- .		\n"	\
>+	    "	.previous					\n")
>+
> #define __KSYMTAB_ENTRY(sym, sec)					\
> 	__ADDRESSABLE(sym)						\
> 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
>@@ -56,32 +68,53 @@ extern struct module __this_module;
> 	    "__ksymtab_" #sym ":				\n"	\
> 	    "	.long	" #sym "- .				\n"	\
> 	    "	.long	__kstrtab_" #sym "- .			\n"	\
>+	    "	.long	0 - .					\n"	\
> 	    "	.previous					\n")
>
> struct kernel_symbol {
> 	int value_offset;
> 	int name_offset;
>+	int namespace_offset;
> };
> #else
>+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
>+	static const struct kernel_symbol __ksymtab_##sym##__##ns	\
>+	asm("__ksymtab_" #sym NS_SEPARATOR #ns)				\
>+	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
>+	__aligned(sizeof(void *))					\
>+	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym}

Style nit: missing space after __kstrtab_ns_##sym.

>+
> #define __KSYMTAB_ENTRY(sym, sec)					\
> 	static const struct kernel_symbol __ksymtab_##sym		\
>+	asm("__ksymtab_" #sym)						\
> 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
> 	__aligned(sizeof(void *))					\
>-	= { (unsigned long)&sym, __kstrtab_##sym }
>+	= { (unsigned long)&sym, __kstrtab_##sym, NULL }
>
> struct kernel_symbol {
> 	unsigned long value;
> 	const char *name;
>+	const char *namespace;
> };
> #endif
>
>-/* For every exported symbol, place a struct in the __ksymtab section */
>-#define ___EXPORT_SYMBOL(sym, sec)					\
>+#define ___export_symbol_common(sym, sec)				\
> 	extern typeof(sym) sym;						\
> 	__CRC_SYMBOL(sym, sec)						\
> 	static const char __kstrtab_##sym[]				\
> 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
>-	= #sym;								\
>+	= #sym								\

Any particular reason for this change? Not that it's important, just
noticing the inconsistent inclusion of the semicolon in some of the
macros (e.g. __CRC_SYMBOL includes it but __export_symbol_common doesn't).

>+
>+/* For every exported symbol, place a struct in the __ksymtab section */
>+#define ___EXPORT_SYMBOL_NS(sym, sec, ns)				\
>+	___export_symbol_common(sym, sec);			\
>+	static const char __kstrtab_ns_##sym[]				\
>+	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
>+	= #ns;								\
>+	__KSYMTAB_ENTRY_NS(sym, sec, ns)
>+
>+#define ___EXPORT_SYMBOL(sym, sec)					\
>+	___export_symbol_common(sym, sec);				\
> 	__KSYMTAB_ENTRY(sym, sec)
>
> #if defined(__DISABLE_EXPORTS)
>@@ -91,6 +124,7 @@ struct kernel_symbol {
>  * be reused in other execution contexts such as the UEFI stub or the
>  * decompressor.
>  */
>+#define __EXPORT_SYMBOL_NS(sym, sec, ns)
> #define __EXPORT_SYMBOL(sym, sec)
>
> #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
>@@ -117,18 +151,26 @@ struct kernel_symbol {
> #define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
> #define __cond_export_sym_0(sym, sec) /* nothing */
>
>+#define __EXPORT_SYMBOL_NS(sym, sec, ns)				\
>+	__ksym_marker(sym);						\
>+	__cond_export_ns_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
>+#define __cond_export_ns_sym(sym, sec, ns, conf)			\
>+	___cond_export_ns_sym(sym, sec, ns, conf)
>+#define ___cond_export_ns_sym(sym, sec, ns, enabled)			\
>+	__cond_export_ns_sym_##enabled(sym, sec, ns)
>+#define __cond_export_ns_sym_1(sym, sec, ns) ___EXPORT_SYMBOL_NS(sym, sec, ns)
>+#define __cond_export_ns_sym_0(sym, sec, ns) /* nothing */
>+
> #else
>+#define __EXPORT_SYMBOL_NS ___EXPORT_SYMBOL_NS
> #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
> #endif
>
>-#define EXPORT_SYMBOL(sym)					\
>-	__EXPORT_SYMBOL(sym, "")
>-
>-#define EXPORT_SYMBOL_GPL(sym)					\
>-	__EXPORT_SYMBOL(sym, "_gpl")
>-
>-#define EXPORT_SYMBOL_GPL_FUTURE(sym)				\
>-	__EXPORT_SYMBOL(sym, "_gpl_future")
>+#define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
>+#define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
>+#define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
>+#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL_NS(sym, "", ns)
>+#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL_NS(sym, "_gpl", ns)
>
> #ifdef CONFIG_UNUSED_SYMBOLS
> #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
>@@ -138,11 +180,28 @@ struct kernel_symbol {
> #define EXPORT_UNUSED_SYMBOL_GPL(sym)
> #endif
>
>-#endif	/* __GENKSYMS__ */
>+#endif	/* __KERNEL__ && !__GENKSYMS__ */
>+
>+#if defined(__GENKSYMS__)
>+/*
>+ * When we're running genksyms, ignore the namespace and make the _NS
>+ * variants look like the normal ones. There are two reasons for this:
>+ * 1) In the normal definition of EXPORT_SYMBOL_NS, the 'ns' macro
>+ *    argument is itself not expanded because it's always tokenized or
>+ *    concatenated; but when running genksyms, a blank definition of the
>+ *    macro does allow the argument to be expanded; if a namespace
>+ *    happens to collide with a #define, this can cause issues.
>+ * 2) There's no need to modify genksyms to deal with the _NS variants
>+ */
>+#define EXPORT_SYMBOL_NS(sym, ns) EXPORT_SYMBOL(sym)
>+#define EXPORT_SYMBOL_NS_GPL(sym, ns) EXPORT_SYMBOL_GPL(sym)
>+#endif
>
> #else /* !CONFIG_MODULES... */
>
> #define EXPORT_SYMBOL(sym)
>+#define EXPORT_SYMBOL_NS(sym, ns)
>+#define EXPORT_SYMBOL_NS_GPL(sym, ns)
> #define EXPORT_SYMBOL_GPL(sym)
> #define EXPORT_SYMBOL_GPL_FUTURE(sym)
> #define EXPORT_UNUSED_SYMBOL(sym)
>diff --git a/include/linux/module.h b/include/linux/module.h
>index 1455812dd325..b3611e749f72 100644
>--- a/include/linux/module.h
>+++ b/include/linux/module.h
>@@ -280,6 +280,8 @@ struct notifier_block;
>
> #ifdef CONFIG_MODULES
>
>+#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)
>+
> extern int modules_disabled; /* for sysctl */
> /* Get/put a kernel symbol (calls must be symmetric) */
> void *__symbol_get(const char *symbol);
>diff --git a/kernel/module.c b/kernel/module.c
>index a23067907169..57e8253f2251 100644
>--- a/kernel/module.c
>+++ b/kernel/module.c
>@@ -544,6 +544,15 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
> #endif
> }
>
>+static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
>+{
>+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
>+	return offset_to_ptr(&sym->namespace_offset);
>+#else
>+	return sym->namespace;
>+#endif
>+}
>+
> static int cmp_name(const void *va, const void *vb)
> {
> 	const char *a;
>@@ -1379,6 +1388,34 @@ static inline int same_magic(const char *amagic, const char *bmagic,
> }
> #endif /* CONFIG_MODVERSIONS */
>
>+static char *get_modinfo(const struct load_info *info, const char *tag);
>+static char *get_next_modinfo(const struct load_info *info, const char *tag,
>+			      char *prev);
>+
>+static int verify_namespace_is_imported(const struct load_info *info,
>+					const struct kernel_symbol *sym,
>+					struct module *mod)
>+{
>+	const char *namespace;
>+	char *imported_namespace;
>+
>+	namespace = kernel_symbol_namespace(sym);
>+	if (namespace) {
>+		imported_namespace = get_modinfo(info, "import_ns");
>+		while (imported_namespace) {
>+			if (strcmp(namespace, imported_namespace) == 0)
>+				return 0;
>+			imported_namespace = get_next_modinfo(
>+				info, "import_ns", imported_namespace);
>+		}
>+		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
>+		       mod->name, kernel_symbol_name(sym), namespace);
>+		return -EINVAL;
>+	}
>+	return 0;
>+}
>+
>+
> /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
> static const struct kernel_symbol *resolve_symbol(struct module *mod,
> 						  const struct load_info *info,
>@@ -1413,6 +1450,12 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
> 		goto getname;
> 	}
>
>+	err = verify_namespace_is_imported(info, sym, mod);
>+	if (err) {
>+		sym = ERR_PTR(err);
>+		goto getname;
>+	}

I think we should verify the namespace before taking a reference to
the owner module (just swap the verify_namespace_is_imported() and
ref_module() calls here).

Other than that, this patch looks good. Thanks!

>+
> getname:
> 	/* We must make copy under the lock if we failed to get ref. */
> 	strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
>-- 
>2.23.0.rc1.153.gdeed80330f-goog
>

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

* Re: [PATCH v3 03/11] module: add support for symbol namespaces.
  2019-08-27 15:37       ` Jessica Yu
@ 2019-08-27 16:04         ` Matthias Maennich
  0 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-27 16:04 UTC (permalink / raw)
  To: Jessica Yu
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Michael Ellerman,
	Andrew Morton, Ard Biesheuvel

On Tue, Aug 27, 2019 at 05:37:18PM +0200, Jessica Yu wrote:
>+++ Matthias Maennich [21/08/19 12:49 +0100]:
>>The EXPORT_SYMBOL_NS() and EXPORT_SYMBOL_NS_GPL() macros can be used to
>>export a symbol to a specific namespace.  There are no _GPL_FUTURE and
>>_UNUSED variants because these are currently unused, and I'm not sure
>>they are necessary.
>>
>>I didn't add EXPORT_SYMBOL_NS() for ASM exports; this patch sets the
>>namespace of ASM exports to NULL by default. In case of relative
>>references, it will be relocatable to NULL. If there's a need, this
>>should be pretty easy to add.
>>
>>A module that wants to use a symbol exported to a namespace must add a
>>MODULE_IMPORT_NS() statement to their module code; otherwise, modpost
>>will complain when building the module, and the kernel module loader
>>will emit an error and fail when loading the module.
>>
>>MODULE_IMPORT_NS() adds a modinfo tag 'import_ns' to the module. That
>>tag can be observed by the modinfo command, modpost and kernel/module.c
>>at the time of loading the module.
>>
>>The ELF symbols are renamed to include the namespace with an asm label;
>>for example, symbol 'usb_stor_suspend' in namespace USB_STORAGE becomes
>>'usb_stor_suspend.USB_STORAGE'.  This allows modpost to do namespace
>>checking, without having to go through all the effort of parsing ELF and
>>relocation records just to get to the struct kernel_symbols.
>>
>>On x86_64 I saw no difference in binary size (compression), but at
>>runtime this will require a word of memory per export to hold the
>>namespace. An alternative could be to store namespaced symbols in their
>>own section and use a separate 'struct namespaced_kernel_symbol' for
>>that section, at the cost of making the module loader more complex.
>>
>>Co-developed-by: Martijn Coenen <maco@android.com>
>>Signed-off-by: Martijn Coenen <maco@android.com>
>>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>Signed-off-by: Matthias Maennich <maennich@google.com>
>>---
>>include/asm-generic/export.h |  6 +--
>>include/linux/export.h       | 85 ++++++++++++++++++++++++++++++------
>>include/linux/module.h       |  2 +
>>kernel/module.c              | 43 ++++++++++++++++++
>>4 files changed, 120 insertions(+), 16 deletions(-)
>>
>>diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
>>index 63f54907317b..e2b5d0f569d3 100644
>>--- a/include/asm-generic/export.h
>>+++ b/include/asm-generic/export.h
>>@@ -17,11 +17,11 @@
>>
>>.macro __put, val, name
>>#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
>>-	.long	\val - ., \name - .
>>+	.long	\val - ., \name - ., 0 - .
>>#elif defined(CONFIG_64BIT)
>>-	.quad	\val, \name
>>+	.quad	\val, \name, 0
>>#else
>>-	.long	\val, \name
>>+	.long	\val, \name, 0
>>#endif
>>.endm
>>
>>diff --git a/include/linux/export.h b/include/linux/export.h
>>index 28a4d2150689..8e12e05444d1 100644
>>--- a/include/linux/export.h
>>+++ b/include/linux/export.h
>>@@ -20,6 +20,8 @@ extern struct module __this_module;
>>
>>#ifdef CONFIG_MODULES
>>
>>+#define NS_SEPARATOR "."
>>+
>>#if defined(__KERNEL__) && !defined(__GENKSYMS__)
>>#ifdef CONFIG_MODVERSIONS
>>/* Mark the CRC weak since genksyms apparently decides not to
>>@@ -49,6 +51,16 @@ extern struct module __this_module;
>> * absolute relocations that require runtime processing on relocatable
>> * kernels.
>> */
>>+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
>>+	__ADDRESSABLE(sym)						\
>>+	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
>>+	    "	.balign	4					\n"	\
>>+	    "__ksymtab_" #sym NS_SEPARATOR #ns ":		\n"	\
>>+	    "	.long	" #sym "- .				\n"	\
>>+	    "	.long	__kstrtab_" #sym "- .			\n"	\
>>+	    "	.long	__kstrtab_ns_" #sym "- .		\n"	\
>>+	    "	.previous					\n")
>>+
>>#define __KSYMTAB_ENTRY(sym, sec)					\
>>	__ADDRESSABLE(sym)						\
>>	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
>>@@ -56,32 +68,53 @@ extern struct module __this_module;
>>	    "__ksymtab_" #sym ":				\n"	\
>>	    "	.long	" #sym "- .				\n"	\
>>	    "	.long	__kstrtab_" #sym "- .			\n"	\
>>+	    "	.long	0 - .					\n"	\
>>	    "	.previous					\n")
>>
>>struct kernel_symbol {
>>	int value_offset;
>>	int name_offset;
>>+	int namespace_offset;
>>};
>>#else
>>+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
>>+	static const struct kernel_symbol __ksymtab_##sym##__##ns	\
>>+	asm("__ksymtab_" #sym NS_SEPARATOR #ns)				\
>>+	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
>>+	__aligned(sizeof(void *))					\
>>+	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym}
>
>Style nit: missing space after __kstrtab_ns_##sym.
>
>>+
>>#define __KSYMTAB_ENTRY(sym, sec)					\
>>	static const struct kernel_symbol __ksymtab_##sym		\
>>+	asm("__ksymtab_" #sym)						\
>>	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
>>	__aligned(sizeof(void *))					\
>>-	= { (unsigned long)&sym, __kstrtab_##sym }
>>+	= { (unsigned long)&sym, __kstrtab_##sym, NULL }
>>
>>struct kernel_symbol {
>>	unsigned long value;
>>	const char *name;
>>+	const char *namespace;
>>};
>>#endif
>>
>>-/* For every exported symbol, place a struct in the __ksymtab section */
>>-#define ___EXPORT_SYMBOL(sym, sec)					\
>>+#define ___export_symbol_common(sym, sec)				\
>>	extern typeof(sym) sym;						\
>>	__CRC_SYMBOL(sym, sec)						\
>>	static const char __kstrtab_##sym[]				\
>>	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
>>-	= #sym;								\
>>+	= #sym								\
>
>Any particular reason for this change? Not that it's important, just
>noticing the inconsistent inclusion of the semicolon in some of the
>macros (e.g. __CRC_SYMBOL includes it but __export_symbol_common doesn't).
>

I tried to be consistent to let the macro "call site" provide the final
semicolon. And you are right, I could have adjusted __CRC_SYMBOL as
well. I will adjust this for the next version.

>>+
>>+/* For every exported symbol, place a struct in the __ksymtab section */
>>+#define ___EXPORT_SYMBOL_NS(sym, sec, ns)				\
>>+	___export_symbol_common(sym, sec);			\
>>+	static const char __kstrtab_ns_##sym[]				\
>>+	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
>>+	= #ns;								\
>>+	__KSYMTAB_ENTRY_NS(sym, sec, ns)
>>+
>>+#define ___EXPORT_SYMBOL(sym, sec)					\
>>+	___export_symbol_common(sym, sec);				\
>>	__KSYMTAB_ENTRY(sym, sec)
>>
>>#if defined(__DISABLE_EXPORTS)
>>@@ -91,6 +124,7 @@ struct kernel_symbol {
>> * be reused in other execution contexts such as the UEFI stub or the
>> * decompressor.
>> */
>>+#define __EXPORT_SYMBOL_NS(sym, sec, ns)
>>#define __EXPORT_SYMBOL(sym, sec)
>>
>>#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
>>@@ -117,18 +151,26 @@ struct kernel_symbol {
>>#define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
>>#define __cond_export_sym_0(sym, sec) /* nothing */
>>
>>+#define __EXPORT_SYMBOL_NS(sym, sec, ns)				\
>>+	__ksym_marker(sym);						\
>>+	__cond_export_ns_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
>>+#define __cond_export_ns_sym(sym, sec, ns, conf)			\
>>+	___cond_export_ns_sym(sym, sec, ns, conf)
>>+#define ___cond_export_ns_sym(sym, sec, ns, enabled)			\
>>+	__cond_export_ns_sym_##enabled(sym, sec, ns)
>>+#define __cond_export_ns_sym_1(sym, sec, ns) ___EXPORT_SYMBOL_NS(sym, sec, ns)
>>+#define __cond_export_ns_sym_0(sym, sec, ns) /* nothing */
>>+
>>#else
>>+#define __EXPORT_SYMBOL_NS ___EXPORT_SYMBOL_NS
>>#define __EXPORT_SYMBOL ___EXPORT_SYMBOL
>>#endif
>>
>>-#define EXPORT_SYMBOL(sym)					\
>>-	__EXPORT_SYMBOL(sym, "")
>>-
>>-#define EXPORT_SYMBOL_GPL(sym)					\
>>-	__EXPORT_SYMBOL(sym, "_gpl")
>>-
>>-#define EXPORT_SYMBOL_GPL_FUTURE(sym)				\
>>-	__EXPORT_SYMBOL(sym, "_gpl_future")
>>+#define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
>>+#define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
>>+#define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
>>+#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL_NS(sym, "", ns)
>>+#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL_NS(sym, "_gpl", ns)
>>
>>#ifdef CONFIG_UNUSED_SYMBOLS
>>#define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
>>@@ -138,11 +180,28 @@ struct kernel_symbol {
>>#define EXPORT_UNUSED_SYMBOL_GPL(sym)
>>#endif
>>
>>-#endif	/* __GENKSYMS__ */
>>+#endif	/* __KERNEL__ && !__GENKSYMS__ */
>>+
>>+#if defined(__GENKSYMS__)
>>+/*
>>+ * When we're running genksyms, ignore the namespace and make the _NS
>>+ * variants look like the normal ones. There are two reasons for this:
>>+ * 1) In the normal definition of EXPORT_SYMBOL_NS, the 'ns' macro
>>+ *    argument is itself not expanded because it's always tokenized or
>>+ *    concatenated; but when running genksyms, a blank definition of the
>>+ *    macro does allow the argument to be expanded; if a namespace
>>+ *    happens to collide with a #define, this can cause issues.
>>+ * 2) There's no need to modify genksyms to deal with the _NS variants
>>+ */
>>+#define EXPORT_SYMBOL_NS(sym, ns) EXPORT_SYMBOL(sym)
>>+#define EXPORT_SYMBOL_NS_GPL(sym, ns) EXPORT_SYMBOL_GPL(sym)
>>+#endif
>>
>>#else /* !CONFIG_MODULES... */
>>
>>#define EXPORT_SYMBOL(sym)
>>+#define EXPORT_SYMBOL_NS(sym, ns)
>>+#define EXPORT_SYMBOL_NS_GPL(sym, ns)
>>#define EXPORT_SYMBOL_GPL(sym)
>>#define EXPORT_SYMBOL_GPL_FUTURE(sym)
>>#define EXPORT_UNUSED_SYMBOL(sym)
>>diff --git a/include/linux/module.h b/include/linux/module.h
>>index 1455812dd325..b3611e749f72 100644
>>--- a/include/linux/module.h
>>+++ b/include/linux/module.h
>>@@ -280,6 +280,8 @@ struct notifier_block;
>>
>>#ifdef CONFIG_MODULES
>>
>>+#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)
>>+
>>extern int modules_disabled; /* for sysctl */
>>/* Get/put a kernel symbol (calls must be symmetric) */
>>void *__symbol_get(const char *symbol);
>>diff --git a/kernel/module.c b/kernel/module.c
>>index a23067907169..57e8253f2251 100644
>>--- a/kernel/module.c
>>+++ b/kernel/module.c
>>@@ -544,6 +544,15 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
>>#endif
>>}
>>
>>+static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
>>+{
>>+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
>>+	return offset_to_ptr(&sym->namespace_offset);
>>+#else
>>+	return sym->namespace;
>>+#endif
>>+}
>>+
>>static int cmp_name(const void *va, const void *vb)
>>{
>>	const char *a;
>>@@ -1379,6 +1388,34 @@ static inline int same_magic(const char *amagic, const char *bmagic,
>>}
>>#endif /* CONFIG_MODVERSIONS */
>>
>>+static char *get_modinfo(const struct load_info *info, const char *tag);
>>+static char *get_next_modinfo(const struct load_info *info, const char *tag,
>>+			      char *prev);
>>+
>>+static int verify_namespace_is_imported(const struct load_info *info,
>>+					const struct kernel_symbol *sym,
>>+					struct module *mod)
>>+{
>>+	const char *namespace;
>>+	char *imported_namespace;
>>+
>>+	namespace = kernel_symbol_namespace(sym);
>>+	if (namespace) {
>>+		imported_namespace = get_modinfo(info, "import_ns");
>>+		while (imported_namespace) {
>>+			if (strcmp(namespace, imported_namespace) == 0)
>>+				return 0;
>>+			imported_namespace = get_next_modinfo(
>>+				info, "import_ns", imported_namespace);
>>+		}
>>+		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
>>+		       mod->name, kernel_symbol_name(sym), namespace);
>>+		return -EINVAL;
>>+	}
>>+	return 0;
>>+}
>>+
>>+
>>/* Resolve a symbol for this module.  I.e. if we find one, record usage. */
>>static const struct kernel_symbol *resolve_symbol(struct module *mod,
>>						  const struct load_info *info,
>>@@ -1413,6 +1450,12 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
>>		goto getname;
>>	}
>>
>>+	err = verify_namespace_is_imported(info, sym, mod);
>>+	if (err) {
>>+		sym = ERR_PTR(err);
>>+		goto getname;
>>+	}
>
>I think we should verify the namespace before taking a reference to
>the owner module (just swap the verify_namespace_is_imported() and
>ref_module() calls here).
>
>Other than that, this patch looks good. Thanks!
>

Thanks! I will address the points above.

Cheers,
Matthias

>>+
>>getname:
>>	/* We must make copy under the lock if we failed to get ref. */
>>	strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
>>-- 
>>2.23.0.rc1.153.gdeed80330f-goog
>>

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

* Re: [PATCH v3 04/11] modpost: add support for symbol namespaces
  2019-08-27 14:41         ` Matthias Maennich
@ 2019-08-28  9:43           ` Jessica Yu
  2019-08-28  9:55             ` Matthias Maennich
  0 siblings, 1 reply; 105+ messages in thread
From: Jessica Yu @ 2019-08-28  9:43 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

+++ Matthias Maennich [27/08/19 15:41 +0100]:
>On Mon, Aug 26, 2019 at 06:21:38PM +0200, Jessica Yu wrote:
>>+++ Matthias Maennich [21/08/19 12:49 +0100]:
>>>Add support for symbols that are exported into namespaces. For that,
>>>extract any namespace suffix from the symbol name. In addition, emit a
>>>warning whenever a module refers to an exported symbol without
>>>explicitly importing the namespace that it is defined in. This patch
>>>consistently adds the namespace suffix to symbol names exported into
>>>Module.symvers.
>>>
>>>Example warning emitted by modpost in case of the above violation:
>>>
>>>WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
>>>USB_STORAGE, but does not import it.
>>>
>>>Co-developed-by: Martijn Coenen <maco@android.com>
>>>Signed-off-by: Martijn Coenen <maco@android.com>
>>>Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
>>>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>>Signed-off-by: Matthias Maennich <maennich@google.com>
>>>---
>>>scripts/mod/modpost.c | 91 +++++++++++++++++++++++++++++++++++++------
>>>scripts/mod/modpost.h |  7 ++++
>>>2 files changed, 87 insertions(+), 11 deletions(-)
>>>
>>>diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
>>>index f277e116e0eb..538bb24ffee3 100644
>>>--- a/scripts/mod/modpost.c
>>>+++ b/scripts/mod/modpost.c
>>>@@ -164,6 +164,7 @@ struct symbol {
>>>	struct module *module;
>>>	unsigned int crc;
>>>	int crc_valid;
>>>+	const char *namespace;
>>>	unsigned int weak:1;
>>>	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
>>>	unsigned int kernel:1;     /* 1 if symbol is from kernel
>>>@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
>>>	return NULL;
>>>}
>>>
>>>+static bool contains_namespace(struct namespace_list *list,
>>>+			       const char *namespace)
>>>+{
>>>+	struct namespace_list *ns_entry;
>>>+
>>>+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
>>>+		if (strcmp(ns_entry->namespace, namespace) == 0)
>>>+			return true;
>>>+
>>>+	return false;
>>>+}
>>>+
>>>+static void add_namespace(struct namespace_list **list, const char *namespace)
>>>+{
>>>+	struct namespace_list *ns_entry;
>>>+
>>>+	if (!contains_namespace(*list, namespace)) {
>>>+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
>>>+					 strlen(namespace) + 1));
>>>+		strcpy(ns_entry->namespace, namespace);
>>>+		ns_entry->next = *list;
>>>+		*list = ns_entry;
>>>+	}
>>>+}
>>>+
>>>+static bool module_imports_namespace(struct module *module,
>>>+				     const char *namespace)
>>>+{
>>>+	return contains_namespace(module->imported_namespaces, namespace);
>>>+}
>>>+
>>>static const struct {
>>>	const char *str;
>>>	enum export export;
>>>@@ -312,6 +344,22 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
>>>		return export_unknown;
>>>}
>>>
>>>+static const char *sym_extract_namespace(const char **symname)
>>>+{
>>>+	size_t n;
>>>+	char *dupsymname;
>>>+
>>>+	n = strcspn(*symname, ".");
>>>+	if (n < strlen(*symname) - 1) {
>>>+		dupsymname = NOFAIL(strdup(*symname));
>>>+		dupsymname[n] = '\0';
>>>+		*symname = dupsymname;
>>>+		return dupsymname + n + 1;
>>>+	}
>>>+
>>>+	return NULL;
>>>+}
>>>+
>>>/**
>>>* Add an exported symbol - it may have already been added without a
>>>* CRC, in this case just update the CRC
>>>@@ -319,16 +367,18 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
>>>static struct symbol *sym_add_exported(const char *name, struct module *mod,
>>>				       enum export export)
>>>{
>>>-	struct symbol *s = find_symbol(name);
>>>+	const char *symbol_name = name;
>>>+	const char *namespace = sym_extract_namespace(&symbol_name);
>>>+	struct symbol *s = find_symbol(symbol_name);
>>>
>>>	if (!s) {
>>>-		s = new_symbol(name, mod, export);
>>>+		s = new_symbol(symbol_name, mod, export);
>>>+		s->namespace = namespace;
>>>	} else {
>>>		if (!s->preloaded) {
>>>-			warn("%s: '%s' exported twice. Previous export "
>>>-			     "was in %s%s\n", mod->name, name,
>>>-			     s->module->name,
>>>-			     is_vmlinux(s->module->name) ?"":".ko");
>>>+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
>>>+			     mod->name, symbol_name, s->module->name,
>>>+			     is_vmlinux(s->module->name) ? "" : ".ko");
>>>		} else {
>>>			/* In case Module.symvers was out of date */
>>>			s->module = mod;
>>>@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
>>>	const char *symname;
>>>	char *version;
>>>	char *license;
>>>+	char *namespace;
>>>	struct module *mod;
>>>	struct elf_info info = { };
>>>	Elf_Sym *sym;
>>>@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
>>>		license = get_next_modinfo(&info, "license", license);
>>>	}
>>>
>>>+	namespace = get_modinfo(&info, "import_ns");
>>>+	while (namespace) {
>>>+		add_namespace(&mod->imported_namespaces, namespace);
>>>+		namespace = get_next_modinfo(&info, "import_ns", namespace);
>>>+	}
>>>+
>>>	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
>>>		symname = remove_dot(info.strtab + sym->st_name);
>>>
>>>@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
>>>			basename++;
>>>		else
>>>			basename = mod->name;
>>>+
>>>+		if (exp->namespace &&
>>>+		    !module_imports_namespace(mod, exp->namespace)) {
>>>+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
>>>+			     basename, exp->name, exp->namespace);
>>>+		}
>>>+
>>>		if (!mod->gpl_compatible)
>>>			check_for_gpl_usage(exp->export, basename, exp->name);
>>>		check_for_unused(exp->export, basename, exp->name);
>>>@@ -2395,16 +2459,21 @@ static void write_dump(const char *fname)
>>>{
>>>	struct buffer buf = { };
>>>	struct symbol *symbol;
>>>+	const char *namespace;
>>>	int n;
>>>
>>>	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
>>>		symbol = symbolhash[n];
>>>		while (symbol) {
>>>-			if (dump_sym(symbol))
>>>-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
>>>-					symbol->crc, symbol->name,
>>>-					symbol->module->name,
>>>-					export_str(symbol->export));
>>>+			if (dump_sym(symbol)) {
>>>+				namespace = symbol->namespace;
>>>+				buf_printf(&buf, "0x%08x\t%s%s%s\t%s\t%s\n",
>>>+					   symbol->crc, symbol->name,
>>>+					   namespace ? "." : "",
>>>+					   namespace ? namespace : "",
>>
>>I think it might be cleaner to just have namespace be a separate
>>field in Module.symvers, rather than appending a dot and the
>>namespace at the end of a symbol name. Maybe something like
>>
>>  <crc> <symbol_name> <namespace> <module>
>>
>>For symbols without a namespace, we could just have "", with all
>>fields delimited by tabs. This is just a stylistic suggestion, what do
>>you think?
>
>I thought of something like that initially, but did not do it to not
>break users of this file. But as I am anyway breaking users by changing
>the symbol name into symbol.NS, I might as well do it as you suggested.
>Since read_dump() also knew already how to extract the namespaces from
>symbol.NS, it had already worked without a change to the reading code
>of modpost. Are there any other consumers of Module.symvers that we
>should be aware of?

Maybe we can ease any possible breakage caused by the new format by
putting the namespace column last? Then the first 4 fields crc,
symbol, module, export type will remain in the same order, with
namespace last. In-tree, I think we would need to update
scripts/export_report.pl.

kmod is likely to be affected too - Lucas, would the addition of a new
field (in the order described above) in Module.symvers break any kmod tools?

Thanks,

Jessica


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

* Re: [PATCH v3 04/11] modpost: add support for symbol namespaces
  2019-08-28  9:43           ` Jessica Yu
@ 2019-08-28  9:55             ` Matthias Maennich
  2019-08-28 10:16               ` Jessica Yu
  0 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-08-28  9:55 UTC (permalink / raw)
  To: Jessica Yu
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

On Wed, Aug 28, 2019 at 11:43:26AM +0200, Jessica Yu wrote:
>+++ Matthias Maennich [27/08/19 15:41 +0100]:
>>On Mon, Aug 26, 2019 at 06:21:38PM +0200, Jessica Yu wrote:
>>>+++ Matthias Maennich [21/08/19 12:49 +0100]:
>>>>Add support for symbols that are exported into namespaces. For that,
>>>>extract any namespace suffix from the symbol name. In addition, emit a
>>>>warning whenever a module refers to an exported symbol without
>>>>explicitly importing the namespace that it is defined in. This patch
>>>>consistently adds the namespace suffix to symbol names exported into
>>>>Module.symvers.
>>>>
>>>>Example warning emitted by modpost in case of the above violation:
>>>>
>>>>WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
>>>>USB_STORAGE, but does not import it.
>>>>
>>>>Co-developed-by: Martijn Coenen <maco@android.com>
>>>>Signed-off-by: Martijn Coenen <maco@android.com>
>>>>Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
>>>>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>>>Signed-off-by: Matthias Maennich <maennich@google.com>
>>>>---
>>>>scripts/mod/modpost.c | 91 +++++++++++++++++++++++++++++++++++++------
>>>>scripts/mod/modpost.h |  7 ++++
>>>>2 files changed, 87 insertions(+), 11 deletions(-)
>>>>
>>>>diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
>>>>index f277e116e0eb..538bb24ffee3 100644
>>>>--- a/scripts/mod/modpost.c
>>>>+++ b/scripts/mod/modpost.c
>>>>@@ -164,6 +164,7 @@ struct symbol {
>>>>	struct module *module;
>>>>	unsigned int crc;
>>>>	int crc_valid;
>>>>+	const char *namespace;
>>>>	unsigned int weak:1;
>>>>	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
>>>>	unsigned int kernel:1;     /* 1 if symbol is from kernel
>>>>@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
>>>>	return NULL;
>>>>}
>>>>
>>>>+static bool contains_namespace(struct namespace_list *list,
>>>>+			       const char *namespace)
>>>>+{
>>>>+	struct namespace_list *ns_entry;
>>>>+
>>>>+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
>>>>+		if (strcmp(ns_entry->namespace, namespace) == 0)
>>>>+			return true;
>>>>+
>>>>+	return false;
>>>>+}
>>>>+
>>>>+static void add_namespace(struct namespace_list **list, const char *namespace)
>>>>+{
>>>>+	struct namespace_list *ns_entry;
>>>>+
>>>>+	if (!contains_namespace(*list, namespace)) {
>>>>+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
>>>>+					 strlen(namespace) + 1));
>>>>+		strcpy(ns_entry->namespace, namespace);
>>>>+		ns_entry->next = *list;
>>>>+		*list = ns_entry;
>>>>+	}
>>>>+}
>>>>+
>>>>+static bool module_imports_namespace(struct module *module,
>>>>+				     const char *namespace)
>>>>+{
>>>>+	return contains_namespace(module->imported_namespaces, namespace);
>>>>+}
>>>>+
>>>>static const struct {
>>>>	const char *str;
>>>>	enum export export;
>>>>@@ -312,6 +344,22 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
>>>>		return export_unknown;
>>>>}
>>>>
>>>>+static const char *sym_extract_namespace(const char **symname)
>>>>+{
>>>>+	size_t n;
>>>>+	char *dupsymname;
>>>>+
>>>>+	n = strcspn(*symname, ".");
>>>>+	if (n < strlen(*symname) - 1) {
>>>>+		dupsymname = NOFAIL(strdup(*symname));
>>>>+		dupsymname[n] = '\0';
>>>>+		*symname = dupsymname;
>>>>+		return dupsymname + n + 1;
>>>>+	}
>>>>+
>>>>+	return NULL;
>>>>+}
>>>>+
>>>>/**
>>>>* Add an exported symbol - it may have already been added without a
>>>>* CRC, in this case just update the CRC
>>>>@@ -319,16 +367,18 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
>>>>static struct symbol *sym_add_exported(const char *name, struct module *mod,
>>>>				       enum export export)
>>>>{
>>>>-	struct symbol *s = find_symbol(name);
>>>>+	const char *symbol_name = name;
>>>>+	const char *namespace = sym_extract_namespace(&symbol_name);
>>>>+	struct symbol *s = find_symbol(symbol_name);
>>>>
>>>>	if (!s) {
>>>>-		s = new_symbol(name, mod, export);
>>>>+		s = new_symbol(symbol_name, mod, export);
>>>>+		s->namespace = namespace;
>>>>	} else {
>>>>		if (!s->preloaded) {
>>>>-			warn("%s: '%s' exported twice. Previous export "
>>>>-			     "was in %s%s\n", mod->name, name,
>>>>-			     s->module->name,
>>>>-			     is_vmlinux(s->module->name) ?"":".ko");
>>>>+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
>>>>+			     mod->name, symbol_name, s->module->name,
>>>>+			     is_vmlinux(s->module->name) ? "" : ".ko");
>>>>		} else {
>>>>			/* In case Module.symvers was out of date */
>>>>			s->module = mod;
>>>>@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
>>>>	const char *symname;
>>>>	char *version;
>>>>	char *license;
>>>>+	char *namespace;
>>>>	struct module *mod;
>>>>	struct elf_info info = { };
>>>>	Elf_Sym *sym;
>>>>@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
>>>>		license = get_next_modinfo(&info, "license", license);
>>>>	}
>>>>
>>>>+	namespace = get_modinfo(&info, "import_ns");
>>>>+	while (namespace) {
>>>>+		add_namespace(&mod->imported_namespaces, namespace);
>>>>+		namespace = get_next_modinfo(&info, "import_ns", namespace);
>>>>+	}
>>>>+
>>>>	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
>>>>		symname = remove_dot(info.strtab + sym->st_name);
>>>>
>>>>@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
>>>>			basename++;
>>>>		else
>>>>			basename = mod->name;
>>>>+
>>>>+		if (exp->namespace &&
>>>>+		    !module_imports_namespace(mod, exp->namespace)) {
>>>>+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
>>>>+			     basename, exp->name, exp->namespace);
>>>>+		}
>>>>+
>>>>		if (!mod->gpl_compatible)
>>>>			check_for_gpl_usage(exp->export, basename, exp->name);
>>>>		check_for_unused(exp->export, basename, exp->name);
>>>>@@ -2395,16 +2459,21 @@ static void write_dump(const char *fname)
>>>>{
>>>>	struct buffer buf = { };
>>>>	struct symbol *symbol;
>>>>+	const char *namespace;
>>>>	int n;
>>>>
>>>>	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
>>>>		symbol = symbolhash[n];
>>>>		while (symbol) {
>>>>-			if (dump_sym(symbol))
>>>>-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
>>>>-					symbol->crc, symbol->name,
>>>>-					symbol->module->name,
>>>>-					export_str(symbol->export));
>>>>+			if (dump_sym(symbol)) {
>>>>+				namespace = symbol->namespace;
>>>>+				buf_printf(&buf, "0x%08x\t%s%s%s\t%s\t%s\n",
>>>>+					   symbol->crc, symbol->name,
>>>>+					   namespace ? "." : "",
>>>>+					   namespace ? namespace : "",
>>>
>>>I think it might be cleaner to just have namespace be a separate
>>>field in Module.symvers, rather than appending a dot and the
>>>namespace at the end of a symbol name. Maybe something like
>>>
>>> <crc> <symbol_name> <namespace> <module>
>>>
>>>For symbols without a namespace, we could just have "", with all
>>>fields delimited by tabs. This is just a stylistic suggestion, what do
>>>you think?
>>
>>I thought of something like that initially, but did not do it to not
>>break users of this file. But as I am anyway breaking users by changing
>>the symbol name into symbol.NS, I might as well do it as you suggested.
>>Since read_dump() also knew already how to extract the namespaces from
>>symbol.NS, it had already worked without a change to the reading code
>>of modpost. Are there any other consumers of Module.symvers that we
>>should be aware of?
>
>Maybe we can ease any possible breakage caused by the new format by
>putting the namespace column last? Then the first 4 fields crc,
>symbol, module, export type will remain in the same order, with
>namespace last. In-tree, I think we would need to update
>scripts/export_report.pl.
>
>kmod is likely to be affected too - Lucas, would the addition of a new
>field (in the order described above) in Module.symvers break any kmod tools?
>

According to the comment right above read_dump() and having a look a the
implementation of it, the format is

   0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]

So, independently of what 'something' is, also export seems to be
optional. OTOH, write_dump() seems to consistently write

   0x12345678<tab>symbol<tab>module<tab>export

Not sure if there is something modifying Module.symvers afterwards or
whether this is something that historically needs to be supported, but
if we could rule that out, I would prefer

  0x12345678<tab>symbol<tab>namespace<tab>module<tab>export

as you initially suggested.

Cheers,
Matthias

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

* Re: [PATCH v3 04/11] modpost: add support for symbol namespaces
  2019-08-28  9:55             ` Matthias Maennich
@ 2019-08-28 10:16               ` Jessica Yu
  0 siblings, 0 replies; 105+ messages in thread
From: Jessica Yu @ 2019-08-28 10:16 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro

+++ Matthias Maennich [28/08/19 10:55 +0100]:
>On Wed, Aug 28, 2019 at 11:43:26AM +0200, Jessica Yu wrote:
>>+++ Matthias Maennich [27/08/19 15:41 +0100]:
>>>On Mon, Aug 26, 2019 at 06:21:38PM +0200, Jessica Yu wrote:
>>>>+++ Matthias Maennich [21/08/19 12:49 +0100]:
>>>>>Add support for symbols that are exported into namespaces. For that,
>>>>>extract any namespace suffix from the symbol name. In addition, emit a
>>>>>warning whenever a module refers to an exported symbol without
>>>>>explicitly importing the namespace that it is defined in. This patch
>>>>>consistently adds the namespace suffix to symbol names exported into
>>>>>Module.symvers.
>>>>>
>>>>>Example warning emitted by modpost in case of the above violation:
>>>>>
>>>>>WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
>>>>>USB_STORAGE, but does not import it.
>>>>>
>>>>>Co-developed-by: Martijn Coenen <maco@android.com>
>>>>>Signed-off-by: Martijn Coenen <maco@android.com>
>>>>>Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
>>>>>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>>>>Signed-off-by: Matthias Maennich <maennich@google.com>
>>>>>---
>>>>>scripts/mod/modpost.c | 91 +++++++++++++++++++++++++++++++++++++------
>>>>>scripts/mod/modpost.h |  7 ++++
>>>>>2 files changed, 87 insertions(+), 11 deletions(-)
>>>>>
>>>>>diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
>>>>>index f277e116e0eb..538bb24ffee3 100644
>>>>>--- a/scripts/mod/modpost.c
>>>>>+++ b/scripts/mod/modpost.c
>>>>>@@ -164,6 +164,7 @@ struct symbol {
>>>>>	struct module *module;
>>>>>	unsigned int crc;
>>>>>	int crc_valid;
>>>>>+	const char *namespace;
>>>>>	unsigned int weak:1;
>>>>>	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
>>>>>	unsigned int kernel:1;     /* 1 if symbol is from kernel
>>>>>@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
>>>>>	return NULL;
>>>>>}
>>>>>
>>>>>+static bool contains_namespace(struct namespace_list *list,
>>>>>+			       const char *namespace)
>>>>>+{
>>>>>+	struct namespace_list *ns_entry;
>>>>>+
>>>>>+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
>>>>>+		if (strcmp(ns_entry->namespace, namespace) == 0)
>>>>>+			return true;
>>>>>+
>>>>>+	return false;
>>>>>+}
>>>>>+
>>>>>+static void add_namespace(struct namespace_list **list, const char *namespace)
>>>>>+{
>>>>>+	struct namespace_list *ns_entry;
>>>>>+
>>>>>+	if (!contains_namespace(*list, namespace)) {
>>>>>+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
>>>>>+					 strlen(namespace) + 1));
>>>>>+		strcpy(ns_entry->namespace, namespace);
>>>>>+		ns_entry->next = *list;
>>>>>+		*list = ns_entry;
>>>>>+	}
>>>>>+}
>>>>>+
>>>>>+static bool module_imports_namespace(struct module *module,
>>>>>+				     const char *namespace)
>>>>>+{
>>>>>+	return contains_namespace(module->imported_namespaces, namespace);
>>>>>+}
>>>>>+
>>>>>static const struct {
>>>>>	const char *str;
>>>>>	enum export export;
>>>>>@@ -312,6 +344,22 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
>>>>>		return export_unknown;
>>>>>}
>>>>>
>>>>>+static const char *sym_extract_namespace(const char **symname)
>>>>>+{
>>>>>+	size_t n;
>>>>>+	char *dupsymname;
>>>>>+
>>>>>+	n = strcspn(*symname, ".");
>>>>>+	if (n < strlen(*symname) - 1) {
>>>>>+		dupsymname = NOFAIL(strdup(*symname));
>>>>>+		dupsymname[n] = '\0';
>>>>>+		*symname = dupsymname;
>>>>>+		return dupsymname + n + 1;
>>>>>+	}
>>>>>+
>>>>>+	return NULL;
>>>>>+}
>>>>>+
>>>>>/**
>>>>>* Add an exported symbol - it may have already been added without a
>>>>>* CRC, in this case just update the CRC
>>>>>@@ -319,16 +367,18 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
>>>>>static struct symbol *sym_add_exported(const char *name, struct module *mod,
>>>>>				       enum export export)
>>>>>{
>>>>>-	struct symbol *s = find_symbol(name);
>>>>>+	const char *symbol_name = name;
>>>>>+	const char *namespace = sym_extract_namespace(&symbol_name);
>>>>>+	struct symbol *s = find_symbol(symbol_name);
>>>>>
>>>>>	if (!s) {
>>>>>-		s = new_symbol(name, mod, export);
>>>>>+		s = new_symbol(symbol_name, mod, export);
>>>>>+		s->namespace = namespace;
>>>>>	} else {
>>>>>		if (!s->preloaded) {
>>>>>-			warn("%s: '%s' exported twice. Previous export "
>>>>>-			     "was in %s%s\n", mod->name, name,
>>>>>-			     s->module->name,
>>>>>-			     is_vmlinux(s->module->name) ?"":".ko");
>>>>>+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
>>>>>+			     mod->name, symbol_name, s->module->name,
>>>>>+			     is_vmlinux(s->module->name) ? "" : ".ko");
>>>>>		} else {
>>>>>			/* In case Module.symvers was out of date */
>>>>>			s->module = mod;
>>>>>@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
>>>>>	const char *symname;
>>>>>	char *version;
>>>>>	char *license;
>>>>>+	char *namespace;
>>>>>	struct module *mod;
>>>>>	struct elf_info info = { };
>>>>>	Elf_Sym *sym;
>>>>>@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
>>>>>		license = get_next_modinfo(&info, "license", license);
>>>>>	}
>>>>>
>>>>>+	namespace = get_modinfo(&info, "import_ns");
>>>>>+	while (namespace) {
>>>>>+		add_namespace(&mod->imported_namespaces, namespace);
>>>>>+		namespace = get_next_modinfo(&info, "import_ns", namespace);
>>>>>+	}
>>>>>+
>>>>>	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
>>>>>		symname = remove_dot(info.strtab + sym->st_name);
>>>>>
>>>>>@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
>>>>>			basename++;
>>>>>		else
>>>>>			basename = mod->name;
>>>>>+
>>>>>+		if (exp->namespace &&
>>>>>+		    !module_imports_namespace(mod, exp->namespace)) {
>>>>>+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
>>>>>+			     basename, exp->name, exp->namespace);
>>>>>+		}
>>>>>+
>>>>>		if (!mod->gpl_compatible)
>>>>>			check_for_gpl_usage(exp->export, basename, exp->name);
>>>>>		check_for_unused(exp->export, basename, exp->name);
>>>>>@@ -2395,16 +2459,21 @@ static void write_dump(const char *fname)
>>>>>{
>>>>>	struct buffer buf = { };
>>>>>	struct symbol *symbol;
>>>>>+	const char *namespace;
>>>>>	int n;
>>>>>
>>>>>	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
>>>>>		symbol = symbolhash[n];
>>>>>		while (symbol) {
>>>>>-			if (dump_sym(symbol))
>>>>>-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
>>>>>-					symbol->crc, symbol->name,
>>>>>-					symbol->module->name,
>>>>>-					export_str(symbol->export));
>>>>>+			if (dump_sym(symbol)) {
>>>>>+				namespace = symbol->namespace;
>>>>>+				buf_printf(&buf, "0x%08x\t%s%s%s\t%s\t%s\n",
>>>>>+					   symbol->crc, symbol->name,
>>>>>+					   namespace ? "." : "",
>>>>>+					   namespace ? namespace : "",
>>>>
>>>>I think it might be cleaner to just have namespace be a separate
>>>>field in Module.symvers, rather than appending a dot and the
>>>>namespace at the end of a symbol name. Maybe something like
>>>>
>>>><crc> <symbol_name> <namespace> <module>
>>>>
>>>>For symbols without a namespace, we could just have "", with all
>>>>fields delimited by tabs. This is just a stylistic suggestion, what do
>>>>you think?
>>>
>>>I thought of something like that initially, but did not do it to not
>>>break users of this file. But as I am anyway breaking users by changing
>>>the symbol name into symbol.NS, I might as well do it as you suggested.
>>>Since read_dump() also knew already how to extract the namespaces from
>>>symbol.NS, it had already worked without a change to the reading code
>>>of modpost. Are there any other consumers of Module.symvers that we
>>>should be aware of?
>>
>>Maybe we can ease any possible breakage caused by the new format by
>>putting the namespace column last? Then the first 4 fields crc,
>>symbol, module, export type will remain in the same order, with
>>namespace last. In-tree, I think we would need to update
>>scripts/export_report.pl.
>>
>>kmod is likely to be affected too - Lucas, would the addition of a new
>>field (in the order described above) in Module.symvers break any kmod tools?
>>
>
>According to the comment right above read_dump() and having a look a the
>implementation of it, the format is
>
>  0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
>
>So, independently of what 'something' is, also export seems to be
>optional. OTOH, write_dump() seems to consistently write
>
>  0x12345678<tab>symbol<tab>module<tab>export
>
>Not sure if there is something modifying Module.symvers afterwards or
>whether this is something that historically needs to be supported, but
>if we could rule that out, I would prefer
>
> 0x12345678<tab>symbol<tab>namespace<tab>module<tab>export
>
>as you initially suggested.

I don't mind either format :-) As long as the kmod folks are OK with it.

Thanks,

Jessica



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

* Re: [PATCH v3 06/11] export: allow definition default namespaces in Makefiles or sources
  2019-08-21 11:49     ` [PATCH v3 06/11] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
@ 2019-08-28 10:49       ` Jessica Yu
  2019-08-28 10:56         ` Matthias Maennich
  0 siblings, 1 reply; 105+ messages in thread
From: Jessica Yu @ 2019-08-28 10:49 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Michael Ellerman, Ingo Molnar,
	Ard Biesheuvel

+++ Matthias Maennich [21/08/19 12:49 +0100]:
>To avoid excessive usage of EXPORT_SYMBOL_NS(sym, MY_NAMESPACE), where
>MY_NAMESPACE will always be the namespace we are exporting to, allow
>exporting all definitions of EXPORT_SYMBOL() and friends by defining
>DEFAULT_SYMBOL_NAMESPACE.
>
>For example, to export all symbols defined in usb-common into the
>namespace USB_COMMON, add a line like this to drivers/usb/common/Makefile:
>
>  ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON
>
>That is equivalent to changing all EXPORT_SYMBOL(sym) definitions to
>EXPORT_SYMBOL_NS(sym, USB_COMMON). Subsequently all symbol namespaces
>functionality will apply.
>
>Another way of making use of this feature is to define the namespace
>within source or header files similar to how TRACE_SYSTEM defines are
>used:
>  #undef DEFAULT_SYMBOL_NAMESPACE
>  #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON
>
>Please note that, as opposed to TRACE_SYSTEM, DEFAULT_SYMBOL_NAMESPACE
>has to be defined before including include/linux/export.h.
>
>If DEFAULT_SYMBOL_NAMESPACE is defined, a symbol can still be exported
>to another namespace by using EXPORT_SYMBOL_NS() and friends with
>explicitly specifying the namespace.

This changelog provides a good summary of how to use
DEFAULT_SYMBOL_NAMESPACE, I wonder if we should explicitly document
its proper usage somewhere? (along with EXPORT_SYMBOL_NS*)
The EXPORT_SYMBOL API is briefly documented in
Documentation/kernel-hacking/hacking.rst - it might be slightly dated,
but perhaps it'd fit there best?

>Suggested-by: Arnd Bergmann <arnd@arndb.de>
>Reviewed-by: Martijn Coenen <maco@android.com>
>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>Signed-off-by: Matthias Maennich <maennich@google.com>
>---
> include/linux/export.h | 6 ++++++
> 1 file changed, 6 insertions(+)
>
>diff --git a/include/linux/export.h b/include/linux/export.h
>index 8e12e05444d1..1fb243abdbc4 100644
>--- a/include/linux/export.h
>+++ b/include/linux/export.h
>@@ -166,6 +166,12 @@ struct kernel_symbol {
> #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
> #endif
>
>+#ifdef DEFAULT_SYMBOL_NAMESPACE
>+#undef __EXPORT_SYMBOL
>+#define __EXPORT_SYMBOL(sym, sec)				\
>+	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
>+#endif
>+
> #define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
> #define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
> #define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
>-- 
>2.23.0.rc1.153.gdeed80330f-goog
>

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

* Re: [PATCH v3 06/11] export: allow definition default namespaces in Makefiles or sources
  2019-08-28 10:49       ` Jessica Yu
@ 2019-08-28 10:56         ` Matthias Maennich
  0 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-08-28 10:56 UTC (permalink / raw)
  To: Jessica Yu
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Michael Ellerman, Ingo Molnar,
	Ard Biesheuvel

On Wed, Aug 28, 2019 at 12:49:51PM +0200, Jessica Yu wrote:
>+++ Matthias Maennich [21/08/19 12:49 +0100]:
>>To avoid excessive usage of EXPORT_SYMBOL_NS(sym, MY_NAMESPACE), where
>>MY_NAMESPACE will always be the namespace we are exporting to, allow
>>exporting all definitions of EXPORT_SYMBOL() and friends by defining
>>DEFAULT_SYMBOL_NAMESPACE.
>>
>>For example, to export all symbols defined in usb-common into the
>>namespace USB_COMMON, add a line like this to drivers/usb/common/Makefile:
>>
>> ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON
>>
>>That is equivalent to changing all EXPORT_SYMBOL(sym) definitions to
>>EXPORT_SYMBOL_NS(sym, USB_COMMON). Subsequently all symbol namespaces
>>functionality will apply.
>>
>>Another way of making use of this feature is to define the namespace
>>within source or header files similar to how TRACE_SYSTEM defines are
>>used:
>> #undef DEFAULT_SYMBOL_NAMESPACE
>> #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON
>>
>>Please note that, as opposed to TRACE_SYSTEM, DEFAULT_SYMBOL_NAMESPACE
>>has to be defined before including include/linux/export.h.
>>
>>If DEFAULT_SYMBOL_NAMESPACE is defined, a symbol can still be exported
>>to another namespace by using EXPORT_SYMBOL_NS() and friends with
>>explicitly specifying the namespace.
>
>This changelog provides a good summary of how to use
>DEFAULT_SYMBOL_NAMESPACE, I wonder if we should explicitly document
>its proper usage somewhere? (along with EXPORT_SYMBOL_NS*)
>The EXPORT_SYMBOL API is briefly documented in
>Documentation/kernel-hacking/hacking.rst - it might be slightly dated,
>but perhaps it'd fit there best?

I will add documentation along with the commits. Not only for the
macros, but in general to describe the feature.

>>Suggested-by: Arnd Bergmann <arnd@arndb.de>
>>Reviewed-by: Martijn Coenen <maco@android.com>
>>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>Signed-off-by: Matthias Maennich <maennich@google.com>
>>---
>>include/linux/export.h | 6 ++++++
>>1 file changed, 6 insertions(+)
>>
>>diff --git a/include/linux/export.h b/include/linux/export.h
>>index 8e12e05444d1..1fb243abdbc4 100644
>>--- a/include/linux/export.h
>>+++ b/include/linux/export.h
>>@@ -166,6 +166,12 @@ struct kernel_symbol {
>>#define __EXPORT_SYMBOL ___EXPORT_SYMBOL
>>#endif
>>
>>+#ifdef DEFAULT_SYMBOL_NAMESPACE
>>+#undef __EXPORT_SYMBOL
>>+#define __EXPORT_SYMBOL(sym, sec)				\
>>+	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
>>+#endif
>>+
>>#define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
>>#define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
>>#define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
>>-- 
>>2.23.0.rc1.153.gdeed80330f-goog
>>

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

* Re: [PATCH v3 08/11] scripts: Coccinelle script for namespace dependencies.
  2019-08-21 11:49     ` [PATCH v3 08/11] scripts: Coccinelle script for " Matthias Maennich
  2019-08-22  6:09       ` [v3 " Markus Elfring
@ 2019-08-29 12:13       ` " Jessica Yu
  1 sibling, 0 replies; 105+ messages in thread
From: Jessica Yu @ 2019-08-29 12:13 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, geert, gregkh, hpa, joel,
	kstewart, linux-arch, linux-kbuild, linux-m68k, linux-modules,
	linux-scsi, linux-usb, lucas.de.marchi, maco, maco, michal.lkml,
	mingo, oneukum, pombredanne, sam, sspatil, stern, tglx,
	usb-storage, x86, yamada.masahiro, Julia Lawall, Gilles Muller,
	Nicolas Palix, Mauro Carvalho Chehab, David S. Miller,
	Nicolas Ferre, cocci

+++ Matthias Maennich [21/08/19 12:49 +0100]:
>A script that uses the '<module>.ns_deps' files generated by modpost to
>automatically add the required symbol namespace dependencies to each
>module.
>
>Usage:
>1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
>   DEFAULT_SYMBOL_NAMESPACE
>2) Run 'make' (or 'make modules') and get warnings about modules not
>   importing that namespace.
>3) Run 'make nsdeps' to automatically add required import statements
>   to said modules.
>
>This makes it easer for subsystem maintainers to introduce and maintain
>symbol namespaces into their codebase.
>
>Co-developed-by: Martijn Coenen <maco@android.com>
>Signed-off-by: Martijn Coenen <maco@android.com>
>Acked-by: Julia Lawall <julia.lawall@lip6.fr>
>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>Signed-off-by: Matthias Maennich <maennich@google.com>
>---
> MAINTAINERS                                 |  5 ++
> Makefile                                    | 12 +++++
> scripts/Makefile.modpost                    |  4 +-
> scripts/coccinelle/misc/add_namespace.cocci | 23 +++++++++
> scripts/nsdeps                              | 56 +++++++++++++++++++++
> 5 files changed, 99 insertions(+), 1 deletion(-)
> create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
> create mode 100644 scripts/nsdeps
>
>diff --git a/MAINTAINERS b/MAINTAINERS
>index 08176d64eed5..dd5b37b49a07 100644
>--- a/MAINTAINERS
>+++ b/MAINTAINERS
>@@ -11428,6 +11428,11 @@ S:	Maintained
> T:	git git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
> F:	tools/include/nolibc/
>
>+NSDEPS
>+M:	Matthias Maennich <maennich@google.com>
>+S:	Maintained
>+F:	scripts/nsdeps
>+
> NTB AMD DRIVER
> M:	Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
> L:	linux-ntb@googlegroups.com
>diff --git a/Makefile b/Makefile
>index a89870188c09..40311f583ee1 100644
>--- a/Makefile
>+++ b/Makefile
>@@ -1500,6 +1500,9 @@ help:
> 	@echo  '  headerdep       - Detect inclusion cycles in headers'
> 	@echo  '  coccicheck      - Check with Coccinelle'
> 	@echo  ''
>+	@echo  'Tools:'
>+	@echo  '  nsdeps          - Generate missing symbol namespace dependencies'
>+	@echo  ''
> 	@echo  'Kernel selftest:'
> 	@echo  '  kselftest       - Build and run kernel selftest (run as root)'
> 	@echo  '                    Build, install, and boot kernel before'
>@@ -1687,6 +1690,15 @@ quiet_cmd_tags = GEN     $@
> tags TAGS cscope gtags: FORCE
> 	$(call cmd,tags)
>
>+# Script to generate missing namespace dependencies
>+# ---------------------------------------------------------------------------
>+
>+PHONY += nsdeps
>+
>+nsdeps:
>+	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost nsdeps
>+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@
>+
> # Scripts to check various things for consistency
> # ---------------------------------------------------------------------------
>
>diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
>index 26e6574ecd08..743fe3a2e885 100644
>--- a/scripts/Makefile.modpost
>+++ b/scripts/Makefile.modpost
>@@ -56,7 +56,8 @@ MODPOST = scripts/mod/modpost						\
> 	$(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS)))	\
> 	$(if $(KBUILD_EXTMOD),-o $(modulesymfile))			\
> 	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)			\
>-	$(if $(KBUILD_MODPOST_WARN),-w)
>+	$(if $(KBUILD_MODPOST_WARN),-w)					\
>+	$(if $(filter nsdeps,$(MAKECMDGOALS)),-d)
>
> ifdef MODPOST_VMLINUX
>
>@@ -134,6 +135,7 @@ $(modules): %.ko :%.o %.mod.o FORCE
>
> targets += $(modules)
>
>+nsdeps: __modpost
>
> # Add FORCE to the prequisites of a target to force it to be always rebuilt.
> # ---------------------------------------------------------------------------
>diff --git a/scripts/coccinelle/misc/add_namespace.cocci b/scripts/coccinelle/misc/add_namespace.cocci
>new file mode 100644
>index 000000000000..c832bb6445a8
>--- /dev/null
>+++ b/scripts/coccinelle/misc/add_namespace.cocci
>@@ -0,0 +1,23 @@
>+// SPDX-License-Identifier: GPL-2.0-only
>+//
>+/// Adds missing MODULE_IMPORT_NS statements to source files
>+///
>+/// This script is usually called from scripts/nsdeps with -D ns=<namespace> to
>+/// add a missing namespace tag to a module source file.
>+///
>+
>+@has_ns_import@
>+declarer name MODULE_IMPORT_NS;
>+identifier virtual.ns;
>+@@
>+MODULE_IMPORT_NS(ns);
>+
>+// Add missing imports, but only adjacent to a MODULE_LICENSE statement.
>+// That ensures we are adding it only to the main module source file.
>+@do_import depends on !has_ns_import@
>+declarer name MODULE_LICENSE;
>+expression license;
>+identifier virtual.ns;
>+@@
>+MODULE_LICENSE(license);
>++ MODULE_IMPORT_NS(ns);
>diff --git a/scripts/nsdeps b/scripts/nsdeps
>new file mode 100644
>index 000000000000..3b5995a61e65
>--- /dev/null
>+++ b/scripts/nsdeps
>@@ -0,0 +1,56 @@
>+#!/bin/bash
>+# SPDX-License-Identifier: GPL-2.0
>+# Linux kernel symbol namespace import generator
>+#
>+# This script requires a minimum spatch version.
>+SPATCH_REQ_VERSION="1.0.4"
>+
>+DIR="$(dirname $(readlink -f $0))/.."
>+SPATCH="`which ${SPATCH:=spatch}`"
>+if [ ! -x "$SPATCH" ]; then
>+	echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
>+	exit 1
>+fi
>+
>+SPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh)
>+SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
>+SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
>+
>+if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
>+	echo "spatch needs to be version $SPATCH_REQ_VERSION or higher"
>+	exit 1
>+fi
>+
>+generate_deps_for_ns() {
>+	$SPATCH --very-quiet --in-place --sp-file \
>+		$srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
>+}
>+
>+generate_deps() {
>+	local mod_name=`basename $@ .ko`
>+	local mod_file=`echo $@ | sed -e 's/\.ko/\.mod/'`
>+	local ns_deps_file=`echo $@ | sed -e 's/\.ko/\.ns_deps/'`
>+	if [ ! -f "$ns_deps_file" ]; then return; fi
>+	local mod_source_files=`cat $mod_file | sed -n 1p | sed -e 's/\.o/\.c/g'`
>+	for ns in `cat $ns_deps_file`; do
>+		echo "Adding namespace $ns to module $mod_name (if needed)."
>+		generate_deps_for_ns $ns $mod_source_files
>+		# sort the imports
>+		for source_file in $mod_source_files; do
>+			sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
>+			offset=$(wc -l ${source_file}.tmp | awk '{print $1;}')
>+			cat $source_file | grep MODULE_IMPORT_NS | sort -u >> ${source_file}.tmp
>+			tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp
>+			if ! diff -q ${source_file} ${source_file}.tmp; then
>+				mv ${source_file}.tmp ${source_file}
>+			else
>+				rm ${source_file}.tmp
>+			fi
>+		done
>+	done
>+}
>+
>+for f in `cat $srctree/modules.order`; do
>+	generate_deps $f
>+done

Hi Matthias!

I normally build outside of the source tree (make O=..) and I think
that choked up the nsdeps script a bit. For example when I run
'make nsdeps O=/tmp/linux' I get:

   cat: /home/jeyu/linux/modules.order: No such file or directory

I just changed $srctree/modules.order to $objtree/modules.order and
that fixed it for me. Also, I had to prefix $source_file in the script
with $srctree so that it'd find the right file to modify.

Thanks!

Jessica

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

* [PATCH v4 00/12] Symbol Namespaces
       [not found] <20180716122125.175792-1-maco@android.com>
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
@ 2019-09-03 15:06 ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 01/12] module: support reading multiple values per modinfo tag Matthias Maennich
                     ` (12 more replies)
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
  2 siblings, 13 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

As of Linux 5.3-rc7, there are 31207 [1] exported symbols in the kernel.
That is a growth of roughly 1000 symbols since 4.17 (30206 [2]). There
seems to be some consensus amongst kernel devs that the export surface
is too large, and hard to reason about.

Generally, these symbols fall in one of these categories:
1) Symbols actually meant for drivers
2) Symbols that are only exported because functionality is split over
   multiple modules, yet they really shouldn't be used by modules outside
   of their own subsystem
3) Symbols really only meant for in-tree use

When module developers try to upstream their code, it regularly turns
out that they are using exported symbols that they really shouldn't be
using. This problem is even bigger for drivers that are currently
out-of-tree, which may be using many symbols that they shouldn't be
using, and that break when those symbols are removed or modified.

This patch allows subsystem maintainers to partition their exported
symbols into separate namespaces, and module authors to import such
namespaces only when needed.

This allows subsystem maintainers to more easily limit availability of
these namespaced symbols to other parts of the kernel. It can also be
used to partition the set of exported symbols for documentation
purposes; for example, a set of symbols that is really only used for
debugging could be in a "SUBSYSTEM_DEBUG" namespace.

The series contains two RFC patches that do not need to be merged along
with the rest of the series, but they serve as a reference for using the
symbol namespaces. Especially, the watchdog subsystem might not be
affected by the issues addressed by Symbol Namespaces. I left the patch
in for reference anyway for demonstration purposes.

I continued the work mainly done by Martijn Coenen.

Changes in v2:
- Rather than adding and evaluating separate sections __knsimport_NS,
  use modinfo tags to declare the namespaces a module introduces.
  Adjust modpost and the module loader accordingly.
- Also add support for reading multiple modinfo values for the same tag
  to allow list-like access to modinfo tags.
- The macros in export.h have been cleaned up to avoid redundancy in the
  macro parameters (ns, nspost, nspost2).
- The introduction of relative references in the ksymtab entries caused
  a rework of the macros to accommodate that configuration as well.
- Alignment of kernel_symbol in the ksymtab needed to be fixed to allow
  growing the kernel_symbol struct.
- Modpost does now also append the namespace suffix to the symbol
  entries in Module.symvers.
- The configuration option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS allows
  relaxing the enforcement of properly declared namespace imports at
  module loading time.
- Symbols can be collectively exported into a namespace by defining
  DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile.
- The requirement for a very recent coccinelle spatch has been lifted by
  simplifying the script.
- nsdeps does now ensures MODULE_IMPORT_NS statements are sorted when
  patching the module source files.
- Some minor bugs have been addressed in nsdeps to allow it to work with
  modules that have more than one source file.
- The RFC for the usb-storage symbols has been simplified by using
  DEFAULT_SYMBOL_NAMESPACE=USB_STORAGE rather than explicitly exporting
  each and every symbol into that new namespace.

Changes in v3:
- Reword the documentation for the
  MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS option for clarification.
- Fix printed required version of spatch in coccinelle script.
- Adopt kbuild changes for modpost: .mod files are no longer generated
  in .tmp_versions. Similarely, generate the .ns_deps files in the tree
  along with the .mod files. Also, nsdeps now uses modules.order as
  source for the list modules to consider.
- Add an RFC patch to introduce the namespace WATCHDOG_CORE for symbols
  exported in watchdog_core.c.

Changes in v4:
- scripts/nsdeps:
  - exit on first error
  - support out-of-tree builds O=...
- scripts/mod/modpost: make the namespace a separate field when
  exporting to Module.symvers (rather than symbol.NS)
- scripts/export_report.pl: update for new Module.symvers format
- include/linux/export.h: fixed style nits
- kernel/module.c: ensure namespaces are imported before taking a
  reference to the owner module
- Documentation: document the Symbol Namespace feature and update
  references to Module.symvers and EXPORT_SYMBOL*

This patch series was developed against v5.3-rc7.

[1] git grep "^EXPORT_SYMBOL\w*(" v5.3-rc7 | wc -l
[2] git grep "^EXPORT_SYMBOL\w*(" v4.17    | wc -l

Cc: arnd@arndb.de
Cc: gregkh@linuxfoundation.org
Cc: jeyu@kernel.org
Cc: joel@joelfernandes.org
Cc: lucas.de.marchi@gmail.com
Cc: maco@android.com
Cc: sspatil@google.com
Cc: will@kernel.org
Cc: yamada.masahiro@socionext.com
Cc: linux-kbuild@vger.kernel.org
Cc: linux-modules@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: usb-storage@lists.one-eyed-alien.net
Cc: linux-watchdog@vger.kernel.org


Matthias Maennich (12):
  module: support reading multiple values per modinfo tag
  export: explicitly align struct kernel_symbol
  module: add support for symbol namespaces.
  modpost: add support for symbol namespaces
  module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  export: allow definition default namespaces in Makefiles or sources
  modpost: add support for generating namespace dependencies
  scripts: Coccinelle script for namespace dependencies.
  docs: Add documentation for Symbol Namespaces
  usb-storage: remove single-use define for debugging
  RFC: usb-storage: export symbols in USB_STORAGE namespace
  RFC: watchdog: export core symbols in WATCHDOG_CORE namespace

 .gitignore                                  |   1 +
 Documentation/kbuild/modules.rst            |   7 +-
 Documentation/kbuild/namespaces.rst         | 154 ++++++++++++++++++++
 Documentation/kernel-hacking/hacking.rst    |  18 +++
 MAINTAINERS                                 |   5 +
 Makefile                                    |  14 +-
 arch/m68k/include/asm/export.h              |   1 -
 drivers/hwmon/ftsteutates.c                 |   1 +
 drivers/hwmon/sch56xx-common.c              |   1 +
 drivers/rtc/rtc-abx80x.c                    |   1 +
 drivers/usb/storage/Makefile                |   2 +
 drivers/usb/storage/alauda.c                |   1 +
 drivers/usb/storage/cypress_atacb.c         |   1 +
 drivers/usb/storage/datafab.c               |   1 +
 drivers/usb/storage/debug.h                 |   2 -
 drivers/usb/storage/ene_ub6250.c            |   1 +
 drivers/usb/storage/freecom.c               |   1 +
 drivers/usb/storage/isd200.c                |   1 +
 drivers/usb/storage/jumpshot.c              |   1 +
 drivers/usb/storage/karma.c                 |   1 +
 drivers/usb/storage/onetouch.c              |   1 +
 drivers/usb/storage/realtek_cr.c            |   1 +
 drivers/usb/storage/scsiglue.c              |   2 +-
 drivers/usb/storage/sddr09.c                |   1 +
 drivers/usb/storage/sddr55.c                |   1 +
 drivers/usb/storage/shuttle_usbat.c         |   1 +
 drivers/usb/storage/uas.c                   |   1 +
 drivers/watchdog/armada_37xx_wdt.c          |   1 +
 drivers/watchdog/asm9260_wdt.c              |   1 +
 drivers/watchdog/aspeed_wdt.c               |   1 +
 drivers/watchdog/at91sam9_wdt.c             |   1 +
 drivers/watchdog/atlas7_wdt.c               |   1 +
 drivers/watchdog/bcm2835_wdt.c              |   1 +
 drivers/watchdog/bcm47xx_wdt.c              |   1 +
 drivers/watchdog/bcm7038_wdt.c              |   1 +
 drivers/watchdog/bcm_kona_wdt.c             |   1 +
 drivers/watchdog/bd70528_wdt.c              |   1 +
 drivers/watchdog/cadence_wdt.c              |   1 +
 drivers/watchdog/da9052_wdt.c               |   1 +
 drivers/watchdog/da9055_wdt.c               |   1 +
 drivers/watchdog/da9062_wdt.c               |   1 +
 drivers/watchdog/da9063_wdt.c               |   1 +
 drivers/watchdog/davinci_wdt.c              |   1 +
 drivers/watchdog/digicolor_wdt.c            |   1 +
 drivers/watchdog/dw_wdt.c                   |   1 +
 drivers/watchdog/ebc-c384_wdt.c             |   1 +
 drivers/watchdog/ep93xx_wdt.c               |   1 +
 drivers/watchdog/ftwdt010_wdt.c             |   1 +
 drivers/watchdog/gpio_wdt.c                 |   1 +
 drivers/watchdog/hpwdt.c                    |   1 +
 drivers/watchdog/i6300esb.c                 |   1 +
 drivers/watchdog/iTCO_wdt.c                 |   1 +
 drivers/watchdog/ie6xx_wdt.c                |   1 +
 drivers/watchdog/imgpdc_wdt.c               |   1 +
 drivers/watchdog/imx2_wdt.c                 |   1 +
 drivers/watchdog/intel-mid_wdt.c            |   1 +
 drivers/watchdog/it87_wdt.c                 |   1 +
 drivers/watchdog/kempld_wdt.c               |   1 +
 drivers/watchdog/lpc18xx_wdt.c              |   1 +
 drivers/watchdog/max63xx_wdt.c              |   1 +
 drivers/watchdog/max77620_wdt.c             |   1 +
 drivers/watchdog/mei_wdt.c                  |   1 +
 drivers/watchdog/mena21_wdt.c               |   1 +
 drivers/watchdog/menf21bmc_wdt.c            |   1 +
 drivers/watchdog/menz69_wdt.c               |   1 +
 drivers/watchdog/meson_gxbb_wdt.c           |   1 +
 drivers/watchdog/meson_wdt.c                |   1 +
 drivers/watchdog/mlx_wdt.c                  |   1 +
 drivers/watchdog/moxart_wdt.c               |   1 +
 drivers/watchdog/mtk_wdt.c                  |   1 +
 drivers/watchdog/ni903x_wdt.c               |   1 +
 drivers/watchdog/nic7018_wdt.c              |   1 +
 drivers/watchdog/npcm_wdt.c                 |   1 +
 drivers/watchdog/of_xilinx_wdt.c            |   1 +
 drivers/watchdog/omap_wdt.c                 |   1 +
 drivers/watchdog/pm8916_wdt.c               |   1 +
 drivers/watchdog/qcom-wdt.c                 |   1 +
 drivers/watchdog/rave-sp-wdt.c              |   1 +
 drivers/watchdog/renesas_wdt.c              |   1 +
 drivers/watchdog/retu_wdt.c                 |   1 +
 drivers/watchdog/rn5t618_wdt.c              |   1 +
 drivers/watchdog/rza_wdt.c                  |   1 +
 drivers/watchdog/s3c2410_wdt.c              |   1 +
 drivers/watchdog/sama5d4_wdt.c              |   1 +
 drivers/watchdog/sirfsoc_wdt.c              |   1 +
 drivers/watchdog/softdog.c                  |   1 +
 drivers/watchdog/sp5100_tco.c               |   1 +
 drivers/watchdog/sprd_wdt.c                 |   1 +
 drivers/watchdog/st_lpc_wdt.c               |   1 +
 drivers/watchdog/stmp3xxx_rtc_wdt.c         |   1 +
 drivers/watchdog/stpmic1_wdt.c              |   1 +
 drivers/watchdog/sunxi_wdt.c                |   1 +
 drivers/watchdog/tangox_wdt.c               |   1 +
 drivers/watchdog/tegra_wdt.c                |   1 +
 drivers/watchdog/tqmx86_wdt.c               |   1 +
 drivers/watchdog/ts4800_wdt.c               |   1 +
 drivers/watchdog/ts72xx_wdt.c               |   1 +
 drivers/watchdog/twl4030_wdt.c              |   1 +
 drivers/watchdog/uniphier_wdt.c             |   1 +
 drivers/watchdog/via_wdt.c                  |   1 +
 drivers/watchdog/w83627hf_wdt.c             |   1 +
 drivers/watchdog/watchdog_core.c            |  10 +-
 drivers/watchdog/wdat_wdt.c                 |   1 +
 drivers/watchdog/wm831x_wdt.c               |   1 +
 drivers/watchdog/wm8350_wdt.c               |   1 +
 drivers/watchdog/xen_wdt.c                  |   1 +
 drivers/watchdog/ziirave_wdt.c              |   1 +
 include/asm-generic/export.h                |  14 +-
 include/linux/export.h                      |  98 +++++++++++--
 include/linux/module.h                      |   2 +
 init/Kconfig                                |  13 ++
 kernel/module.c                             |  67 ++++++++-
 scripts/Makefile.modpost                    |   4 +-
 scripts/coccinelle/misc/add_namespace.cocci |  23 +++
 scripts/export_report.pl                    |   2 +-
 scripts/mod/modpost.c                       | 150 ++++++++++++++++---
 scripts/mod/modpost.h                       |   9 ++
 scripts/nsdeps                              |  60 ++++++++
 118 files changed, 697 insertions(+), 57 deletions(-)
 create mode 100644 Documentation/kbuild/namespaces.rst
 create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
 create mode 100644 scripts/nsdeps

-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 01/12] module: support reading multiple values per modinfo tag
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 02/12] export: explicitly align struct kernel_symbol Matthias Maennich
                     ` (11 subsequent siblings)
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

Similar to modpost's get_next_modinfo(), introduce get_next_modinfo() in
kernel/module.c to acquire any further values associated with the same
modinfo tag name. That is useful for any tags that have multiple
occurrences (such as 'alias'), but is in particular introduced here as
part of the symbol namespaces patch series to read the (potentially)
multiple namespaces a module is importing.

Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 kernel/module.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index 9ee93421269c..3ee507c0a92f 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2481,7 +2481,8 @@ static char *next_string(char *string, unsigned long *secsize)
 	return string;
 }
 
-static char *get_modinfo(struct load_info *info, const char *tag)
+static char *get_next_modinfo(const struct load_info *info, const char *tag,
+			      char *prev)
 {
 	char *p;
 	unsigned int taglen = strlen(tag);
@@ -2492,13 +2493,25 @@ static char *get_modinfo(struct load_info *info, const char *tag)
 	 * get_modinfo() calls made before rewrite_section_headers()
 	 * must use sh_offset, as sh_addr isn't set!
 	 */
-	for (p = (char *)info->hdr + infosec->sh_offset; p; p = next_string(p, &size)) {
+	char *modinfo = (char *)info->hdr + infosec->sh_offset;
+
+	if (prev) {
+		size -= prev - modinfo;
+		modinfo = next_string(prev, &size);
+	}
+
+	for (p = modinfo; p; p = next_string(p, &size)) {
 		if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
 			return p + taglen + 1;
 	}
 	return NULL;
 }
 
+static char *get_modinfo(const struct load_info *info, const char *tag)
+{
+	return get_next_modinfo(info, tag, NULL);
+}
+
 static void setup_modinfo(struct module *mod, struct load_info *info)
 {
 	struct module_attribute *attr;
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 02/12] export: explicitly align struct kernel_symbol
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 01/12] module: support reading multiple values per modinfo tag Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 03/12] module: add support for symbol namespaces Matthias Maennich
                     ` (10 subsequent siblings)
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

This change allows growing struct kernel_symbol without wasting bytes to
alignment. It also concretized the alignment of ksymtab entries if
relative references are used for ksymtab entries.

struct kernel_symbol was already implicitly being aligned to the word
size, except on x86_64 and m68k, where it is aligned to 16 and 2 bytes,
respectively.

As far as I can tell there is no requirement for aligning struct
kernel_symbol to 16 bytes on x86_64, but gcc aligns structs to their
size, and the linker aligns the custom __ksymtab sections to the largest
data type contained within, so setting KSYM_ALIGN to 16 was necessary to
stay consistent with the code generated for non-ASM EXPORT_SYMBOL(). Now
that non-ASM EXPORT_SYMBOL() explicitly aligns to word size (8),
KSYM_ALIGN is no longer necessary.

In case of relative references, the alignment has been changed
accordingly to not waste space when adding new struct members.

As for m68k, struct kernel_symbol is aligned to 2 bytes even though the
structure itself is 8 bytes; using a 4-byte alignment shouldn't hurt.

I manually verified the output of the __ksymtab sections didn't change
on x86, x86_64, arm, arm64 and m68k. As expected, the section contents
didn't change, and the ELF section alignment only changed on x86_64 and
m68k. Feedback from other archs more than welcome.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 arch/m68k/include/asm/export.h | 1 -
 include/asm-generic/export.h   | 8 +++-----
 include/linux/export.h         | 3 ++-
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/arch/m68k/include/asm/export.h b/arch/m68k/include/asm/export.h
index 0af20f48bd07..b53008b67ce1 100644
--- a/arch/m68k/include/asm/export.h
+++ b/arch/m68k/include/asm/export.h
@@ -1,3 +1,2 @@
-#define KSYM_ALIGN 2
 #define KCRC_ALIGN 2
 #include <asm-generic/export.h>
diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 294d6ae785d4..63f54907317b 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -4,15 +4,13 @@
 #ifndef KSYM_FUNC
 #define KSYM_FUNC(x) x
 #endif
-#ifdef CONFIG_64BIT
-#ifndef KSYM_ALIGN
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+#define KSYM_ALIGN 4
+#elif defined(CONFIG_64BIT)
 #define KSYM_ALIGN 8
-#endif
 #else
-#ifndef KSYM_ALIGN
 #define KSYM_ALIGN 4
 #endif
-#endif
 #ifndef KCRC_ALIGN
 #define KCRC_ALIGN 4
 #endif
diff --git a/include/linux/export.h b/include/linux/export.h
index fd8711ed9ac4..28a4d2150689 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -52,7 +52,7 @@ extern struct module __this_module;
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	__ADDRESSABLE(sym)						\
 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
-	    "	.balign	8					\n"	\
+	    "	.balign 4					\n"	\
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
@@ -66,6 +66,7 @@ struct kernel_symbol {
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	static const struct kernel_symbol __ksymtab_##sym		\
 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
+	__aligned(sizeof(void *))					\
 	= { (unsigned long)&sym, __kstrtab_##sym }
 
 struct kernel_symbol {
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 03/12] module: add support for symbol namespaces.
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 01/12] module: support reading multiple values per modinfo tag Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 02/12] export: explicitly align struct kernel_symbol Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 04/12] modpost: " Matthias Maennich
                     ` (9 subsequent siblings)
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

The EXPORT_SYMBOL_NS() and EXPORT_SYMBOL_NS_GPL() macros can be used to
export a symbol to a specific namespace.  There are no _GPL_FUTURE and
_UNUSED variants because these are currently unused, and I'm not sure
they are necessary.

I didn't add EXPORT_SYMBOL_NS() for ASM exports; this patch sets the
namespace of ASM exports to NULL by default. In case of relative
references, it will be relocatable to NULL. If there's a need, this
should be pretty easy to add.

A module that wants to use a symbol exported to a namespace must add a
MODULE_IMPORT_NS() statement to their module code; otherwise, modpost
will complain when building the module, and the kernel module loader
will emit an error and fail when loading the module.

MODULE_IMPORT_NS() adds a modinfo tag 'import_ns' to the module. That
tag can be observed by the modinfo command, modpost and kernel/module.c
at the time of loading the module.

The ELF symbols are renamed to include the namespace with an asm label;
for example, symbol 'usb_stor_suspend' in namespace USB_STORAGE becomes
'usb_stor_suspend.USB_STORAGE'.  This allows modpost to do namespace
checking, without having to go through all the effort of parsing ELF and
relocation records just to get to the struct kernel_symbols.

On x86_64 I saw no difference in binary size (compression), but at
runtime this will require a word of memory per export to hold the
namespace. An alternative could be to store namespaced symbols in their
own section and use a separate 'struct namespaced_kernel_symbol' for
that section, at the cost of making the module loader more complex.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/asm-generic/export.h |  6 +--
 include/linux/export.h       | 91 +++++++++++++++++++++++++++++-------
 include/linux/module.h       |  2 +
 kernel/module.c              | 43 +++++++++++++++++
 4 files changed, 123 insertions(+), 19 deletions(-)

diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 63f54907317b..e2b5d0f569d3 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -17,11 +17,11 @@
 
 .macro __put, val, name
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
-	.long	\val - ., \name - .
+	.long	\val - ., \name - ., 0 - .
 #elif defined(CONFIG_64BIT)
-	.quad	\val, \name
+	.quad	\val, \name, 0
 #else
-	.long	\val, \name
+	.long	\val, \name, 0
 #endif
 .endm
 
diff --git a/include/linux/export.h b/include/linux/export.h
index 28a4d2150689..d59461e71478 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -20,6 +20,8 @@ extern struct module __this_module;
 
 #ifdef CONFIG_MODULES
 
+#define NS_SEPARATOR "."
+
 #if defined(__KERNEL__) && !defined(__GENKSYMS__)
 #ifdef CONFIG_MODVERSIONS
 /* Mark the CRC weak since genksyms apparently decides not to
@@ -29,13 +31,13 @@ extern struct module __this_module;
 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
 	    "	.weak	__crc_" #sym "				\n"	\
 	    "	.long	__crc_" #sym " - .			\n"	\
-	    "	.previous					\n");
+	    "	.previous					\n")
 #else
 #define __CRC_SYMBOL(sym, sec)						\
 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
 	    "	.weak	__crc_" #sym "				\n"	\
 	    "	.long	__crc_" #sym "				\n"	\
-	    "	.previous					\n");
+	    "	.previous					\n")
 #endif
 #else
 #define __CRC_SYMBOL(sym, sec)
@@ -49,6 +51,16 @@ extern struct module __this_module;
  * absolute relocations that require runtime processing on relocatable
  * kernels.
  */
+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
+	__ADDRESSABLE(sym)						\
+	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
+	    "	.balign	4					\n"	\
+	    "__ksymtab_" #sym NS_SEPARATOR #ns ":		\n"	\
+	    "	.long	" #sym "- .				\n"	\
+	    "	.long	__kstrtab_" #sym "- .			\n"	\
+	    "	.long	__kstrtab_ns_" #sym "- .		\n"	\
+	    "	.previous					\n")
+
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	__ADDRESSABLE(sym)						\
 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
@@ -56,32 +68,53 @@ extern struct module __this_module;
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
+	    "	.long	0 - .					\n"	\
 	    "	.previous					\n")
 
 struct kernel_symbol {
 	int value_offset;
 	int name_offset;
+	int namespace_offset;
 };
 #else
+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
+	static const struct kernel_symbol __ksymtab_##sym##__##ns	\
+	asm("__ksymtab_" #sym NS_SEPARATOR #ns)				\
+	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
+	__aligned(sizeof(void *))					\
+	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym }
+
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	static const struct kernel_symbol __ksymtab_##sym		\
+	asm("__ksymtab_" #sym)						\
 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
 	__aligned(sizeof(void *))					\
-	= { (unsigned long)&sym, __kstrtab_##sym }
+	= { (unsigned long)&sym, __kstrtab_##sym, NULL }
 
 struct kernel_symbol {
 	unsigned long value;
 	const char *name;
+	const char *namespace;
 };
 #endif
 
-/* For every exported symbol, place a struct in the __ksymtab section */
-#define ___EXPORT_SYMBOL(sym, sec)					\
+#define ___export_symbol_common(sym, sec)				\
 	extern typeof(sym) sym;						\
-	__CRC_SYMBOL(sym, sec)						\
+	__CRC_SYMBOL(sym, sec);						\
 	static const char __kstrtab_##sym[]				\
 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
-	= #sym;								\
+	= #sym								\
+
+/* For every exported symbol, place a struct in the __ksymtab section */
+#define ___EXPORT_SYMBOL_NS(sym, sec, ns)				\
+	___export_symbol_common(sym, sec);				\
+	static const char __kstrtab_ns_##sym[]				\
+	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
+	= #ns;								\
+	__KSYMTAB_ENTRY_NS(sym, sec, ns)
+
+#define ___EXPORT_SYMBOL(sym, sec)					\
+	___export_symbol_common(sym, sec);				\
 	__KSYMTAB_ENTRY(sym, sec)
 
 #if defined(__DISABLE_EXPORTS)
@@ -91,6 +124,7 @@ struct kernel_symbol {
  * be reused in other execution contexts such as the UEFI stub or the
  * decompressor.
  */
+#define __EXPORT_SYMBOL_NS(sym, sec, ns)
 #define __EXPORT_SYMBOL(sym, sec)
 
 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
@@ -117,18 +151,26 @@ struct kernel_symbol {
 #define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
 #define __cond_export_sym_0(sym, sec) /* nothing */
 
+#define __EXPORT_SYMBOL_NS(sym, sec, ns)				\
+	__ksym_marker(sym);						\
+	__cond_export_ns_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
+#define __cond_export_ns_sym(sym, sec, ns, conf)			\
+	___cond_export_ns_sym(sym, sec, ns, conf)
+#define ___cond_export_ns_sym(sym, sec, ns, enabled)			\
+	__cond_export_ns_sym_##enabled(sym, sec, ns)
+#define __cond_export_ns_sym_1(sym, sec, ns) ___EXPORT_SYMBOL_NS(sym, sec, ns)
+#define __cond_export_ns_sym_0(sym, sec, ns) /* nothing */
+
 #else
+#define __EXPORT_SYMBOL_NS ___EXPORT_SYMBOL_NS
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
 #endif
 
-#define EXPORT_SYMBOL(sym)					\
-	__EXPORT_SYMBOL(sym, "")
-
-#define EXPORT_SYMBOL_GPL(sym)					\
-	__EXPORT_SYMBOL(sym, "_gpl")
-
-#define EXPORT_SYMBOL_GPL_FUTURE(sym)				\
-	__EXPORT_SYMBOL(sym, "_gpl_future")
+#define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
+#define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
+#define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
+#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL_NS(sym, "", ns)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL_NS(sym, "_gpl", ns)
 
 #ifdef CONFIG_UNUSED_SYMBOLS
 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
@@ -138,11 +180,28 @@ struct kernel_symbol {
 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
 #endif
 
-#endif	/* __GENKSYMS__ */
+#endif	/* __KERNEL__ && !__GENKSYMS__ */
+
+#if defined(__GENKSYMS__)
+/*
+ * When we're running genksyms, ignore the namespace and make the _NS
+ * variants look like the normal ones. There are two reasons for this:
+ * 1) In the normal definition of EXPORT_SYMBOL_NS, the 'ns' macro
+ *    argument is itself not expanded because it's always tokenized or
+ *    concatenated; but when running genksyms, a blank definition of the
+ *    macro does allow the argument to be expanded; if a namespace
+ *    happens to collide with a #define, this can cause issues.
+ * 2) There's no need to modify genksyms to deal with the _NS variants
+ */
+#define EXPORT_SYMBOL_NS(sym, ns) EXPORT_SYMBOL(sym)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns) EXPORT_SYMBOL_GPL(sym)
+#endif
 
 #else /* !CONFIG_MODULES... */
 
 #define EXPORT_SYMBOL(sym)
+#define EXPORT_SYMBOL_NS(sym, ns)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns)
 #define EXPORT_SYMBOL_GPL(sym)
 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
 #define EXPORT_UNUSED_SYMBOL(sym)
diff --git a/include/linux/module.h b/include/linux/module.h
index 1455812dd325..b3611e749f72 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -280,6 +280,8 @@ struct notifier_block;
 
 #ifdef CONFIG_MODULES
 
+#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)
+
 extern int modules_disabled; /* for sysctl */
 /* Get/put a kernel symbol (calls must be symmetric) */
 void *__symbol_get(const char *symbol);
diff --git a/kernel/module.c b/kernel/module.c
index 3ee507c0a92f..6bb9b938f9c7 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -544,6 +544,15 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
 #endif
 }
 
+static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
+{
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	return offset_to_ptr(&sym->namespace_offset);
+#else
+	return sym->namespace;
+#endif
+}
+
 static int cmp_name(const void *va, const void *vb)
 {
 	const char *a;
@@ -1379,6 +1388,34 @@ static inline int same_magic(const char *amagic, const char *bmagic,
 }
 #endif /* CONFIG_MODVERSIONS */
 
+static char *get_modinfo(const struct load_info *info, const char *tag);
+static char *get_next_modinfo(const struct load_info *info, const char *tag,
+			      char *prev);
+
+static int verify_namespace_is_imported(const struct load_info *info,
+					const struct kernel_symbol *sym,
+					struct module *mod)
+{
+	const char *namespace;
+	char *imported_namespace;
+
+	namespace = kernel_symbol_namespace(sym);
+	if (namespace) {
+		imported_namespace = get_modinfo(info, "import_ns");
+		while (imported_namespace) {
+			if (strcmp(namespace, imported_namespace) == 0)
+				return 0;
+			imported_namespace = get_next_modinfo(
+				info, "import_ns", imported_namespace);
+		}
+		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
+		       mod->name, kernel_symbol_name(sym), namespace);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+
 /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
 static const struct kernel_symbol *resolve_symbol(struct module *mod,
 						  const struct load_info *info,
@@ -1407,6 +1444,12 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
 		goto getname;
 	}
 
+	err = verify_namespace_is_imported(info, sym, mod);
+	if (err) {
+		sym = ERR_PTR(err);
+		goto getname;
+	}
+
 	err = ref_module(mod, owner);
 	if (err) {
 		sym = ERR_PTR(err);
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 04/12] modpost: add support for symbol namespaces
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (2 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 03/12] module: add support for symbol namespaces Matthias Maennich
@ 2019-09-03 15:06   ` " Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 05/12] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
                     ` (8 subsequent siblings)
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

Add support for symbols that are exported into namespaces. For that,
extract any namespace suffix from the symbol name. In addition, emit a
warning whenever a module refers to an exported symbol without
explicitly importing the namespace that it is defined in. This patch
consistently adds the namespace suffix to symbol names exported into
Module.symvers.

Example warning emitted by modpost in case of the above violation:

 WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
 USB_STORAGE, but does not import it.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 Documentation/kbuild/modules.rst |   7 ++-
 scripts/export_report.pl         |   2 +-
 scripts/mod/modpost.c            | 104 ++++++++++++++++++++++++++-----
 scripts/mod/modpost.h            |   7 +++
 4 files changed, 101 insertions(+), 19 deletions(-)

diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 24e763482650..d2ae799237fd 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -470,9 +470,12 @@ build.
 
 	The syntax of the Module.symvers file is::
 
-		<CRC>	    <Symbol>	       <module>
+	<CRC>       <Symbol>          <Namespace>  <Module>                         <Export Type>
 
-		0x2d036834  scsi_remove_host   drivers/scsi/scsi_mod
+	0xe1cc2a05  usb_stor_suspend  USB_STORAGE  drivers/usb/storage/usb-storage  EXPORT_SYMBOL_GPL
+
+	The fields are separated by tabs and values may be empty (e.g.
+	if no namespace is defined for an exported symbol).
 
 	For a kernel build without CONFIG_MODVERSIONS enabled, the CRC
 	would read 0x00000000.
diff --git a/scripts/export_report.pl b/scripts/export_report.pl
index 7d3030d03a25..548330e8c4e7 100755
--- a/scripts/export_report.pl
+++ b/scripts/export_report.pl
@@ -94,7 +94,7 @@ if (defined $opt{'o'}) {
 #
 while ( <$module_symvers> ) {
 	chomp;
-	my (undef, $symbol, $module, $gpl) = split;
+	my (undef, $symbol, $namespace, $module, $gpl) = split('\t');
 	$SYMBOL { $symbol } =  [ $module , "0" , $symbol, $gpl];
 }
 close($module_symvers);
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index f277e116e0eb..c2d49afaea1c 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -164,6 +164,7 @@ struct symbol {
 	struct module *module;
 	unsigned int crc;
 	int crc_valid;
+	const char *namespace;
 	unsigned int weak:1;
 	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
 	unsigned int kernel:1;     /* 1 if symbol is from kernel
@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
 	return NULL;
 }
 
+static bool contains_namespace(struct namespace_list *list,
+			       const char *namespace)
+{
+	struct namespace_list *ns_entry;
+
+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
+		if (strcmp(ns_entry->namespace, namespace) == 0)
+			return true;
+
+	return false;
+}
+
+static void add_namespace(struct namespace_list **list, const char *namespace)
+{
+	struct namespace_list *ns_entry;
+
+	if (!contains_namespace(*list, namespace)) {
+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
+					 strlen(namespace) + 1));
+		strcpy(ns_entry->namespace, namespace);
+		ns_entry->next = *list;
+		*list = ns_entry;
+	}
+}
+
+static bool module_imports_namespace(struct module *module,
+				     const char *namespace)
+{
+	return contains_namespace(module->imported_namespaces, namespace);
+}
+
 static const struct {
 	const char *str;
 	enum export export;
@@ -312,23 +344,39 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
 		return export_unknown;
 }
 
+static const char *sym_extract_namespace(const char **symname)
+{
+	size_t n;
+	char *dupsymname;
+
+	n = strcspn(*symname, ".");
+	if (n < strlen(*symname) - 1) {
+		dupsymname = NOFAIL(strdup(*symname));
+		dupsymname[n] = '\0';
+		*symname = dupsymname;
+		return dupsymname + n + 1;
+	}
+
+	return NULL;
+}
+
 /**
  * Add an exported symbol - it may have already been added without a
  * CRC, in this case just update the CRC
  **/
-static struct symbol *sym_add_exported(const char *name, struct module *mod,
-				       enum export export)
+static struct symbol *sym_add_exported(const char *name, const char *namespace,
+				       struct module *mod, enum export export)
 {
 	struct symbol *s = find_symbol(name);
 
 	if (!s) {
 		s = new_symbol(name, mod, export);
+		s->namespace = namespace;
 	} else {
 		if (!s->preloaded) {
-			warn("%s: '%s' exported twice. Previous export "
-			     "was in %s%s\n", mod->name, name,
-			     s->module->name,
-			     is_vmlinux(s->module->name) ?"":".ko");
+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
+			     mod->name, name, s->module->name,
+			     is_vmlinux(s->module->name) ? "" : ".ko");
 		} else {
 			/* In case Module.symvers was out of date */
 			s->module = mod;
@@ -620,6 +668,7 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
 	unsigned int crc;
 	enum export export;
 	bool is_crc = false;
+	const char *name, *namespace;
 
 	if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
 	    strstarts(symname, "__ksymtab"))
@@ -691,8 +740,9 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
 	default:
 		/* All exported symbols */
 		if (strstarts(symname, "__ksymtab_")) {
-			sym_add_exported(symname + strlen("__ksymtab_"), mod,
-					export);
+			name = symname + strlen("__ksymtab_");
+			namespace = sym_extract_namespace(&name);
+			sym_add_exported(name, namespace, mod, export);
 		}
 		if (strcmp(symname, "init_module") == 0)
 			mod->has_init = 1;
@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
 	const char *symname;
 	char *version;
 	char *license;
+	char *namespace;
 	struct module *mod;
 	struct elf_info info = { };
 	Elf_Sym *sym;
@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
 		license = get_next_modinfo(&info, "license", license);
 	}
 
+	namespace = get_modinfo(&info, "import_ns");
+	while (namespace) {
+		add_namespace(&mod->imported_namespaces, namespace);
+		namespace = get_next_modinfo(&info, "import_ns", namespace);
+	}
+
 	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
 		symname = remove_dot(info.strtab + sym->st_name);
 
@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
 			basename++;
 		else
 			basename = mod->name;
+
+		if (exp->namespace &&
+		    !module_imports_namespace(mod, exp->namespace)) {
+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
+			     basename, exp->name, exp->namespace);
+		}
+
 		if (!mod->gpl_compatible)
 			check_for_gpl_usage(exp->export, basename, exp->name);
 		check_for_unused(exp->export, basename, exp->name);
@@ -2341,7 +2405,7 @@ static void read_dump(const char *fname, unsigned int kernel)
 		return;
 
 	while ((line = get_next_line(&pos, file, size))) {
-		char *symname, *modname, *d, *export, *end;
+		char *symname, *namespace, *modname, *d, *export, *end;
 		unsigned int crc;
 		struct module *mod;
 		struct symbol *s;
@@ -2349,7 +2413,10 @@ static void read_dump(const char *fname, unsigned int kernel)
 		if (!(symname = strchr(line, '\t')))
 			goto fail;
 		*symname++ = '\0';
-		if (!(modname = strchr(symname, '\t')))
+		if (!(namespace = strchr(symname, '\t')))
+			goto fail;
+		*namespace++ = '\0';
+		if (!(modname = strchr(namespace, '\t')))
 			goto fail;
 		*modname++ = '\0';
 		if ((export = strchr(modname, '\t')) != NULL)
@@ -2366,7 +2433,8 @@ static void read_dump(const char *fname, unsigned int kernel)
 			mod = new_module(modname);
 			mod->skip = 1;
 		}
-		s = sym_add_exported(symname, mod, export_no(export));
+		s = sym_add_exported(symname, namespace, mod,
+				     export_no(export));
 		s->kernel    = kernel;
 		s->preloaded = 1;
 		sym_update_crc(symname, mod, crc, export_no(export));
@@ -2395,16 +2463,20 @@ static void write_dump(const char *fname)
 {
 	struct buffer buf = { };
 	struct symbol *symbol;
+	const char *namespace;
 	int n;
 
 	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
 		symbol = symbolhash[n];
 		while (symbol) {
-			if (dump_sym(symbol))
-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
-					symbol->crc, symbol->name,
-					symbol->module->name,
-					export_str(symbol->export));
+			if (dump_sym(symbol)) {
+				namespace = symbol->namespace;
+				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
+					   symbol->crc, symbol->name,
+					   namespace ? namespace : "",
+					   symbol->module->name,
+					   export_str(symbol->export));
+			}
 			symbol = symbol->next;
 		}
 	}
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 8453d6ac2f77..9626bf3e7424 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -109,6 +109,11 @@ buf_printf(struct buffer *buf, const char *fmt, ...);
 void
 buf_write(struct buffer *buf, const char *s, int len);
 
+struct namespace_list {
+	struct namespace_list *next;
+	char namespace[0];
+};
+
 struct module {
 	struct module *next;
 	const char *name;
@@ -121,6 +126,8 @@ struct module {
 	struct buffer dev_table_buf;
 	char	     srcversion[25];
 	int is_dot_o;
+	// Actual imported namespaces
+	struct namespace_list *imported_namespaces;
 };
 
 struct elf_info {
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 05/12] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (3 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 04/12] modpost: " Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 06/12] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
                     ` (7 subsequent siblings)
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

If MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is enabled (default=n), the
requirement for modules to import all namespaces that are used by
the module is relaxed.

Enabling this option effectively allows (invalid) modules to be loaded
while only a warning is emitted.

Disabling this option keeps the enforcement at module loading time and
loading is denied if the module's imports are not satisfactory.

Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 init/Kconfig    | 13 +++++++++++++
 kernel/module.c | 11 +++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/init/Kconfig b/init/Kconfig
index bd7d650d4a99..cc28561288a7 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -2119,6 +2119,19 @@ config MODULE_COMPRESS_XZ
 
 endchoice
 
+config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
+	bool "Allow loading of modules with missing namespace imports"
+	help
+	  Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
+	  a namespace. A module that makes use of a symbol exported with such a
+	  namespace is required to import the namespace via MODULE_IMPORT_NS().
+	  There is no technical reason to enforce correct namespace imports,
+	  but it creates consistency between symbols defining namespaces and
+	  users importing namespaces they make use of. This option relaxes this
+	  requirement and lifts the enforcement when loading a module.
+
+	  If unsure, say N.
+
 config TRIM_UNUSED_KSYMS
 	bool "Trim unused exported kernel symbols"
 	depends on MODULES && !UNUSED_SYMBOLS
diff --git a/kernel/module.c b/kernel/module.c
index 6bb9b938f9c7..f76efcf2043e 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -1408,9 +1408,16 @@ static int verify_namespace_is_imported(const struct load_info *info,
 			imported_namespace = get_next_modinfo(
 				info, "import_ns", imported_namespace);
 		}
-		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
-		       mod->name, kernel_symbol_name(sym), namespace);
+#ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
+		pr_warn(
+#else
+		pr_err(
+#endif
+			"%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
+			mod->name, kernel_symbol_name(sym), namespace);
+#ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
 		return -EINVAL;
+#endif
 	}
 	return 0;
 }
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 06/12] export: allow definition default namespaces in Makefiles or sources
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (4 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 05/12] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 07/12] modpost: add support for generating namespace dependencies Matthias Maennich
                     ` (6 subsequent siblings)
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

To avoid excessive usage of EXPORT_SYMBOL_NS(sym, MY_NAMESPACE), where
MY_NAMESPACE will always be the namespace we are exporting to, allow
exporting all definitions of EXPORT_SYMBOL() and friends by defining
DEFAULT_SYMBOL_NAMESPACE.

For example, to export all symbols defined in usb-common into the
namespace USB_COMMON, add a line like this to drivers/usb/common/Makefile:

  ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON

That is equivalent to changing all EXPORT_SYMBOL(sym) definitions to
EXPORT_SYMBOL_NS(sym, USB_COMMON). Subsequently all symbol namespaces
functionality will apply.

Another way of making use of this feature is to define the namespace
within source or header files similar to how TRACE_SYSTEM defines are
used:
  #undef DEFAULT_SYMBOL_NAMESPACE
  #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON

Please note that, as opposed to TRACE_SYSTEM, DEFAULT_SYMBOL_NAMESPACE
has to be defined before including include/linux/export.h.

If DEFAULT_SYMBOL_NAMESPACE is defined, a symbol can still be exported
to another namespace by using EXPORT_SYMBOL_NS() and friends with
explicitly specifying the namespace.

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/linux/export.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/export.h b/include/linux/export.h
index d59461e71478..2c5468d8ea9a 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -166,6 +166,12 @@ struct kernel_symbol {
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
 #endif
 
+#ifdef DEFAULT_SYMBOL_NAMESPACE
+#undef __EXPORT_SYMBOL
+#define __EXPORT_SYMBOL(sym, sec)				\
+	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
+#endif
+
 #define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
 #define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
 #define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 07/12] modpost: add support for generating namespace dependencies
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (5 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 06/12] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 08/12] scripts: Coccinelle script for " Matthias Maennich
                     ` (5 subsequent siblings)
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

This patch adds an option to modpost to generate a <module>.ns_deps file
per module, containing the namespace dependencies for that module.

E.g. if the linked module my-module.ko would depend on the symbol
myfunc.MY_NS in the namespace MY_NS, the my-module.ns_deps file created
by modpost would contain the entry MY_NS to express the namespace
dependency of my-module imposed by using the symbol myfunc.

These files can subsequently be used by static analysis tools (like
coccinelle scripts) to address issues with missing namespace imports. A
later patch of this series will introduce such a script 'nsdeps' and a
corresponding make target to automatically add missing
MODULE_IMPORT_NS() definitions to the module's sources. For that it uses
the information provided in the generated .ns_deps files.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 .gitignore            |  1 +
 Makefile              |  2 +-
 scripts/mod/modpost.c | 54 +++++++++++++++++++++++++++++++++++++++----
 scripts/mod/modpost.h |  2 ++
 4 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2030c7a4d2f8..9ee63aa2a3fb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,6 +32,7 @@
 *.lzo
 *.mod
 *.mod.c
+*.ns_deps
 *.o
 *.o.*
 *.order
diff --git a/Makefile b/Makefile
index 0cbe8717bdb3..0cdb957ae2c3 100644
--- a/Makefile
+++ b/Makefile
@@ -1669,7 +1669,7 @@ clean: $(clean-dirs)
 		-o -name '*.ko.*' \
 		-o -name '*.dtb' -o -name '*.dtb.S' -o -name '*.dt.yaml' \
 		-o -name '*.dwo' -o -name '*.lst' \
-		-o -name '*.su' -o -name '*.mod' \
+		-o -name '*.su' -o -name '*.mod' -o -name '*.ns_deps' \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
 		-o -name '*.lex.c' -o -name '*.tab.[ch]' \
 		-o -name '*.asn1.[ch]' \
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index c2d49afaea1c..be72da25fe7c 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -38,6 +38,8 @@ static int sec_mismatch_count = 0;
 static int sec_mismatch_fatal = 0;
 /* ignore missing files */
 static int ignore_missing_files;
+/* write namespace dependencies */
+static int write_namespace_deps;
 
 enum export {
 	export_plain,      export_unused,     export_gpl,
@@ -2176,10 +2178,15 @@ static int check_exports(struct module *mod)
 		else
 			basename = mod->name;
 
-		if (exp->namespace &&
-		    !module_imports_namespace(mod, exp->namespace)) {
-			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
-			     basename, exp->name, exp->namespace);
+		if (exp->namespace) {
+			add_namespace(&mod->required_namespaces,
+				      exp->namespace);
+
+			if (!write_namespace_deps &&
+			    !module_imports_namespace(mod, exp->namespace)) {
+				warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
+				     basename, exp->name, exp->namespace);
+			}
 		}
 
 		if (!mod->gpl_compatible)
@@ -2484,6 +2491,31 @@ static void write_dump(const char *fname)
 	free(buf.p);
 }
 
+static void write_namespace_deps_files(void)
+{
+	struct module *mod;
+	struct namespace_list *ns;
+	struct buffer ns_deps_buf = {};
+
+	for (mod = modules; mod; mod = mod->next) {
+		char fname[PATH_MAX];
+
+		if (mod->skip)
+			continue;
+
+		ns_deps_buf.pos = 0;
+
+		for (ns = mod->required_namespaces; ns; ns = ns->next)
+			buf_printf(&ns_deps_buf, "%s\n", ns->namespace);
+
+		if (ns_deps_buf.pos == 0)
+			continue;
+
+		sprintf(fname, "%s.ns_deps", mod->name);
+		write_if_changed(&ns_deps_buf, fname);
+	}
+}
+
 struct ext_sym_list {
 	struct ext_sym_list *next;
 	const char *file;
@@ -2500,7 +2532,7 @@ int main(int argc, char **argv)
 	struct ext_sym_list *extsym_iter;
 	struct ext_sym_list *extsym_start = NULL;
 
-	while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
+	while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awEd")) != -1) {
 		switch (opt) {
 		case 'i':
 			kernel_read = optarg;
@@ -2541,6 +2573,9 @@ int main(int argc, char **argv)
 		case 'E':
 			sec_mismatch_fatal = 1;
 			break;
+		case 'd':
+			write_namespace_deps = 1;
+			break;
 		default:
 			exit(1);
 		}
@@ -2575,6 +2610,9 @@ int main(int argc, char **argv)
 
 		err |= check_modname_len(mod);
 		err |= check_exports(mod);
+		if (write_namespace_deps)
+			continue;
+
 		add_header(&buf, mod);
 		add_intree_flag(&buf, !external_module);
 		add_retpoline(&buf);
@@ -2587,6 +2625,12 @@ int main(int argc, char **argv)
 		sprintf(fname, "%s.mod.c", mod->name);
 		write_if_changed(&buf, fname);
 	}
+
+	if (write_namespace_deps) {
+		write_namespace_deps_files();
+		return 0;
+	}
+
 	if (dump_write)
 		write_dump(dump_write);
 	if (sec_mismatch_count && sec_mismatch_fatal)
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 9626bf3e7424..92a926d375d2 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -126,6 +126,8 @@ struct module {
 	struct buffer dev_table_buf;
 	char	     srcversion[25];
 	int is_dot_o;
+	// Required namespace dependencies
+	struct namespace_list *required_namespaces;
 	// Actual imported namespaces
 	struct namespace_list *imported_namespaces;
 };
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 08/12] scripts: Coccinelle script for namespace dependencies.
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (6 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 07/12] modpost: add support for generating namespace dependencies Matthias Maennich
@ 2019-09-03 15:06   ` " Matthias Maennich
  2019-09-04  9:53     ` Masahiro Yamada
  2019-09-03 15:06   ` [PATCH v4 09/12] docs: Add documentation for Symbol Namespaces Matthias Maennich
                     ` (4 subsequent siblings)
  12 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog,
	Julia Lawall

A script that uses the '<module>.ns_deps' files generated by modpost to
automatically add the required symbol namespace dependencies to each
module.

Usage:
1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
   DEFAULT_SYMBOL_NAMESPACE
2) Run 'make' (or 'make modules') and get warnings about modules not
   importing that namespace.
3) Run 'make nsdeps' to automatically add required import statements
   to said modules.

This makes it easer for subsystem maintainers to introduce and maintain
symbol namespaces into their codebase.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 MAINTAINERS                                 |  5 ++
 Makefile                                    | 12 +++++
 scripts/Makefile.modpost                    |  4 +-
 scripts/coccinelle/misc/add_namespace.cocci | 23 ++++++++
 scripts/nsdeps                              | 60 +++++++++++++++++++++
 5 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
 create mode 100644 scripts/nsdeps

diff --git a/MAINTAINERS b/MAINTAINERS
index e7a47b5210fd..3b170c11a0e6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11436,6 +11436,11 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
 F:	tools/include/nolibc/
 
+NSDEPS
+M:	Matthias Maennich <maennich@google.com>
+S:	Maintained
+F:	scripts/nsdeps
+
 NTB AMD DRIVER
 M:	Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
 L:	linux-ntb@googlegroups.com
diff --git a/Makefile b/Makefile
index 0cdb957ae2c3..573e90e738ad 100644
--- a/Makefile
+++ b/Makefile
@@ -1500,6 +1500,9 @@ help:
 	@echo  '  headerdep       - Detect inclusion cycles in headers'
 	@echo  '  coccicheck      - Check with Coccinelle'
 	@echo  ''
+	@echo  'Tools:'
+	@echo  '  nsdeps          - Generate missing symbol namespace dependencies'
+	@echo  ''
 	@echo  'Kernel selftest:'
 	@echo  '  kselftest       - Build and run kernel selftest (run as root)'
 	@echo  '                    Build, install, and boot kernel before'
@@ -1687,6 +1690,15 @@ quiet_cmd_tags = GEN     $@
 tags TAGS cscope gtags: FORCE
 	$(call cmd,tags)
 
+# Script to generate missing namespace dependencies
+# ---------------------------------------------------------------------------
+
+PHONY += nsdeps
+
+nsdeps:
+	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost nsdeps
+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@
+
 # Scripts to check various things for consistency
 # ---------------------------------------------------------------------------
 
diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
index 26e6574ecd08..743fe3a2e885 100644
--- a/scripts/Makefile.modpost
+++ b/scripts/Makefile.modpost
@@ -56,7 +56,8 @@ MODPOST = scripts/mod/modpost						\
 	$(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS)))	\
 	$(if $(KBUILD_EXTMOD),-o $(modulesymfile))			\
 	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)			\
-	$(if $(KBUILD_MODPOST_WARN),-w)
+	$(if $(KBUILD_MODPOST_WARN),-w)					\
+	$(if $(filter nsdeps,$(MAKECMDGOALS)),-d)
 
 ifdef MODPOST_VMLINUX
 
@@ -134,6 +135,7 @@ $(modules): %.ko :%.o %.mod.o FORCE
 
 targets += $(modules)
 
+nsdeps: __modpost
 
 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
 # ---------------------------------------------------------------------------
diff --git a/scripts/coccinelle/misc/add_namespace.cocci b/scripts/coccinelle/misc/add_namespace.cocci
new file mode 100644
index 000000000000..c832bb6445a8
--- /dev/null
+++ b/scripts/coccinelle/misc/add_namespace.cocci
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+/// Adds missing MODULE_IMPORT_NS statements to source files
+///
+/// This script is usually called from scripts/nsdeps with -D ns=<namespace> to
+/// add a missing namespace tag to a module source file.
+///
+
+@has_ns_import@
+declarer name MODULE_IMPORT_NS;
+identifier virtual.ns;
+@@
+MODULE_IMPORT_NS(ns);
+
+// Add missing imports, but only adjacent to a MODULE_LICENSE statement.
+// That ensures we are adding it only to the main module source file.
+@do_import depends on !has_ns_import@
+declarer name MODULE_LICENSE;
+expression license;
+identifier virtual.ns;
+@@
+MODULE_LICENSE(license);
++ MODULE_IMPORT_NS(ns);
diff --git a/scripts/nsdeps b/scripts/nsdeps
new file mode 100644
index 000000000000..75dcf79a11ed
--- /dev/null
+++ b/scripts/nsdeps
@@ -0,0 +1,60 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Linux kernel symbol namespace import generator
+#
+# This script requires a minimum spatch version.
+SPATCH_REQ_VERSION="1.0.4"
+
+DIR="$(dirname $(readlink -f $0))/.."
+SPATCH="`which ${SPATCH:=spatch}`"
+if [ ! -x "$SPATCH" ]; then
+	echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
+	exit 1
+fi
+
+SPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh)
+SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
+SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
+
+if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
+	echo "spatch needs to be version $SPATCH_REQ_VERSION or higher"
+	exit 1
+fi
+
+set -e
+
+generate_deps_for_ns() {
+	$SPATCH --very-quiet --in-place --sp-file \
+		$srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
+}
+
+generate_deps() {
+	local mod_name=`basename $@ .ko`
+	local mod_file=`echo $@ | sed -e 's/\.ko/\.mod/'`
+	local ns_deps_file=`echo $@ | sed -e 's/\.ko/\.ns_deps/'`
+	if [ ! -f "$ns_deps_file" ]; then return; fi
+	local mod_source_files=`cat $mod_file | sed -n 1p                      \
+					      | sed -e 's/\.o/\.c/g'           \
+					      | sed "s/[^ ]* */${srctree}\/&/g"`
+	for ns in `cat $ns_deps_file`; do
+		echo "Adding namespace $ns to module $mod_name (if needed)."
+		generate_deps_for_ns $ns $mod_source_files
+		# sort the imports
+		for source_file in $mod_source_files; do
+			sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
+			offset=$(wc -l ${source_file}.tmp | awk '{print $1;}')
+			cat $source_file | grep MODULE_IMPORT_NS | sort -u >> ${source_file}.tmp
+			tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp
+			if ! diff -q ${source_file} ${source_file}.tmp; then
+				mv ${source_file}.tmp ${source_file}
+			else
+				rm ${source_file}.tmp
+			fi
+		done
+	done
+}
+
+for f in `cat $objtree/modules.order`; do
+	generate_deps $f
+done
+
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 09/12] docs: Add documentation for Symbol Namespaces
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (7 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 08/12] scripts: Coccinelle script for " Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-04  7:16     ` Greg KH
  2019-09-03 15:06   ` [PATCH v4 10/12] usb-storage: remove single-use define for debugging Matthias Maennich
                     ` (3 subsequent siblings)
  12 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

Describe using Symbol Namespaces from a perspective of a user. I.e.
module authors or subsystem maintainers.

Signed-off-by: Matthias Maennich <maennich@google.com>
---
 Documentation/kbuild/namespaces.rst      | 154 +++++++++++++++++++++++
 Documentation/kernel-hacking/hacking.rst |  18 +++
 2 files changed, 172 insertions(+)
 create mode 100644 Documentation/kbuild/namespaces.rst

diff --git a/Documentation/kbuild/namespaces.rst b/Documentation/kbuild/namespaces.rst
new file mode 100644
index 000000000000..982ed7b568ac
--- /dev/null
+++ b/Documentation/kbuild/namespaces.rst
@@ -0,0 +1,154 @@
+=================
+Symbol Namespaces
+=================
+
+The following document describes how to use Symbol Namespaces to structure the
+export surface of in-kernel symbols exported through the family of
+EXPORT_SYMBOL() macros.
+
+.. Table of Contents
+
+	=== 1 Introduction
+	=== 2 How to define Symbol Namespaces
+	   --- 2.1 Using the EXPORT_SYMBOL macros
+	   --- 2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
+	=== 3 How to use Symbols exported in Namespaces
+	=== 4 Loading Modules that use namespaced Symbols
+	=== 5 Automatically creating MODULE_IMPORT_NS statements
+
+1. Introduction
+===============
+
+Symbol Namespaces have been introduced as a means to structure the export
+surface of the in-kernel API. It allows subsystem maintainers to partition
+their exported symbols into separate namespaces. That is useful for
+documentation purposes (think of the SUBSYSTEM_DEBUG namespace) as well as for
+limiting the availability of a set of symbols for use in other parts of the
+kernel. As of today, modules that make use of symbols exported into namespaces,
+are required to import the namespace. Otherwise the kernel will, depending on
+its configuration, reject loading the module or warn about a missing import.
+
+2. How to define Symbol Namespaces
+==================================
+
+Symbols can be exported into namespace using different methods. All of them are
+changing the way EXPORT_SYMBOL and friends are instrumented to create ksymtab
+entries.
+
+2.1 Using the EXPORT_SYMBOL macros
+==================================
+
+In addition to the macros EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(), that allow
+exporting of kernel symbols to the kernel symbol table, variants of these are
+available to export symbols into a certain namespace: EXPORT_SYMBOL_NS() and
+EXPORT_SYMBOL_NS_GPL(). They take one additional argument: the namespace.
+Please note that due to macro expansion that argument needs to be a
+preprocessor symbol. E.g. to export the symbol `usb_stor_suspend` into the
+namespace `USB_STORAGE`, use::
+
+	EXPORT_SYMBOL_NS(usb_stor_suspend, USB_STORAGE);
+
+The corresponding ksymtab entry struct `kernel_symbol` will have the member
+`namespace` set accordingly. A symbol that is exported without a namespace will
+refer to `NULL`. There is no default namespace if none is defined. `modpost`
+and kernel/module.c make use the namespace at build time or module load time,
+respectively.
+
+2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
+=============================================
+
+Defining namespaces for all symbols of a subsystem can be very verbose and may
+become hard to maintain. Therefore a default define (DEFAULT_SYMBOL_NAMESPACE)
+is been provided, that, if set, will become the default for all EXPORT_SYMBOL()
+and EXPORT_SYMBOL_GPL() macro expansions that do not specify a namespace.
+
+There are multiple ways of specifying this define and it depends on the
+subsystem and the maintainer's preference, which one to use. The first option
+is to define the default namespace in the `Makefile` of the subsystem. E.g. to
+export all symbols defined in usb-common into the namespace USB_COMMON, add a
+line like this to drivers/usb/common/Makefile::
+
+	ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON
+
+That will affect all EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL() statements. A
+symbol exported with EXPORT_SYMBOL_NS() while this definition is present, will
+still be exported into the namespace that is passed as the namespace argument
+as this argument has preference over a default symbol namespace.
+
+A second option to define the default namespace is directly in the compilation
+unit as preprocessor statement. The above example would then read::
+
+	#undef  DEFAULT_SYMBOL_NAMESPACE
+	#define DEFAULT_SYMBOL_NAMESPACE USB_COMMON
+
+within the corresponding compilation unit before any EXPORT_SYMBOL macro is
+used.
+
+3. How to use Symbols exported in Namespaces
+============================================
+
+In order to use symbols that are exported into namespaces, kernel modules need
+to explicitly import these namespaces. Otherwise the kernel might reject to
+load the module. The module code is required to use the macro MODULE_IMPORT_NS
+for the namespaces it uses symbols from. E.g. a module using the
+usb_stor_suspend symbol from above, needs to import the namespace USB_STORAGE
+using a statement like::
+
+	MODULE_IMPORT_NS(USB_STORAGE);
+
+This will create a `modinfo` tag in the module for each imported namespace.
+This has the side effect, that the imported namespaces of a module can be
+inspected with modinfo::
+
+	$ modinfo drivers/usb/storage/ums-karma.ko
+	[...]
+	import_ns:      USB_STORAGE
+	[...]
+
+
+It is advisable to add the MODULE_IMPORT_NS() statement close to other module
+metadata definitions like MODULE_AUTHOR() or MODULE_LICENSE(). Refer to section
+5. for a way to create missing import statements automatically.
+
+4. Loading Modules that use namespaced Symbols
+==============================================
+
+At module loading time (e.g. `insmod`), the kernel will check each symbol
+referenced from the module for its availability and whether the namespace it
+might be exported to has been imported by the module. The default behaviour of
+the kernel is to reject loading modules that don't specify sufficient imports.
+An error will be logged and loading will be failed with EINVAL. In order to
+allow loading of modules that don't satisfy this precondition, a configuration
+option is available: Setting MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS=y will
+enable loading regardless, but will emit a warning.
+
+5. Automatically creating MODULE_IMPORT_NS statements
+=====================================================
+
+Missing namespaces imports can easily be detected at build time. In fact,
+modpost will emit a warning if a module uses a symbol from a namespace
+without importing it.
+MODULE_IMPORT_NS() statements will usually be added at a definite location
+(along with other module meta data). To make the life of module authors (and
+subsystem maintainers) easier, a script and make target is available to fixup
+missing imports. Fixing missing imports can be done with::
+
+	$ make nsdeps
+
+A typical scenario for module authors would be::
+
+	- write code that depends on a symbol from a not imported namespace
+	- `make`
+	- notice the warning of modpost telling about a missing import
+	- run `make nsdeps` to add the import to the correct code location
+
+For subsystem maintainers introducing a namespace, the steps are very similar.
+Again, `make nsdeps` will eventually add the missing namespace imports for
+in-tree modules::
+
+	- move or add symbols to a namespace (e.g. with EXPORT_SYMBOL_NS())
+	- `make` (preferably with an allmodconfig to cover all in-kernel
+	  modules)
+	- notice the warning of modpost telling about a missing import
+	- run `make nsdeps` to add the import to the correct code location
+
diff --git a/Documentation/kernel-hacking/hacking.rst b/Documentation/kernel-hacking/hacking.rst
index 5891a701a159..a3ddb213a5e1 100644
--- a/Documentation/kernel-hacking/hacking.rst
+++ b/Documentation/kernel-hacking/hacking.rst
@@ -594,6 +594,24 @@ internal implementation issue, and not really an interface. Some
 maintainers and developers may however require EXPORT_SYMBOL_GPL()
 when adding any new APIs or functionality.
 
+:c:func:`EXPORT_SYMBOL_NS()`
+----------------------------
+
+Defined in ``include/linux/export.h``
+
+This is the variant of `EXPORT_SYMBOL()` that allows specifying a symbol
+namespace. Symbol Namespaces are documented in
+``Documentation/kbuild/namespaces.rst``.
+
+:c:func:`EXPORT_SYMBOL_NS_GPL()`
+--------------------------------
+
+Defined in ``include/linux/export.h``
+
+This is the variant of `EXPORT_SYMBOL_GPL()` that allows specifying a symbol
+namespace. Symbol Namespaces are documented in
+``Documentation/kbuild/namespaces.rst``.
+
 Routines and Conventions
 ========================
 
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 10/12] usb-storage: remove single-use define for debugging
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (8 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 09/12] docs: Add documentation for Symbol Namespaces Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 11/12] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
                     ` (2 subsequent siblings)
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

USB_STORAGE was defined as "usb-storage: " and used in a single location
as argument to printk. In order to be able to use the name
'USB_STORAGE', drop the definition and use the string directly for the
printk call.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/usb/storage/debug.h    | 2 --
 drivers/usb/storage/scsiglue.c | 2 +-
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/usb/storage/debug.h b/drivers/usb/storage/debug.h
index 6d64f342f587..16ce06039a4d 100644
--- a/drivers/usb/storage/debug.h
+++ b/drivers/usb/storage/debug.h
@@ -29,8 +29,6 @@
 
 #include <linux/kernel.h>
 
-#define USB_STORAGE "usb-storage: "
-
 #ifdef CONFIG_USB_STORAGE_DEBUG
 void usb_stor_show_command(const struct us_data *us, struct scsi_cmnd *srb);
 void usb_stor_show_sense(const struct us_data *us, unsigned char key,
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 05b80211290d..df4de8323eff 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -379,7 +379,7 @@ static int queuecommand_lck(struct scsi_cmnd *srb,
 
 	/* check for state-transition errors */
 	if (us->srb != NULL) {
-		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
+		printk(KERN_ERR "usb-storage: Error in %s: us->srb = %p\n",
 			__func__, us->srb);
 		return SCSI_MLQUEUE_HOST_BUSY;
 	}
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 11/12] RFC: usb-storage: export symbols in USB_STORAGE namespace
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (9 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 10/12] usb-storage: remove single-use define for debugging Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 15:06   ` [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace Matthias Maennich
  2019-09-04  9:37   ` [PATCH v4 00/12] Symbol Namespaces Masahiro Yamada
  12 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

Modules using these symbols are required to explicitly import the
namespace. This patch was generated with the following steps and serves
as a reference to use the symbol namespace feature:

 1) Define DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
 2) make  (see warnings during modpost about missing imports)
 3) make nsdeps

Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
variants can be used to explicitly specify the namespace. The advantage
of the method used here is that newly added symbols are automatically
exported and existing ones are exported without touching their
respective EXPORT_SYMBOL macro expansion.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/usb/storage/Makefile        | 2 ++
 drivers/usb/storage/alauda.c        | 1 +
 drivers/usb/storage/cypress_atacb.c | 1 +
 drivers/usb/storage/datafab.c       | 1 +
 drivers/usb/storage/ene_ub6250.c    | 1 +
 drivers/usb/storage/freecom.c       | 1 +
 drivers/usb/storage/isd200.c        | 1 +
 drivers/usb/storage/jumpshot.c      | 1 +
 drivers/usb/storage/karma.c         | 1 +
 drivers/usb/storage/onetouch.c      | 1 +
 drivers/usb/storage/realtek_cr.c    | 1 +
 drivers/usb/storage/sddr09.c        | 1 +
 drivers/usb/storage/sddr55.c        | 1 +
 drivers/usb/storage/shuttle_usbat.c | 1 +
 drivers/usb/storage/uas.c           | 1 +
 15 files changed, 16 insertions(+)

diff --git a/drivers/usb/storage/Makefile b/drivers/usb/storage/Makefile
index a67ddcbb4e24..46635fa4a340 100644
--- a/drivers/usb/storage/Makefile
+++ b/drivers/usb/storage/Makefile
@@ -8,6 +8,8 @@
 
 ccflags-y := -I $(srctree)/drivers/scsi
 
+ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_STORAGE
+
 obj-$(CONFIG_USB_UAS)		+= uas.o
 obj-$(CONFIG_USB_STORAGE)	+= usb-storage.o
 
diff --git a/drivers/usb/storage/alauda.c b/drivers/usb/storage/alauda.c
index 6b8edf6178df..ddab2cd3d2e7 100644
--- a/drivers/usb/storage/alauda.c
+++ b/drivers/usb/storage/alauda.c
@@ -36,6 +36,7 @@
 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * Status bytes
diff --git a/drivers/usb/storage/cypress_atacb.c b/drivers/usb/storage/cypress_atacb.c
index 4825902377eb..a6f3267bbef6 100644
--- a/drivers/usb/storage/cypress_atacb.c
+++ b/drivers/usb/storage/cypress_atacb.c
@@ -22,6 +22,7 @@
 MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
 MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
index 09353be199be..588818483f4b 100644
--- a/drivers/usb/storage/datafab.c
+++ b/drivers/usb/storage/datafab.c
@@ -54,6 +54,7 @@
 MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
 MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 struct datafab_info {
 	unsigned long   sectors;	/* total sector count */
diff --git a/drivers/usb/storage/ene_ub6250.c b/drivers/usb/storage/ene_ub6250.c
index c26129d5b943..8b1b73065421 100644
--- a/drivers/usb/storage/ene_ub6250.c
+++ b/drivers/usb/storage/ene_ub6250.c
@@ -26,6 +26,7 @@
 
 MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 MODULE_FIRMWARE(SD_INIT1_FIRMWARE);
 MODULE_FIRMWARE(SD_INIT2_FIRMWARE);
 MODULE_FIRMWARE(SD_RW_FIRMWARE);
diff --git a/drivers/usb/storage/freecom.c b/drivers/usb/storage/freecom.c
index 4f542df37a44..34e7eaff1174 100644
--- a/drivers/usb/storage/freecom.c
+++ b/drivers/usb/storage/freecom.c
@@ -29,6 +29,7 @@
 MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor");
 MODULE_AUTHOR("David Brown <usb-storage@davidb.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #ifdef CONFIG_USB_STORAGE_DEBUG
 static void pdump(struct us_data *us, void *ibuffer, int length);
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index 2b474d60b4db..c4da3fd6eff9 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -53,6 +53,7 @@
 MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
 MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int isd200_Initialization(struct us_data *us);
 
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
index 917f170c4124..229bf0c1afc9 100644
--- a/drivers/usb/storage/jumpshot.c
+++ b/drivers/usb/storage/jumpshot.c
@@ -51,6 +51,7 @@
 MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
 MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/karma.c b/drivers/usb/storage/karma.c
index 395cf8fb5870..05cec81dcd3f 100644
--- a/drivers/usb/storage/karma.c
+++ b/drivers/usb/storage/karma.c
@@ -23,6 +23,7 @@
 MODULE_DESCRIPTION("Driver for Rio Karma");
 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>, Keith Bennett <keith@mcs.st-and.ac.uk>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #define RIO_PREFIX "RIOP\x00"
 #define RIO_PREFIX_LEN 5
diff --git a/drivers/usb/storage/onetouch.c b/drivers/usb/storage/onetouch.c
index 39a5009a41a6..a989fe930e21 100644
--- a/drivers/usb/storage/onetouch.c
+++ b/drivers/usb/storage/onetouch.c
@@ -25,6 +25,7 @@
 MODULE_DESCRIPTION("Maxtor USB OneTouch hard drive button driver");
 MODULE_AUTHOR("Nick Sillik <n.sillik@temple.edu>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #define ONETOUCH_PKT_LEN        0x02
 #define ONETOUCH_BUTTON         KEY_PROG1
diff --git a/drivers/usb/storage/realtek_cr.c b/drivers/usb/storage/realtek_cr.c
index 1d9ce9cbc831..3789698d9d3c 100644
--- a/drivers/usb/storage/realtek_cr.c
+++ b/drivers/usb/storage/realtek_cr.c
@@ -35,6 +35,7 @@
 MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
 MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int auto_delink_en = 1;
 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
diff --git a/drivers/usb/storage/sddr09.c b/drivers/usb/storage/sddr09.c
index bc9da736bdfc..51bcd4a43690 100644
--- a/drivers/usb/storage/sddr09.c
+++ b/drivers/usb/storage/sddr09.c
@@ -47,6 +47,7 @@
 MODULE_DESCRIPTION("Driver for SanDisk SDDR-09 SmartMedia reader");
 MODULE_AUTHOR("Andries Brouwer <aeb@cwi.nl>, Robert Baruch <autophile@starband.net>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int usb_stor_sddr09_dpcm_init(struct us_data *us);
 static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us);
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index b8527c55335b..ba955d65eb0e 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -29,6 +29,7 @@
 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
 MODULE_AUTHOR("Simon Munton");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 854498e1012c..54aa1392c9ca 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -48,6 +48,7 @@
 MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /* Supported device types */
 #define USBAT_DEV_HP8200	0x01
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index 047c5922618f..bf80d6f81f58 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -1219,5 +1219,6 @@ static struct usb_driver uas_driver = {
 module_usb_driver(uas_driver);
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 MODULE_AUTHOR(
 	"Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (10 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 11/12] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
@ 2019-09-03 15:06   ` Matthias Maennich
  2019-09-03 16:10     ` Guenter Roeck
  2019-09-04  9:37   ` [PATCH v4 00/12] Symbol Namespaces Masahiro Yamada
  12 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-09-03 15:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

Modules using symbols from the WATCHDOG_CORE namespace are required to
explicitly import the namespace. This patch was generated with the
following steps and serves as a reference to use the symbol namespace
feature:

 1) Use EXPORT_SYMBOL_NS* macros instead of EXPORT_SYMBOL* for symbols
    in watchdog_core.c
 2) make  (see warnings during modpost about missing imports)
 3) make nsdeps

I used 'allmodconfig' for the above steps to ensure all occurrences are
patched.

Defining DEFAULT_SYMBOL_NAMESPACE in the Makefile is not trivial in this
case as not only watchdog_core is defined in drivers/watchdog/Makefile.
Hence this patch uses the variant of using the EXPORT_SYMBOL_NS* macros
to export into a different namespace.

An alternative to this patch would be a single definition line before
any use of EXPORT_SYMBOL*:
 #define DEFAULT_SYMBOL_NAMESPACE WATCHDOG_CORE

This patch serves as a reference on how to use the symbol namespaces.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/hwmon/ftsteutates.c         |  1 +
 drivers/hwmon/sch56xx-common.c      |  1 +
 drivers/rtc/rtc-abx80x.c            |  1 +
 drivers/watchdog/armada_37xx_wdt.c  |  1 +
 drivers/watchdog/asm9260_wdt.c      |  1 +
 drivers/watchdog/aspeed_wdt.c       |  1 +
 drivers/watchdog/at91sam9_wdt.c     |  1 +
 drivers/watchdog/atlas7_wdt.c       |  1 +
 drivers/watchdog/bcm2835_wdt.c      |  1 +
 drivers/watchdog/bcm47xx_wdt.c      |  1 +
 drivers/watchdog/bcm7038_wdt.c      |  1 +
 drivers/watchdog/bcm_kona_wdt.c     |  1 +
 drivers/watchdog/bd70528_wdt.c      |  1 +
 drivers/watchdog/cadence_wdt.c      |  1 +
 drivers/watchdog/da9052_wdt.c       |  1 +
 drivers/watchdog/da9055_wdt.c       |  1 +
 drivers/watchdog/da9062_wdt.c       |  1 +
 drivers/watchdog/da9063_wdt.c       |  1 +
 drivers/watchdog/davinci_wdt.c      |  1 +
 drivers/watchdog/digicolor_wdt.c    |  1 +
 drivers/watchdog/dw_wdt.c           |  1 +
 drivers/watchdog/ebc-c384_wdt.c     |  1 +
 drivers/watchdog/ep93xx_wdt.c       |  1 +
 drivers/watchdog/ftwdt010_wdt.c     |  1 +
 drivers/watchdog/gpio_wdt.c         |  1 +
 drivers/watchdog/hpwdt.c            |  1 +
 drivers/watchdog/i6300esb.c         |  1 +
 drivers/watchdog/iTCO_wdt.c         |  1 +
 drivers/watchdog/ie6xx_wdt.c        |  1 +
 drivers/watchdog/imgpdc_wdt.c       |  1 +
 drivers/watchdog/imx2_wdt.c         |  1 +
 drivers/watchdog/intel-mid_wdt.c    |  1 +
 drivers/watchdog/it87_wdt.c         |  1 +
 drivers/watchdog/kempld_wdt.c       |  1 +
 drivers/watchdog/lpc18xx_wdt.c      |  1 +
 drivers/watchdog/max63xx_wdt.c      |  1 +
 drivers/watchdog/max77620_wdt.c     |  1 +
 drivers/watchdog/mei_wdt.c          |  1 +
 drivers/watchdog/mena21_wdt.c       |  1 +
 drivers/watchdog/menf21bmc_wdt.c    |  1 +
 drivers/watchdog/menz69_wdt.c       |  1 +
 drivers/watchdog/meson_gxbb_wdt.c   |  1 +
 drivers/watchdog/meson_wdt.c        |  1 +
 drivers/watchdog/mlx_wdt.c          |  1 +
 drivers/watchdog/moxart_wdt.c       |  1 +
 drivers/watchdog/mtk_wdt.c          |  1 +
 drivers/watchdog/ni903x_wdt.c       |  1 +
 drivers/watchdog/nic7018_wdt.c      |  1 +
 drivers/watchdog/npcm_wdt.c         |  1 +
 drivers/watchdog/of_xilinx_wdt.c    |  1 +
 drivers/watchdog/omap_wdt.c         |  1 +
 drivers/watchdog/pm8916_wdt.c       |  1 +
 drivers/watchdog/qcom-wdt.c         |  1 +
 drivers/watchdog/rave-sp-wdt.c      |  1 +
 drivers/watchdog/renesas_wdt.c      |  1 +
 drivers/watchdog/retu_wdt.c         |  1 +
 drivers/watchdog/rn5t618_wdt.c      |  1 +
 drivers/watchdog/rza_wdt.c          |  1 +
 drivers/watchdog/s3c2410_wdt.c      |  1 +
 drivers/watchdog/sama5d4_wdt.c      |  1 +
 drivers/watchdog/sirfsoc_wdt.c      |  1 +
 drivers/watchdog/softdog.c          |  1 +
 drivers/watchdog/sp5100_tco.c       |  1 +
 drivers/watchdog/sprd_wdt.c         |  1 +
 drivers/watchdog/st_lpc_wdt.c       |  1 +
 drivers/watchdog/stmp3xxx_rtc_wdt.c |  1 +
 drivers/watchdog/stpmic1_wdt.c      |  1 +
 drivers/watchdog/sunxi_wdt.c        |  1 +
 drivers/watchdog/tangox_wdt.c       |  1 +
 drivers/watchdog/tegra_wdt.c        |  1 +
 drivers/watchdog/tqmx86_wdt.c       |  1 +
 drivers/watchdog/ts4800_wdt.c       |  1 +
 drivers/watchdog/ts72xx_wdt.c       |  1 +
 drivers/watchdog/twl4030_wdt.c      |  1 +
 drivers/watchdog/uniphier_wdt.c     |  1 +
 drivers/watchdog/via_wdt.c          |  1 +
 drivers/watchdog/w83627hf_wdt.c     |  1 +
 drivers/watchdog/watchdog_core.c    | 10 +++++-----
 drivers/watchdog/wdat_wdt.c         |  1 +
 drivers/watchdog/wm831x_wdt.c       |  1 +
 drivers/watchdog/wm8350_wdt.c       |  1 +
 drivers/watchdog/xen_wdt.c          |  1 +
 drivers/watchdog/ziirave_wdt.c      |  1 +
 83 files changed, 87 insertions(+), 5 deletions(-)

diff --git a/drivers/hwmon/ftsteutates.c b/drivers/hwmon/ftsteutates.c
index 371ce7745f5e..fa67f7c565be 100644
--- a/drivers/hwmon/ftsteutates.c
+++ b/drivers/hwmon/ftsteutates.c
@@ -830,3 +830,4 @@ module_i2c_driver(fts_driver);
 MODULE_AUTHOR("Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>");
 MODULE_DESCRIPTION("FTS Teutates driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/hwmon/sch56xx-common.c b/drivers/hwmon/sch56xx-common.c
index 6c84780e358e..06dd840f3bf7 100644
--- a/drivers/hwmon/sch56xx-common.c
+++ b/drivers/hwmon/sch56xx-common.c
@@ -571,6 +571,7 @@ static void __exit sch56xx_exit(void)
 MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 
 module_init(sch56xx_init);
 module_exit(sch56xx_exit);
diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c
index 73830670a41f..3ff3b96d14d7 100644
--- a/drivers/rtc/rtc-abx80x.c
+++ b/drivers/rtc/rtc-abx80x.c
@@ -880,3 +880,4 @@ MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
 MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/armada_37xx_wdt.c b/drivers/watchdog/armada_37xx_wdt.c
index e5dcb26d85f0..00e153ae70eb 100644
--- a/drivers/watchdog/armada_37xx_wdt.c
+++ b/drivers/watchdog/armada_37xx_wdt.c
@@ -370,4 +370,5 @@ MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");
 MODULE_DESCRIPTION("Armada 37xx CPU Watchdog");
 
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:armada_37xx_wdt");
diff --git a/drivers/watchdog/asm9260_wdt.c b/drivers/watchdog/asm9260_wdt.c
index 45047e514b8e..fd7289df2cb3 100644
--- a/drivers/watchdog/asm9260_wdt.c
+++ b/drivers/watchdog/asm9260_wdt.c
@@ -374,3 +374,4 @@ module_platform_driver(asm9260_wdt_driver);
 MODULE_DESCRIPTION("asm9260 WatchDog Timer Driver");
 MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/aspeed_wdt.c b/drivers/watchdog/aspeed_wdt.c
index cc71861e033a..2607158965d6 100644
--- a/drivers/watchdog/aspeed_wdt.c
+++ b/drivers/watchdog/aspeed_wdt.c
@@ -334,3 +334,4 @@ module_exit(aspeed_wdt_exit);
 
 MODULE_DESCRIPTION("Aspeed Watchdog Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/at91sam9_wdt.c b/drivers/watchdog/at91sam9_wdt.c
index 292b5a1ca831..e7aa31fe4336 100644
--- a/drivers/watchdog/at91sam9_wdt.c
+++ b/drivers/watchdog/at91sam9_wdt.c
@@ -416,3 +416,4 @@ module_platform_driver_probe(at91wdt_driver, at91wdt_probe);
 MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>");
 MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/atlas7_wdt.c b/drivers/watchdog/atlas7_wdt.c
index 9bfe650d802f..f67e6f101102 100644
--- a/drivers/watchdog/atlas7_wdt.c
+++ b/drivers/watchdog/atlas7_wdt.c
@@ -218,4 +218,5 @@ module_platform_driver(atlas7_wdt_driver);
 MODULE_DESCRIPTION("CSRatlas7 watchdog driver");
 MODULE_AUTHOR("Guo Zeng <Guo.Zeng@csr.com>");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:atlas7-wdt");
diff --git a/drivers/watchdog/bcm2835_wdt.c b/drivers/watchdog/bcm2835_wdt.c
index dec6ca019bea..2f41b8a6e2f6 100644
--- a/drivers/watchdog/bcm2835_wdt.c
+++ b/drivers/watchdog/bcm2835_wdt.c
@@ -242,3 +242,4 @@ MODULE_ALIAS("platform:bcm2835-wdt");
 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
 MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/bcm47xx_wdt.c b/drivers/watchdog/bcm47xx_wdt.c
index 05425c1dfd4c..231009f01024 100644
--- a/drivers/watchdog/bcm47xx_wdt.c
+++ b/drivers/watchdog/bcm47xx_wdt.c
@@ -241,3 +241,4 @@ MODULE_AUTHOR("Aleksandar Radovanovic");
 MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
 MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/bcm7038_wdt.c b/drivers/watchdog/bcm7038_wdt.c
index 979caa18d3c8..e66184997775 100644
--- a/drivers/watchdog/bcm7038_wdt.c
+++ b/drivers/watchdog/bcm7038_wdt.c
@@ -212,5 +212,6 @@ module_param(nowayout, bool, 0);
 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
 MODULE_AUTHOR("Justin Chen");
diff --git a/drivers/watchdog/bcm_kona_wdt.c b/drivers/watchdog/bcm_kona_wdt.c
index eb850a8d19df..5b5a6a76c1aa 100644
--- a/drivers/watchdog/bcm_kona_wdt.c
+++ b/drivers/watchdog/bcm_kona_wdt.c
@@ -339,3 +339,4 @@ MODULE_ALIAS("platform:" BCM_KONA_WDT_NAME);
 MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
 MODULE_DESCRIPTION("Broadcom Kona Watchdog Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/bd70528_wdt.c b/drivers/watchdog/bd70528_wdt.c
index b0152fef4fc7..790748a3a30a 100644
--- a/drivers/watchdog/bd70528_wdt.c
+++ b/drivers/watchdog/bd70528_wdt.c
@@ -288,3 +288,4 @@ module_platform_driver(bd70528_wdt);
 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
 MODULE_DESCRIPTION("BD70528 watchdog driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/cadence_wdt.c b/drivers/watchdog/cadence_wdt.c
index f8d4e91d0383..a44f3140656e 100644
--- a/drivers/watchdog/cadence_wdt.c
+++ b/drivers/watchdog/cadence_wdt.c
@@ -438,3 +438,4 @@ module_platform_driver(cdns_wdt_driver);
 MODULE_AUTHOR("Xilinx, Inc.");
 MODULE_DESCRIPTION("Watchdog driver for Cadence WDT");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/da9052_wdt.c b/drivers/watchdog/da9052_wdt.c
index d708c091bf1b..a5ceea0503bb 100644
--- a/drivers/watchdog/da9052_wdt.c
+++ b/drivers/watchdog/da9052_wdt.c
@@ -191,4 +191,5 @@ module_platform_driver(da9052_wdt_driver);
 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
 MODULE_DESCRIPTION("DA9052 SM Device Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:da9052-watchdog");
diff --git a/drivers/watchdog/da9055_wdt.c b/drivers/watchdog/da9055_wdt.c
index 389a4bdd208c..05b075f2ea90 100644
--- a/drivers/watchdog/da9055_wdt.c
+++ b/drivers/watchdog/da9055_wdt.c
@@ -166,4 +166,5 @@ module_platform_driver(da9055_wdt_driver);
 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
 MODULE_DESCRIPTION("DA9055 watchdog");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:da9055-watchdog");
diff --git a/drivers/watchdog/da9062_wdt.c b/drivers/watchdog/da9062_wdt.c
index e149e66a6ea9..dfc2025d36ba 100644
--- a/drivers/watchdog/da9062_wdt.c
+++ b/drivers/watchdog/da9062_wdt.c
@@ -232,4 +232,5 @@ module_platform_driver(da9062_wdt_driver);
 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
 MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:da9062-watchdog");
diff --git a/drivers/watchdog/da9063_wdt.c b/drivers/watchdog/da9063_wdt.c
index 3d65e92a4e3f..ae0cb7011762 100644
--- a/drivers/watchdog/da9063_wdt.c
+++ b/drivers/watchdog/da9063_wdt.c
@@ -239,4 +239,5 @@ module_platform_driver(da9063_wdt_driver);
 MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>");
 MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);
diff --git a/drivers/watchdog/davinci_wdt.c b/drivers/watchdog/davinci_wdt.c
index 2b3f3cd382ef..4fa01dfa4edb 100644
--- a/drivers/watchdog/davinci_wdt.c
+++ b/drivers/watchdog/davinci_wdt.c
@@ -274,4 +274,5 @@ MODULE_PARM_DESC(heartbeat,
 		 __MODULE_STRING(DEFAULT_HEARTBEAT));
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:davinci-wdt");
diff --git a/drivers/watchdog/digicolor_wdt.c b/drivers/watchdog/digicolor_wdt.c
index 073d37867f47..ed86cf879dae 100644
--- a/drivers/watchdog/digicolor_wdt.c
+++ b/drivers/watchdog/digicolor_wdt.c
@@ -161,3 +161,4 @@ module_platform_driver(dc_wdt_driver);
 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
 MODULE_DESCRIPTION("Driver for Conexant Digicolor watchdog timer");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/dw_wdt.c b/drivers/watchdog/dw_wdt.c
index fef7c61f5555..e98ccb38eb1d 100644
--- a/drivers/watchdog/dw_wdt.c
+++ b/drivers/watchdog/dw_wdt.c
@@ -340,3 +340,4 @@ module_platform_driver(dw_wdt_driver);
 MODULE_AUTHOR("Jamie Iles");
 MODULE_DESCRIPTION("Synopsys DesignWare Watchdog Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/ebc-c384_wdt.c b/drivers/watchdog/ebc-c384_wdt.c
index 8ef4b0df3855..7e4349472745 100644
--- a/drivers/watchdog/ebc-c384_wdt.c
+++ b/drivers/watchdog/ebc-c384_wdt.c
@@ -139,4 +139,5 @@ module_exit(ebc_c384_wdt_exit);
 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
 MODULE_DESCRIPTION("WinSystems EBC-C384 watchdog timer driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("isa:" MODULE_NAME);
diff --git a/drivers/watchdog/ep93xx_wdt.c b/drivers/watchdog/ep93xx_wdt.c
index 38e26f160b9a..edfa422896d7 100644
--- a/drivers/watchdog/ep93xx_wdt.c
+++ b/drivers/watchdog/ep93xx_wdt.c
@@ -144,3 +144,4 @@ MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
 MODULE_DESCRIPTION("EP93xx Watchdog");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/ftwdt010_wdt.c b/drivers/watchdog/ftwdt010_wdt.c
index 21dcc7765688..c19186bc00b6 100644
--- a/drivers/watchdog/ftwdt010_wdt.c
+++ b/drivers/watchdog/ftwdt010_wdt.c
@@ -227,3 +227,4 @@ module_platform_driver(ftwdt010_wdt_driver);
 MODULE_AUTHOR("Linus Walleij");
 MODULE_DESCRIPTION("Watchdog driver for Faraday Technology FTWDT010");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/gpio_wdt.c b/drivers/watchdog/gpio_wdt.c
index 0923201ce874..6a90a7ecadeb 100644
--- a/drivers/watchdog/gpio_wdt.c
+++ b/drivers/watchdog/gpio_wdt.c
@@ -194,3 +194,4 @@ module_platform_driver(gpio_wdt_driver);
 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
 MODULE_DESCRIPTION("GPIO Watchdog");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/hpwdt.c b/drivers/watchdog/hpwdt.c
index 7d34bcf1c45b..fb4d228a0d7e 100644
--- a/drivers/watchdog/hpwdt.c
+++ b/drivers/watchdog/hpwdt.c
@@ -386,6 +386,7 @@ static struct pci_driver hpwdt_driver = {
 MODULE_AUTHOR("Tom Mingarelli");
 MODULE_DESCRIPTION("hpe watchdog driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_VERSION(HPWDT_VERSION);
 
 module_param(soft_margin, int, 0);
diff --git a/drivers/watchdog/i6300esb.c b/drivers/watchdog/i6300esb.c
index a30835f547b3..700a8c6cae00 100644
--- a/drivers/watchdog/i6300esb.c
+++ b/drivers/watchdog/i6300esb.c
@@ -351,3 +351,4 @@ module_pci_driver(esb_driver);
 MODULE_AUTHOR("Ross Biro and David Härdeman");
 MODULE_DESCRIPTION("Watchdog driver for Intel 6300ESB chipsets");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/iTCO_wdt.c b/drivers/watchdog/iTCO_wdt.c
index c559f706ae7e..454d8ff3edc1 100644
--- a/drivers/watchdog/iTCO_wdt.c
+++ b/drivers/watchdog/iTCO_wdt.c
@@ -632,4 +632,5 @@ MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
 MODULE_DESCRIPTION("Intel TCO WatchDog Timer Driver");
 MODULE_VERSION(DRV_VERSION);
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:" DRV_NAME);
diff --git a/drivers/watchdog/ie6xx_wdt.c b/drivers/watchdog/ie6xx_wdt.c
index 8f28993fab8b..76bcff28eaf9 100644
--- a/drivers/watchdog/ie6xx_wdt.c
+++ b/drivers/watchdog/ie6xx_wdt.c
@@ -314,4 +314,5 @@ module_exit(ie6xx_wdt_exit);
 MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
 MODULE_DESCRIPTION("Intel Atom E6xx Watchdog Device Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:" DRIVER_NAME);
diff --git a/drivers/watchdog/imgpdc_wdt.c b/drivers/watchdog/imgpdc_wdt.c
index b57ff3787052..6fe0fd7c0719 100644
--- a/drivers/watchdog/imgpdc_wdt.c
+++ b/drivers/watchdog/imgpdc_wdt.c
@@ -314,3 +314,4 @@ MODULE_AUTHOR("Jude Abraham <Jude.Abraham@imgtec.com>");
 MODULE_AUTHOR("Naidu Tellapati <Naidu.Tellapati@imgtec.com>");
 MODULE_DESCRIPTION("Imagination Technologies PDC Watchdog Timer Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/imx2_wdt.c b/drivers/watchdog/imx2_wdt.c
index 32af3974e6bb..dc6d4132ec1f 100644
--- a/drivers/watchdog/imx2_wdt.c
+++ b/drivers/watchdog/imx2_wdt.c
@@ -433,4 +433,5 @@ module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
 MODULE_AUTHOR("Wolfram Sang");
 MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:" DRIVER_NAME);
diff --git a/drivers/watchdog/intel-mid_wdt.c b/drivers/watchdog/intel-mid_wdt.c
index 2cdbd37c700c..b859fd6d263b 100644
--- a/drivers/watchdog/intel-mid_wdt.c
+++ b/drivers/watchdog/intel-mid_wdt.c
@@ -181,3 +181,4 @@ module_platform_driver(mid_wdt_driver);
 MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
 MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/it87_wdt.c b/drivers/watchdog/it87_wdt.c
index a4b71ebc8cab..610f5cdebf72 100644
--- a/drivers/watchdog/it87_wdt.c
+++ b/drivers/watchdog/it87_wdt.c
@@ -354,3 +354,4 @@ module_exit(it87_wdt_exit);
 MODULE_AUTHOR("Oliver Schuster");
 MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/kempld_wdt.c b/drivers/watchdog/kempld_wdt.c
index 40bd518ed873..20a40e221dbb 100644
--- a/drivers/watchdog/kempld_wdt.c
+++ b/drivers/watchdog/kempld_wdt.c
@@ -550,3 +550,4 @@ module_platform_driver(kempld_wdt_driver);
 MODULE_DESCRIPTION("KEM PLD Watchdog Driver");
 MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/lpc18xx_wdt.c b/drivers/watchdog/lpc18xx_wdt.c
index 78cf11c94941..4e34a4bd0b24 100644
--- a/drivers/watchdog/lpc18xx_wdt.c
+++ b/drivers/watchdog/lpc18xx_wdt.c
@@ -316,3 +316,4 @@ module_platform_driver(lpc18xx_wdt_driver);
 MODULE_AUTHOR("Ariel D'Alessandro <ariel@vanguardiasur.com.ar>");
 MODULE_DESCRIPTION("NXP LPC18xx Watchdog Timer Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/max63xx_wdt.c b/drivers/watchdog/max63xx_wdt.c
index 3a899628a834..053c9285adac 100644
--- a/drivers/watchdog/max63xx_wdt.c
+++ b/drivers/watchdog/max63xx_wdt.c
@@ -284,3 +284,4 @@ MODULE_PARM_DESC(nodelay,
 		 "(max6373/74 only, default=0)");
 
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/max77620_wdt.c b/drivers/watchdog/max77620_wdt.c
index be6a53c30002..304817d379a7 100644
--- a/drivers/watchdog/max77620_wdt.c
+++ b/drivers/watchdog/max77620_wdt.c
@@ -209,3 +209,4 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 
 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/mei_wdt.c b/drivers/watchdog/mei_wdt.c
index 5391bf3e6b11..5e03226cada1 100644
--- a/drivers/watchdog/mei_wdt.c
+++ b/drivers/watchdog/mei_wdt.c
@@ -662,4 +662,5 @@ module_mei_cl_driver(mei_wdt_driver);
 
 MODULE_AUTHOR("Intel Corporation");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_DESCRIPTION("Device driver for Intel MEI iAMT watchdog");
diff --git a/drivers/watchdog/mena21_wdt.c b/drivers/watchdog/mena21_wdt.c
index 99d2359d5a8a..b759c66f1446 100644
--- a/drivers/watchdog/mena21_wdt.c
+++ b/drivers/watchdog/mena21_wdt.c
@@ -225,4 +225,5 @@ module_platform_driver(a21_wdt_driver);
 MODULE_AUTHOR("MEN Mikro Elektronik");
 MODULE_DESCRIPTION("MEN A21 Watchdog");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:a21-watchdog");
diff --git a/drivers/watchdog/menf21bmc_wdt.c b/drivers/watchdog/menf21bmc_wdt.c
index 81ebdfc371f4..358881618e2f 100644
--- a/drivers/watchdog/menf21bmc_wdt.c
+++ b/drivers/watchdog/menf21bmc_wdt.c
@@ -181,4 +181,5 @@ module_platform_driver(menf21bmc_wdt);
 MODULE_DESCRIPTION("MEN 14F021P00 BMC Watchdog driver");
 MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:menf21bmc_wdt");
diff --git a/drivers/watchdog/menz69_wdt.c b/drivers/watchdog/menz69_wdt.c
index ed18238c5407..d13fcfc0f59c 100644
--- a/drivers/watchdog/menz69_wdt.c
+++ b/drivers/watchdog/menz69_wdt.c
@@ -167,4 +167,5 @@ module_mcb_driver(men_z069_driver);
 
 MODULE_AUTHOR("Johannes Thumshirn <jth@kernel.org>");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("mcb:16z069");
diff --git a/drivers/watchdog/meson_gxbb_wdt.c b/drivers/watchdog/meson_gxbb_wdt.c
index d17c1a6ed723..f30f85380679 100644
--- a/drivers/watchdog/meson_gxbb_wdt.c
+++ b/drivers/watchdog/meson_gxbb_wdt.c
@@ -204,3 +204,4 @@ module_platform_driver(meson_gxbb_wdt_driver);
 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
 MODULE_DESCRIPTION("Amlogic Meson GXBB Watchdog timer driver");
 MODULE_LICENSE("Dual BSD/GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/meson_wdt.c b/drivers/watchdog/meson_wdt.c
index 459f3ae02c91..0d60bb5aa688 100644
--- a/drivers/watchdog/meson_wdt.c
+++ b/drivers/watchdog/meson_wdt.c
@@ -228,5 +228,6 @@ MODULE_PARM_DESC(nowayout,
 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
 MODULE_DESCRIPTION("Meson Watchdog Timer Driver");
diff --git a/drivers/watchdog/mlx_wdt.c b/drivers/watchdog/mlx_wdt.c
index 03b9ac4b99af..2df48a6ed04b 100644
--- a/drivers/watchdog/mlx_wdt.c
+++ b/drivers/watchdog/mlx_wdt.c
@@ -287,4 +287,5 @@ module_platform_driver(mlxreg_wdt_driver);
 MODULE_AUTHOR("Michael Shych <michaelsh@mellanox.com>");
 MODULE_DESCRIPTION("Mellanox watchdog driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:mlx-wdt");
diff --git a/drivers/watchdog/moxart_wdt.c b/drivers/watchdog/moxart_wdt.c
index 6340a1f5f471..02dbfe2accfa 100644
--- a/drivers/watchdog/moxart_wdt.c
+++ b/drivers/watchdog/moxart_wdt.c
@@ -164,4 +164,5 @@ MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
 
 MODULE_DESCRIPTION("MOXART watchdog driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");
diff --git a/drivers/watchdog/mtk_wdt.c b/drivers/watchdog/mtk_wdt.c
index 9c3d0033260d..074c04324738 100644
--- a/drivers/watchdog/mtk_wdt.c
+++ b/drivers/watchdog/mtk_wdt.c
@@ -247,6 +247,7 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
 			__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
 MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
 MODULE_VERSION(DRV_VERSION);
diff --git a/drivers/watchdog/ni903x_wdt.c b/drivers/watchdog/ni903x_wdt.c
index 4cebad324b20..724d47f2f325 100644
--- a/drivers/watchdog/ni903x_wdt.c
+++ b/drivers/watchdog/ni903x_wdt.c
@@ -255,3 +255,4 @@ MODULE_DESCRIPTION("NI 903x Watchdog");
 MODULE_AUTHOR("Jeff Westfahl <jeff.westfahl@ni.com>");
 MODULE_AUTHOR("Kyle Roeschley <kyle.roeschley@ni.com>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/nic7018_wdt.c b/drivers/watchdog/nic7018_wdt.c
index 2a46cc662943..efe0f188c79a 100644
--- a/drivers/watchdog/nic7018_wdt.c
+++ b/drivers/watchdog/nic7018_wdt.c
@@ -250,3 +250,4 @@ module_platform_driver(watchdog_driver);
 MODULE_DESCRIPTION("National Instruments NIC7018 Watchdog driver");
 MODULE_AUTHOR("Hui Chun Ong <hui.chun.ong@ni.com>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/npcm_wdt.c b/drivers/watchdog/npcm_wdt.c
index 9c773c3d6d5d..aa2022a44985 100644
--- a/drivers/watchdog/npcm_wdt.c
+++ b/drivers/watchdog/npcm_wdt.c
@@ -248,3 +248,4 @@ module_platform_driver(npcm_wdt_driver);
 MODULE_AUTHOR("Joel Stanley");
 MODULE_DESCRIPTION("Watchdog driver for NPCM");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/of_xilinx_wdt.c b/drivers/watchdog/of_xilinx_wdt.c
index 7fe4f7c3f7ce..8d93e2089c6d 100644
--- a/drivers/watchdog/of_xilinx_wdt.c
+++ b/drivers/watchdog/of_xilinx_wdt.c
@@ -308,3 +308,4 @@ module_platform_driver(xwdt_driver);
 MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
 MODULE_DESCRIPTION("Xilinx Watchdog driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/omap_wdt.c b/drivers/watchdog/omap_wdt.c
index 9b91882fe3c4..49565608924a 100644
--- a/drivers/watchdog/omap_wdt.c
+++ b/drivers/watchdog/omap_wdt.c
@@ -375,4 +375,5 @@ module_platform_driver(omap_wdt_driver);
 
 MODULE_AUTHOR("George G. Davis");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:omap_wdt");
diff --git a/drivers/watchdog/pm8916_wdt.c b/drivers/watchdog/pm8916_wdt.c
index 2d3652004e39..f13c1c2bec07 100644
--- a/drivers/watchdog/pm8916_wdt.c
+++ b/drivers/watchdog/pm8916_wdt.c
@@ -210,3 +210,4 @@ module_platform_driver(pm8916_wdt_driver);
 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
 MODULE_DESCRIPTION("Qualcomm pm8916 watchdog driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/qcom-wdt.c b/drivers/watchdog/qcom-wdt.c
index 7be7f87be28f..1ae02d3b5cb2 100644
--- a/drivers/watchdog/qcom-wdt.c
+++ b/drivers/watchdog/qcom-wdt.c
@@ -272,3 +272,4 @@ module_platform_driver(qcom_watchdog_driver);
 
 MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/rave-sp-wdt.c b/drivers/watchdog/rave-sp-wdt.c
index 2c95615b6354..7fac3fabfcfb 100644
--- a/drivers/watchdog/rave-sp-wdt.c
+++ b/drivers/watchdog/rave-sp-wdt.c
@@ -329,6 +329,7 @@ module_platform_driver(rave_sp_wdt_driver);
 
 MODULE_DEVICE_TABLE(of, rave_sp_wdt_of_match);
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
 MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
diff --git a/drivers/watchdog/renesas_wdt.c b/drivers/watchdog/renesas_wdt.c
index 00662a8e039c..ec5b1ec6c292 100644
--- a/drivers/watchdog/renesas_wdt.c
+++ b/drivers/watchdog/renesas_wdt.c
@@ -315,4 +315,5 @@ module_platform_driver(rwdt_driver);
 
 MODULE_DESCRIPTION("Renesas WDT Watchdog Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
diff --git a/drivers/watchdog/retu_wdt.c b/drivers/watchdog/retu_wdt.c
index 258dfcf9cbda..6a3e2b8e5226 100644
--- a/drivers/watchdog/retu_wdt.c
+++ b/drivers/watchdog/retu_wdt.c
@@ -168,3 +168,4 @@ MODULE_DESCRIPTION("Retu watchdog");
 MODULE_AUTHOR("Amit Kucheria");
 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/rn5t618_wdt.c b/drivers/watchdog/rn5t618_wdt.c
index 234876047431..8f7bc6b1cb64 100644
--- a/drivers/watchdog/rn5t618_wdt.c
+++ b/drivers/watchdog/rn5t618_wdt.c
@@ -191,3 +191,4 @@ module_platform_driver(rn5t618_wdt_driver);
 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
 MODULE_DESCRIPTION("RN5T618 watchdog driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/rza_wdt.c b/drivers/watchdog/rza_wdt.c
index 7b6c365f7cd3..31ff9acc9af2 100644
--- a/drivers/watchdog/rza_wdt.c
+++ b/drivers/watchdog/rza_wdt.c
@@ -245,3 +245,4 @@ module_platform_driver(rza_wdt_driver);
 MODULE_DESCRIPTION("Renesas RZ/A WDT Driver");
 MODULE_AUTHOR("Chris Brandt <chris.brandt@renesas.com>");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c
index 2395f353e52d..5c1800d41411 100644
--- a/drivers/watchdog/s3c2410_wdt.c
+++ b/drivers/watchdog/s3c2410_wdt.c
@@ -739,3 +739,4 @@ module_platform_driver(s3c2410wdt_driver);
 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/sama5d4_wdt.c b/drivers/watchdog/sama5d4_wdt.c
index d193a60430b2..725f68703f1e 100644
--- a/drivers/watchdog/sama5d4_wdt.c
+++ b/drivers/watchdog/sama5d4_wdt.c
@@ -321,3 +321,4 @@ module_platform_driver(sama5d4_wdt_driver);
 MODULE_AUTHOR("Atmel Corporation");
 MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/sirfsoc_wdt.c b/drivers/watchdog/sirfsoc_wdt.c
index 734cf2966ecb..bcb75d9978f0 100644
--- a/drivers/watchdog/sirfsoc_wdt.c
+++ b/drivers/watchdog/sirfsoc_wdt.c
@@ -213,4 +213,5 @@ module_platform_driver(sirfsoc_wdt_driver);
 MODULE_DESCRIPTION("SiRF SoC watchdog driver");
 MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:sirfsoc-wdt");
diff --git a/drivers/watchdog/softdog.c b/drivers/watchdog/softdog.c
index 3e4885c1545e..8d50543f7340 100644
--- a/drivers/watchdog/softdog.c
+++ b/drivers/watchdog/softdog.c
@@ -165,3 +165,4 @@ module_exit(softdog_exit);
 MODULE_AUTHOR("Alan Cox");
 MODULE_DESCRIPTION("Software Watchdog Device Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/sp5100_tco.c b/drivers/watchdog/sp5100_tco.c
index 93bd302ae7c5..716b9c43baf1 100644
--- a/drivers/watchdog/sp5100_tco.c
+++ b/drivers/watchdog/sp5100_tco.c
@@ -486,3 +486,4 @@ module_exit(sp5100_tco_exit);
 MODULE_AUTHOR("Priyanka Gupta");
 MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/sprd_wdt.c b/drivers/watchdog/sprd_wdt.c
index edba4e278685..55e883e8dcf4 100644
--- a/drivers/watchdog/sprd_wdt.c
+++ b/drivers/watchdog/sprd_wdt.c
@@ -384,3 +384,4 @@ module_platform_driver(sprd_watchdog_driver);
 MODULE_AUTHOR("Eric Long <eric.long@spreadtrum.com>");
 MODULE_DESCRIPTION("Spreadtrum Watchdog Timer Controller Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/st_lpc_wdt.c b/drivers/watchdog/st_lpc_wdt.c
index 14ab6559c748..4a7e6b4694fa 100644
--- a/drivers/watchdog/st_lpc_wdt.c
+++ b/drivers/watchdog/st_lpc_wdt.c
@@ -305,3 +305,4 @@ module_platform_driver(st_wdog_driver);
 MODULE_AUTHOR("David Paris <david.paris@st.com>");
 MODULE_DESCRIPTION("ST LPC Watchdog Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/stmp3xxx_rtc_wdt.c b/drivers/watchdog/stmp3xxx_rtc_wdt.c
index 7caf3aa71c6a..f1dd033c2b4b 100644
--- a/drivers/watchdog/stmp3xxx_rtc_wdt.c
+++ b/drivers/watchdog/stmp3xxx_rtc_wdt.c
@@ -150,4 +150,5 @@ module_platform_driver(stmp3xxx_wdt_driver);
 
 MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
diff --git a/drivers/watchdog/stpmic1_wdt.c b/drivers/watchdog/stpmic1_wdt.c
index 45d0c543466f..6cdbe4bf5aaa 100644
--- a/drivers/watchdog/stpmic1_wdt.c
+++ b/drivers/watchdog/stpmic1_wdt.c
@@ -138,3 +138,4 @@ module_platform_driver(stpmic1_wdt_driver);
 MODULE_DESCRIPTION("Watchdog driver for STPMIC1 device");
 MODULE_AUTHOR("Pascal Paillet <p.paillet@st.com>");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/sunxi_wdt.c b/drivers/watchdog/sunxi_wdt.c
index 5f05a45ac187..dea10c79b817 100644
--- a/drivers/watchdog/sunxi_wdt.c
+++ b/drivers/watchdog/sunxi_wdt.c
@@ -289,6 +289,7 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 		"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
 MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
 MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
diff --git a/drivers/watchdog/tangox_wdt.c b/drivers/watchdog/tangox_wdt.c
index 1afb0e9d808c..bc9025d31977 100644
--- a/drivers/watchdog/tangox_wdt.c
+++ b/drivers/watchdog/tangox_wdt.c
@@ -207,3 +207,4 @@ module_platform_driver(tangox_wdt_driver);
 MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
 MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/tegra_wdt.c b/drivers/watchdog/tegra_wdt.c
index dfe06e506cad..06acd8251928 100644
--- a/drivers/watchdog/tegra_wdt.c
+++ b/drivers/watchdog/tegra_wdt.c
@@ -276,3 +276,4 @@ module_platform_driver(tegra_wdt_driver);
 MODULE_AUTHOR("NVIDIA Corporation");
 MODULE_DESCRIPTION("Tegra Watchdog Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/tqmx86_wdt.c b/drivers/watchdog/tqmx86_wdt.c
index 72d0b0adde38..35b8bfffa34d 100644
--- a/drivers/watchdog/tqmx86_wdt.c
+++ b/drivers/watchdog/tqmx86_wdt.c
@@ -124,3 +124,4 @@ MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
 MODULE_DESCRIPTION("TQMx86 Watchdog");
 MODULE_ALIAS("platform:tqmx86-wdt");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/ts4800_wdt.c b/drivers/watchdog/ts4800_wdt.c
index c137ad2bd5c3..b07f75f9049b 100644
--- a/drivers/watchdog/ts4800_wdt.c
+++ b/drivers/watchdog/ts4800_wdt.c
@@ -200,4 +200,5 @@ module_platform_driver(ts4800_wdt_driver);
 
 MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:ts4800_wdt");
diff --git a/drivers/watchdog/ts72xx_wdt.c b/drivers/watchdog/ts72xx_wdt.c
index bf918f5fa131..efd74cfabbf8 100644
--- a/drivers/watchdog/ts72xx_wdt.c
+++ b/drivers/watchdog/ts72xx_wdt.c
@@ -174,4 +174,5 @@ module_platform_driver(ts72xx_wdt_driver);
 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
 MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:ts72xx-wdt");
diff --git a/drivers/watchdog/twl4030_wdt.c b/drivers/watchdog/twl4030_wdt.c
index 355e428c0b99..98bcebe3a0b7 100644
--- a/drivers/watchdog/twl4030_wdt.c
+++ b/drivers/watchdog/twl4030_wdt.c
@@ -123,5 +123,6 @@ module_platform_driver(twl4030_wdt_driver);
 
 MODULE_AUTHOR("Nokia Corporation");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:twl4030_wdt");
 
diff --git a/drivers/watchdog/uniphier_wdt.c b/drivers/watchdog/uniphier_wdt.c
index 8e9242c23022..3a6d2d3a8f3c 100644
--- a/drivers/watchdog/uniphier_wdt.c
+++ b/drivers/watchdog/uniphier_wdt.c
@@ -255,3 +255,4 @@ MODULE_PARM_DESC(nowayout,
 MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
 MODULE_DESCRIPTION("UniPhier Watchdog Device Driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/via_wdt.c b/drivers/watchdog/via_wdt.c
index eeb39f96e72e..899a4fe1d7b6 100644
--- a/drivers/watchdog/via_wdt.c
+++ b/drivers/watchdog/via_wdt.c
@@ -259,3 +259,4 @@ module_pci_driver(wdt_driver);
 MODULE_AUTHOR("Marc Vertes");
 MODULE_DESCRIPTION("Driver for watchdog timer on VIA chipset");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/w83627hf_wdt.c b/drivers/watchdog/w83627hf_wdt.c
index 38b31e9947aa..b66aea20a96e 100644
--- a/drivers/watchdog/w83627hf_wdt.c
+++ b/drivers/watchdog/w83627hf_wdt.c
@@ -528,5 +528,6 @@ module_init(wdt_init);
 module_exit(wdt_exit);
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_AUTHOR("Pádraig  Brady <P@draigBrady.com>");
 MODULE_DESCRIPTION("w83627hf/thf WDT driver");
diff --git a/drivers/watchdog/watchdog_core.c b/drivers/watchdog/watchdog_core.c
index 21e8085b848b..9deb22af5daa 100644
--- a/drivers/watchdog/watchdog_core.c
+++ b/drivers/watchdog/watchdog_core.c
@@ -145,7 +145,7 @@ int watchdog_init_timeout(struct watchdog_device *wdd,
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(watchdog_init_timeout);
+EXPORT_SYMBOL_NS_GPL(watchdog_init_timeout, WATCHDOG_CORE);
 
 static int watchdog_restart_notifier(struct notifier_block *nb,
 				     unsigned long action, void *data)
@@ -180,7 +180,7 @@ void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
 {
 	wdd->restart_nb.priority = priority;
 }
-EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
+EXPORT_SYMBOL_NS_GPL(watchdog_set_restart_priority, WATCHDOG_CORE);
 
 static int __watchdog_register_device(struct watchdog_device *wdd)
 {
@@ -279,7 +279,7 @@ int watchdog_register_device(struct watchdog_device *wdd)
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(watchdog_register_device);
+EXPORT_SYMBOL_NS_GPL(watchdog_register_device, WATCHDOG_CORE);
 
 static void __watchdog_unregister_device(struct watchdog_device *wdd)
 {
@@ -311,7 +311,7 @@ void watchdog_unregister_device(struct watchdog_device *wdd)
 	mutex_unlock(&wtd_deferred_reg_mutex);
 }
 
-EXPORT_SYMBOL_GPL(watchdog_unregister_device);
+EXPORT_SYMBOL_NS_GPL(watchdog_unregister_device, WATCHDOG_CORE);
 
 static void devm_watchdog_unregister_device(struct device *dev, void *res)
 {
@@ -348,7 +348,7 @@ int devm_watchdog_register_device(struct device *dev,
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
+EXPORT_SYMBOL_NS_GPL(devm_watchdog_register_device, WATCHDOG_CORE);
 
 static int __init watchdog_deferred_registration(void)
 {
diff --git a/drivers/watchdog/wdat_wdt.c b/drivers/watchdog/wdat_wdt.c
index e7cf41aa26c3..027339a557ce 100644
--- a/drivers/watchdog/wdat_wdt.c
+++ b/drivers/watchdog/wdat_wdt.c
@@ -523,4 +523,5 @@ module_platform_driver(wdat_wdt_driver);
 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
 MODULE_DESCRIPTION("ACPI Hardware Watchdog (WDAT) driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:wdat_wdt");
diff --git a/drivers/watchdog/wm831x_wdt.c b/drivers/watchdog/wm831x_wdt.c
index 030ce240620d..a17e2ac251a1 100644
--- a/drivers/watchdog/wm831x_wdt.c
+++ b/drivers/watchdog/wm831x_wdt.c
@@ -282,4 +282,5 @@ module_platform_driver(wm831x_wdt_driver);
 MODULE_AUTHOR("Mark Brown");
 MODULE_DESCRIPTION("WM831x Watchdog");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:wm831x-watchdog");
diff --git a/drivers/watchdog/wm8350_wdt.c b/drivers/watchdog/wm8350_wdt.c
index 33c62d51f00a..f81e661662e6 100644
--- a/drivers/watchdog/wm8350_wdt.c
+++ b/drivers/watchdog/wm8350_wdt.c
@@ -175,4 +175,5 @@ module_platform_driver(wm8350_wdt_driver);
 MODULE_AUTHOR("Mark Brown");
 MODULE_DESCRIPTION("WM8350 Watchdog");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
 MODULE_ALIAS("platform:wm8350-wdt");
diff --git a/drivers/watchdog/xen_wdt.c b/drivers/watchdog/xen_wdt.c
index b343f421dc72..8b21a4b51047 100644
--- a/drivers/watchdog/xen_wdt.c
+++ b/drivers/watchdog/xen_wdt.c
@@ -206,3 +206,4 @@ module_exit(xen_wdt_cleanup_module);
 MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
 MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
diff --git a/drivers/watchdog/ziirave_wdt.c b/drivers/watchdog/ziirave_wdt.c
index dec660c509b3..2d81c7b546f2 100644
--- a/drivers/watchdog/ziirave_wdt.c
+++ b/drivers/watchdog/ziirave_wdt.c
@@ -751,3 +751,4 @@ module_i2c_driver(ziirave_wdt_driver);
 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
 MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(WATCHDOG_CORE);
-- 
2.23.0.187.g17f5b7556c-goog


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

* Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-03 15:06   ` [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace Matthias Maennich
@ 2019-09-03 16:10     ` Guenter Roeck
  2019-09-04  8:45       ` Masahiro Yamada
  0 siblings, 1 reply; 105+ messages in thread
From: Guenter Roeck @ 2019-09-03 16:10 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, gregkh, jeyu, joel,
	lucas.de.marchi, maco, sspatil, will, yamada.masahiro,
	linux-kbuild, linux-modules, linux-usb, usb-storage,
	linux-watchdog

On Tue, Sep 03, 2019 at 04:06:38PM +0100, Matthias Maennich wrote:
> Modules using symbols from the WATCHDOG_CORE namespace are required to
> explicitly import the namespace. This patch was generated with the
> following steps and serves as a reference to use the symbol namespace
> feature:
> 
>  1) Use EXPORT_SYMBOL_NS* macros instead of EXPORT_SYMBOL* for symbols
>     in watchdog_core.c
>  2) make  (see warnings during modpost about missing imports)
>  3) make nsdeps
> 
> I used 'allmodconfig' for the above steps to ensure all occurrences are
> patched.
> 
> Defining DEFAULT_SYMBOL_NAMESPACE in the Makefile is not trivial in this
> case as not only watchdog_core is defined in drivers/watchdog/Makefile.
> Hence this patch uses the variant of using the EXPORT_SYMBOL_NS* macros
> to export into a different namespace.
> 
> An alternative to this patch would be a single definition line before
> any use of EXPORT_SYMBOL*:
>  #define DEFAULT_SYMBOL_NAMESPACE WATCHDOG_CORE
> 
> This patch serves as a reference on how to use the symbol namespaces.
> 
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Signed-off-by: Matthias Maennich <maennich@google.com>

As mentioned before, I am opposed to this set of changes. I don't see
the point of restricting the use of exported symbols in WATCHDOG_CORE.

Guenter

> ---
>  drivers/hwmon/ftsteutates.c         |  1 +
>  drivers/hwmon/sch56xx-common.c      |  1 +
>  drivers/rtc/rtc-abx80x.c            |  1 +
>  drivers/watchdog/armada_37xx_wdt.c  |  1 +
>  drivers/watchdog/asm9260_wdt.c      |  1 +
>  drivers/watchdog/aspeed_wdt.c       |  1 +
>  drivers/watchdog/at91sam9_wdt.c     |  1 +
>  drivers/watchdog/atlas7_wdt.c       |  1 +
>  drivers/watchdog/bcm2835_wdt.c      |  1 +
>  drivers/watchdog/bcm47xx_wdt.c      |  1 +
>  drivers/watchdog/bcm7038_wdt.c      |  1 +
>  drivers/watchdog/bcm_kona_wdt.c     |  1 +
>  drivers/watchdog/bd70528_wdt.c      |  1 +
>  drivers/watchdog/cadence_wdt.c      |  1 +
>  drivers/watchdog/da9052_wdt.c       |  1 +
>  drivers/watchdog/da9055_wdt.c       |  1 +
>  drivers/watchdog/da9062_wdt.c       |  1 +
>  drivers/watchdog/da9063_wdt.c       |  1 +
>  drivers/watchdog/davinci_wdt.c      |  1 +
>  drivers/watchdog/digicolor_wdt.c    |  1 +
>  drivers/watchdog/dw_wdt.c           |  1 +
>  drivers/watchdog/ebc-c384_wdt.c     |  1 +
>  drivers/watchdog/ep93xx_wdt.c       |  1 +
>  drivers/watchdog/ftwdt010_wdt.c     |  1 +
>  drivers/watchdog/gpio_wdt.c         |  1 +
>  drivers/watchdog/hpwdt.c            |  1 +
>  drivers/watchdog/i6300esb.c         |  1 +
>  drivers/watchdog/iTCO_wdt.c         |  1 +
>  drivers/watchdog/ie6xx_wdt.c        |  1 +
>  drivers/watchdog/imgpdc_wdt.c       |  1 +
>  drivers/watchdog/imx2_wdt.c         |  1 +
>  drivers/watchdog/intel-mid_wdt.c    |  1 +
>  drivers/watchdog/it87_wdt.c         |  1 +
>  drivers/watchdog/kempld_wdt.c       |  1 +
>  drivers/watchdog/lpc18xx_wdt.c      |  1 +
>  drivers/watchdog/max63xx_wdt.c      |  1 +
>  drivers/watchdog/max77620_wdt.c     |  1 +
>  drivers/watchdog/mei_wdt.c          |  1 +
>  drivers/watchdog/mena21_wdt.c       |  1 +
>  drivers/watchdog/menf21bmc_wdt.c    |  1 +
>  drivers/watchdog/menz69_wdt.c       |  1 +
>  drivers/watchdog/meson_gxbb_wdt.c   |  1 +
>  drivers/watchdog/meson_wdt.c        |  1 +
>  drivers/watchdog/mlx_wdt.c          |  1 +
>  drivers/watchdog/moxart_wdt.c       |  1 +
>  drivers/watchdog/mtk_wdt.c          |  1 +
>  drivers/watchdog/ni903x_wdt.c       |  1 +
>  drivers/watchdog/nic7018_wdt.c      |  1 +
>  drivers/watchdog/npcm_wdt.c         |  1 +
>  drivers/watchdog/of_xilinx_wdt.c    |  1 +
>  drivers/watchdog/omap_wdt.c         |  1 +
>  drivers/watchdog/pm8916_wdt.c       |  1 +
>  drivers/watchdog/qcom-wdt.c         |  1 +
>  drivers/watchdog/rave-sp-wdt.c      |  1 +
>  drivers/watchdog/renesas_wdt.c      |  1 +
>  drivers/watchdog/retu_wdt.c         |  1 +
>  drivers/watchdog/rn5t618_wdt.c      |  1 +
>  drivers/watchdog/rza_wdt.c          |  1 +
>  drivers/watchdog/s3c2410_wdt.c      |  1 +
>  drivers/watchdog/sama5d4_wdt.c      |  1 +
>  drivers/watchdog/sirfsoc_wdt.c      |  1 +
>  drivers/watchdog/softdog.c          |  1 +
>  drivers/watchdog/sp5100_tco.c       |  1 +
>  drivers/watchdog/sprd_wdt.c         |  1 +
>  drivers/watchdog/st_lpc_wdt.c       |  1 +
>  drivers/watchdog/stmp3xxx_rtc_wdt.c |  1 +
>  drivers/watchdog/stpmic1_wdt.c      |  1 +
>  drivers/watchdog/sunxi_wdt.c        |  1 +
>  drivers/watchdog/tangox_wdt.c       |  1 +
>  drivers/watchdog/tegra_wdt.c        |  1 +
>  drivers/watchdog/tqmx86_wdt.c       |  1 +
>  drivers/watchdog/ts4800_wdt.c       |  1 +
>  drivers/watchdog/ts72xx_wdt.c       |  1 +
>  drivers/watchdog/twl4030_wdt.c      |  1 +
>  drivers/watchdog/uniphier_wdt.c     |  1 +
>  drivers/watchdog/via_wdt.c          |  1 +
>  drivers/watchdog/w83627hf_wdt.c     |  1 +
>  drivers/watchdog/watchdog_core.c    | 10 +++++-----
>  drivers/watchdog/wdat_wdt.c         |  1 +
>  drivers/watchdog/wm831x_wdt.c       |  1 +
>  drivers/watchdog/wm8350_wdt.c       |  1 +
>  drivers/watchdog/xen_wdt.c          |  1 +
>  drivers/watchdog/ziirave_wdt.c      |  1 +
>  83 files changed, 87 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/hwmon/ftsteutates.c b/drivers/hwmon/ftsteutates.c
> index 371ce7745f5e..fa67f7c565be 100644
> --- a/drivers/hwmon/ftsteutates.c
> +++ b/drivers/hwmon/ftsteutates.c
> @@ -830,3 +830,4 @@ module_i2c_driver(fts_driver);
>  MODULE_AUTHOR("Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>");
>  MODULE_DESCRIPTION("FTS Teutates driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/hwmon/sch56xx-common.c b/drivers/hwmon/sch56xx-common.c
> index 6c84780e358e..06dd840f3bf7 100644
> --- a/drivers/hwmon/sch56xx-common.c
> +++ b/drivers/hwmon/sch56xx-common.c
> @@ -571,6 +571,7 @@ static void __exit sch56xx_exit(void)
>  MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
>  MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  
>  module_init(sch56xx_init);
>  module_exit(sch56xx_exit);
> diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c
> index 73830670a41f..3ff3b96d14d7 100644
> --- a/drivers/rtc/rtc-abx80x.c
> +++ b/drivers/rtc/rtc-abx80x.c
> @@ -880,3 +880,4 @@ MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
>  MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
>  MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/armada_37xx_wdt.c b/drivers/watchdog/armada_37xx_wdt.c
> index e5dcb26d85f0..00e153ae70eb 100644
> --- a/drivers/watchdog/armada_37xx_wdt.c
> +++ b/drivers/watchdog/armada_37xx_wdt.c
> @@ -370,4 +370,5 @@ MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");
>  MODULE_DESCRIPTION("Armada 37xx CPU Watchdog");
>  
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:armada_37xx_wdt");
> diff --git a/drivers/watchdog/asm9260_wdt.c b/drivers/watchdog/asm9260_wdt.c
> index 45047e514b8e..fd7289df2cb3 100644
> --- a/drivers/watchdog/asm9260_wdt.c
> +++ b/drivers/watchdog/asm9260_wdt.c
> @@ -374,3 +374,4 @@ module_platform_driver(asm9260_wdt_driver);
>  MODULE_DESCRIPTION("asm9260 WatchDog Timer Driver");
>  MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/aspeed_wdt.c b/drivers/watchdog/aspeed_wdt.c
> index cc71861e033a..2607158965d6 100644
> --- a/drivers/watchdog/aspeed_wdt.c
> +++ b/drivers/watchdog/aspeed_wdt.c
> @@ -334,3 +334,4 @@ module_exit(aspeed_wdt_exit);
>  
>  MODULE_DESCRIPTION("Aspeed Watchdog Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/at91sam9_wdt.c b/drivers/watchdog/at91sam9_wdt.c
> index 292b5a1ca831..e7aa31fe4336 100644
> --- a/drivers/watchdog/at91sam9_wdt.c
> +++ b/drivers/watchdog/at91sam9_wdt.c
> @@ -416,3 +416,4 @@ module_platform_driver_probe(at91wdt_driver, at91wdt_probe);
>  MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>");
>  MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/atlas7_wdt.c b/drivers/watchdog/atlas7_wdt.c
> index 9bfe650d802f..f67e6f101102 100644
> --- a/drivers/watchdog/atlas7_wdt.c
> +++ b/drivers/watchdog/atlas7_wdt.c
> @@ -218,4 +218,5 @@ module_platform_driver(atlas7_wdt_driver);
>  MODULE_DESCRIPTION("CSRatlas7 watchdog driver");
>  MODULE_AUTHOR("Guo Zeng <Guo.Zeng@csr.com>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:atlas7-wdt");
> diff --git a/drivers/watchdog/bcm2835_wdt.c b/drivers/watchdog/bcm2835_wdt.c
> index dec6ca019bea..2f41b8a6e2f6 100644
> --- a/drivers/watchdog/bcm2835_wdt.c
> +++ b/drivers/watchdog/bcm2835_wdt.c
> @@ -242,3 +242,4 @@ MODULE_ALIAS("platform:bcm2835-wdt");
>  MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
>  MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/bcm47xx_wdt.c b/drivers/watchdog/bcm47xx_wdt.c
> index 05425c1dfd4c..231009f01024 100644
> --- a/drivers/watchdog/bcm47xx_wdt.c
> +++ b/drivers/watchdog/bcm47xx_wdt.c
> @@ -241,3 +241,4 @@ MODULE_AUTHOR("Aleksandar Radovanovic");
>  MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
>  MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/bcm7038_wdt.c b/drivers/watchdog/bcm7038_wdt.c
> index 979caa18d3c8..e66184997775 100644
> --- a/drivers/watchdog/bcm7038_wdt.c
> +++ b/drivers/watchdog/bcm7038_wdt.c
> @@ -212,5 +212,6 @@ module_param(nowayout, bool, 0);
>  MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
>  	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
>  MODULE_AUTHOR("Justin Chen");
> diff --git a/drivers/watchdog/bcm_kona_wdt.c b/drivers/watchdog/bcm_kona_wdt.c
> index eb850a8d19df..5b5a6a76c1aa 100644
> --- a/drivers/watchdog/bcm_kona_wdt.c
> +++ b/drivers/watchdog/bcm_kona_wdt.c
> @@ -339,3 +339,4 @@ MODULE_ALIAS("platform:" BCM_KONA_WDT_NAME);
>  MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
>  MODULE_DESCRIPTION("Broadcom Kona Watchdog Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/bd70528_wdt.c b/drivers/watchdog/bd70528_wdt.c
> index b0152fef4fc7..790748a3a30a 100644
> --- a/drivers/watchdog/bd70528_wdt.c
> +++ b/drivers/watchdog/bd70528_wdt.c
> @@ -288,3 +288,4 @@ module_platform_driver(bd70528_wdt);
>  MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
>  MODULE_DESCRIPTION("BD70528 watchdog driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/cadence_wdt.c b/drivers/watchdog/cadence_wdt.c
> index f8d4e91d0383..a44f3140656e 100644
> --- a/drivers/watchdog/cadence_wdt.c
> +++ b/drivers/watchdog/cadence_wdt.c
> @@ -438,3 +438,4 @@ module_platform_driver(cdns_wdt_driver);
>  MODULE_AUTHOR("Xilinx, Inc.");
>  MODULE_DESCRIPTION("Watchdog driver for Cadence WDT");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/da9052_wdt.c b/drivers/watchdog/da9052_wdt.c
> index d708c091bf1b..a5ceea0503bb 100644
> --- a/drivers/watchdog/da9052_wdt.c
> +++ b/drivers/watchdog/da9052_wdt.c
> @@ -191,4 +191,5 @@ module_platform_driver(da9052_wdt_driver);
>  MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
>  MODULE_DESCRIPTION("DA9052 SM Device Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:da9052-watchdog");
> diff --git a/drivers/watchdog/da9055_wdt.c b/drivers/watchdog/da9055_wdt.c
> index 389a4bdd208c..05b075f2ea90 100644
> --- a/drivers/watchdog/da9055_wdt.c
> +++ b/drivers/watchdog/da9055_wdt.c
> @@ -166,4 +166,5 @@ module_platform_driver(da9055_wdt_driver);
>  MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
>  MODULE_DESCRIPTION("DA9055 watchdog");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:da9055-watchdog");
> diff --git a/drivers/watchdog/da9062_wdt.c b/drivers/watchdog/da9062_wdt.c
> index e149e66a6ea9..dfc2025d36ba 100644
> --- a/drivers/watchdog/da9062_wdt.c
> +++ b/drivers/watchdog/da9062_wdt.c
> @@ -232,4 +232,5 @@ module_platform_driver(da9062_wdt_driver);
>  MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
>  MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:da9062-watchdog");
> diff --git a/drivers/watchdog/da9063_wdt.c b/drivers/watchdog/da9063_wdt.c
> index 3d65e92a4e3f..ae0cb7011762 100644
> --- a/drivers/watchdog/da9063_wdt.c
> +++ b/drivers/watchdog/da9063_wdt.c
> @@ -239,4 +239,5 @@ module_platform_driver(da9063_wdt_driver);
>  MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>");
>  MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);
> diff --git a/drivers/watchdog/davinci_wdt.c b/drivers/watchdog/davinci_wdt.c
> index 2b3f3cd382ef..4fa01dfa4edb 100644
> --- a/drivers/watchdog/davinci_wdt.c
> +++ b/drivers/watchdog/davinci_wdt.c
> @@ -274,4 +274,5 @@ MODULE_PARM_DESC(heartbeat,
>  		 __MODULE_STRING(DEFAULT_HEARTBEAT));
>  
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:davinci-wdt");
> diff --git a/drivers/watchdog/digicolor_wdt.c b/drivers/watchdog/digicolor_wdt.c
> index 073d37867f47..ed86cf879dae 100644
> --- a/drivers/watchdog/digicolor_wdt.c
> +++ b/drivers/watchdog/digicolor_wdt.c
> @@ -161,3 +161,4 @@ module_platform_driver(dc_wdt_driver);
>  MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
>  MODULE_DESCRIPTION("Driver for Conexant Digicolor watchdog timer");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/dw_wdt.c b/drivers/watchdog/dw_wdt.c
> index fef7c61f5555..e98ccb38eb1d 100644
> --- a/drivers/watchdog/dw_wdt.c
> +++ b/drivers/watchdog/dw_wdt.c
> @@ -340,3 +340,4 @@ module_platform_driver(dw_wdt_driver);
>  MODULE_AUTHOR("Jamie Iles");
>  MODULE_DESCRIPTION("Synopsys DesignWare Watchdog Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/ebc-c384_wdt.c b/drivers/watchdog/ebc-c384_wdt.c
> index 8ef4b0df3855..7e4349472745 100644
> --- a/drivers/watchdog/ebc-c384_wdt.c
> +++ b/drivers/watchdog/ebc-c384_wdt.c
> @@ -139,4 +139,5 @@ module_exit(ebc_c384_wdt_exit);
>  MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
>  MODULE_DESCRIPTION("WinSystems EBC-C384 watchdog timer driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("isa:" MODULE_NAME);
> diff --git a/drivers/watchdog/ep93xx_wdt.c b/drivers/watchdog/ep93xx_wdt.c
> index 38e26f160b9a..edfa422896d7 100644
> --- a/drivers/watchdog/ep93xx_wdt.c
> +++ b/drivers/watchdog/ep93xx_wdt.c
> @@ -144,3 +144,4 @@ MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
>  MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
>  MODULE_DESCRIPTION("EP93xx Watchdog");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/ftwdt010_wdt.c b/drivers/watchdog/ftwdt010_wdt.c
> index 21dcc7765688..c19186bc00b6 100644
> --- a/drivers/watchdog/ftwdt010_wdt.c
> +++ b/drivers/watchdog/ftwdt010_wdt.c
> @@ -227,3 +227,4 @@ module_platform_driver(ftwdt010_wdt_driver);
>  MODULE_AUTHOR("Linus Walleij");
>  MODULE_DESCRIPTION("Watchdog driver for Faraday Technology FTWDT010");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/gpio_wdt.c b/drivers/watchdog/gpio_wdt.c
> index 0923201ce874..6a90a7ecadeb 100644
> --- a/drivers/watchdog/gpio_wdt.c
> +++ b/drivers/watchdog/gpio_wdt.c
> @@ -194,3 +194,4 @@ module_platform_driver(gpio_wdt_driver);
>  MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
>  MODULE_DESCRIPTION("GPIO Watchdog");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/hpwdt.c b/drivers/watchdog/hpwdt.c
> index 7d34bcf1c45b..fb4d228a0d7e 100644
> --- a/drivers/watchdog/hpwdt.c
> +++ b/drivers/watchdog/hpwdt.c
> @@ -386,6 +386,7 @@ static struct pci_driver hpwdt_driver = {
>  MODULE_AUTHOR("Tom Mingarelli");
>  MODULE_DESCRIPTION("hpe watchdog driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_VERSION(HPWDT_VERSION);
>  
>  module_param(soft_margin, int, 0);
> diff --git a/drivers/watchdog/i6300esb.c b/drivers/watchdog/i6300esb.c
> index a30835f547b3..700a8c6cae00 100644
> --- a/drivers/watchdog/i6300esb.c
> +++ b/drivers/watchdog/i6300esb.c
> @@ -351,3 +351,4 @@ module_pci_driver(esb_driver);
>  MODULE_AUTHOR("Ross Biro and David Härdeman");
>  MODULE_DESCRIPTION("Watchdog driver for Intel 6300ESB chipsets");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/iTCO_wdt.c b/drivers/watchdog/iTCO_wdt.c
> index c559f706ae7e..454d8ff3edc1 100644
> --- a/drivers/watchdog/iTCO_wdt.c
> +++ b/drivers/watchdog/iTCO_wdt.c
> @@ -632,4 +632,5 @@ MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
>  MODULE_DESCRIPTION("Intel TCO WatchDog Timer Driver");
>  MODULE_VERSION(DRV_VERSION);
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:" DRV_NAME);
> diff --git a/drivers/watchdog/ie6xx_wdt.c b/drivers/watchdog/ie6xx_wdt.c
> index 8f28993fab8b..76bcff28eaf9 100644
> --- a/drivers/watchdog/ie6xx_wdt.c
> +++ b/drivers/watchdog/ie6xx_wdt.c
> @@ -314,4 +314,5 @@ module_exit(ie6xx_wdt_exit);
>  MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
>  MODULE_DESCRIPTION("Intel Atom E6xx Watchdog Device Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:" DRIVER_NAME);
> diff --git a/drivers/watchdog/imgpdc_wdt.c b/drivers/watchdog/imgpdc_wdt.c
> index b57ff3787052..6fe0fd7c0719 100644
> --- a/drivers/watchdog/imgpdc_wdt.c
> +++ b/drivers/watchdog/imgpdc_wdt.c
> @@ -314,3 +314,4 @@ MODULE_AUTHOR("Jude Abraham <Jude.Abraham@imgtec.com>");
>  MODULE_AUTHOR("Naidu Tellapati <Naidu.Tellapati@imgtec.com>");
>  MODULE_DESCRIPTION("Imagination Technologies PDC Watchdog Timer Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/imx2_wdt.c b/drivers/watchdog/imx2_wdt.c
> index 32af3974e6bb..dc6d4132ec1f 100644
> --- a/drivers/watchdog/imx2_wdt.c
> +++ b/drivers/watchdog/imx2_wdt.c
> @@ -433,4 +433,5 @@ module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
>  MODULE_AUTHOR("Wolfram Sang");
>  MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:" DRIVER_NAME);
> diff --git a/drivers/watchdog/intel-mid_wdt.c b/drivers/watchdog/intel-mid_wdt.c
> index 2cdbd37c700c..b859fd6d263b 100644
> --- a/drivers/watchdog/intel-mid_wdt.c
> +++ b/drivers/watchdog/intel-mid_wdt.c
> @@ -181,3 +181,4 @@ module_platform_driver(mid_wdt_driver);
>  MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
>  MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/it87_wdt.c b/drivers/watchdog/it87_wdt.c
> index a4b71ebc8cab..610f5cdebf72 100644
> --- a/drivers/watchdog/it87_wdt.c
> +++ b/drivers/watchdog/it87_wdt.c
> @@ -354,3 +354,4 @@ module_exit(it87_wdt_exit);
>  MODULE_AUTHOR("Oliver Schuster");
>  MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/kempld_wdt.c b/drivers/watchdog/kempld_wdt.c
> index 40bd518ed873..20a40e221dbb 100644
> --- a/drivers/watchdog/kempld_wdt.c
> +++ b/drivers/watchdog/kempld_wdt.c
> @@ -550,3 +550,4 @@ module_platform_driver(kempld_wdt_driver);
>  MODULE_DESCRIPTION("KEM PLD Watchdog Driver");
>  MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/lpc18xx_wdt.c b/drivers/watchdog/lpc18xx_wdt.c
> index 78cf11c94941..4e34a4bd0b24 100644
> --- a/drivers/watchdog/lpc18xx_wdt.c
> +++ b/drivers/watchdog/lpc18xx_wdt.c
> @@ -316,3 +316,4 @@ module_platform_driver(lpc18xx_wdt_driver);
>  MODULE_AUTHOR("Ariel D'Alessandro <ariel@vanguardiasur.com.ar>");
>  MODULE_DESCRIPTION("NXP LPC18xx Watchdog Timer Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/max63xx_wdt.c b/drivers/watchdog/max63xx_wdt.c
> index 3a899628a834..053c9285adac 100644
> --- a/drivers/watchdog/max63xx_wdt.c
> +++ b/drivers/watchdog/max63xx_wdt.c
> @@ -284,3 +284,4 @@ MODULE_PARM_DESC(nodelay,
>  		 "(max6373/74 only, default=0)");
>  
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/max77620_wdt.c b/drivers/watchdog/max77620_wdt.c
> index be6a53c30002..304817d379a7 100644
> --- a/drivers/watchdog/max77620_wdt.c
> +++ b/drivers/watchdog/max77620_wdt.c
> @@ -209,3 +209,4 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
>  
>  MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/mei_wdt.c b/drivers/watchdog/mei_wdt.c
> index 5391bf3e6b11..5e03226cada1 100644
> --- a/drivers/watchdog/mei_wdt.c
> +++ b/drivers/watchdog/mei_wdt.c
> @@ -662,4 +662,5 @@ module_mei_cl_driver(mei_wdt_driver);
>  
>  MODULE_AUTHOR("Intel Corporation");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_DESCRIPTION("Device driver for Intel MEI iAMT watchdog");
> diff --git a/drivers/watchdog/mena21_wdt.c b/drivers/watchdog/mena21_wdt.c
> index 99d2359d5a8a..b759c66f1446 100644
> --- a/drivers/watchdog/mena21_wdt.c
> +++ b/drivers/watchdog/mena21_wdt.c
> @@ -225,4 +225,5 @@ module_platform_driver(a21_wdt_driver);
>  MODULE_AUTHOR("MEN Mikro Elektronik");
>  MODULE_DESCRIPTION("MEN A21 Watchdog");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:a21-watchdog");
> diff --git a/drivers/watchdog/menf21bmc_wdt.c b/drivers/watchdog/menf21bmc_wdt.c
> index 81ebdfc371f4..358881618e2f 100644
> --- a/drivers/watchdog/menf21bmc_wdt.c
> +++ b/drivers/watchdog/menf21bmc_wdt.c
> @@ -181,4 +181,5 @@ module_platform_driver(menf21bmc_wdt);
>  MODULE_DESCRIPTION("MEN 14F021P00 BMC Watchdog driver");
>  MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:menf21bmc_wdt");
> diff --git a/drivers/watchdog/menz69_wdt.c b/drivers/watchdog/menz69_wdt.c
> index ed18238c5407..d13fcfc0f59c 100644
> --- a/drivers/watchdog/menz69_wdt.c
> +++ b/drivers/watchdog/menz69_wdt.c
> @@ -167,4 +167,5 @@ module_mcb_driver(men_z069_driver);
>  
>  MODULE_AUTHOR("Johannes Thumshirn <jth@kernel.org>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("mcb:16z069");
> diff --git a/drivers/watchdog/meson_gxbb_wdt.c b/drivers/watchdog/meson_gxbb_wdt.c
> index d17c1a6ed723..f30f85380679 100644
> --- a/drivers/watchdog/meson_gxbb_wdt.c
> +++ b/drivers/watchdog/meson_gxbb_wdt.c
> @@ -204,3 +204,4 @@ module_platform_driver(meson_gxbb_wdt_driver);
>  MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
>  MODULE_DESCRIPTION("Amlogic Meson GXBB Watchdog timer driver");
>  MODULE_LICENSE("Dual BSD/GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/meson_wdt.c b/drivers/watchdog/meson_wdt.c
> index 459f3ae02c91..0d60bb5aa688 100644
> --- a/drivers/watchdog/meson_wdt.c
> +++ b/drivers/watchdog/meson_wdt.c
> @@ -228,5 +228,6 @@ MODULE_PARM_DESC(nowayout,
>  		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
>  
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
>  MODULE_DESCRIPTION("Meson Watchdog Timer Driver");
> diff --git a/drivers/watchdog/mlx_wdt.c b/drivers/watchdog/mlx_wdt.c
> index 03b9ac4b99af..2df48a6ed04b 100644
> --- a/drivers/watchdog/mlx_wdt.c
> +++ b/drivers/watchdog/mlx_wdt.c
> @@ -287,4 +287,5 @@ module_platform_driver(mlxreg_wdt_driver);
>  MODULE_AUTHOR("Michael Shych <michaelsh@mellanox.com>");
>  MODULE_DESCRIPTION("Mellanox watchdog driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:mlx-wdt");
> diff --git a/drivers/watchdog/moxart_wdt.c b/drivers/watchdog/moxart_wdt.c
> index 6340a1f5f471..02dbfe2accfa 100644
> --- a/drivers/watchdog/moxart_wdt.c
> +++ b/drivers/watchdog/moxart_wdt.c
> @@ -164,4 +164,5 @@ MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
>  
>  MODULE_DESCRIPTION("MOXART watchdog driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");
> diff --git a/drivers/watchdog/mtk_wdt.c b/drivers/watchdog/mtk_wdt.c
> index 9c3d0033260d..074c04324738 100644
> --- a/drivers/watchdog/mtk_wdt.c
> +++ b/drivers/watchdog/mtk_wdt.c
> @@ -247,6 +247,7 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
>  			__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
>  
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
>  MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
>  MODULE_VERSION(DRV_VERSION);
> diff --git a/drivers/watchdog/ni903x_wdt.c b/drivers/watchdog/ni903x_wdt.c
> index 4cebad324b20..724d47f2f325 100644
> --- a/drivers/watchdog/ni903x_wdt.c
> +++ b/drivers/watchdog/ni903x_wdt.c
> @@ -255,3 +255,4 @@ MODULE_DESCRIPTION("NI 903x Watchdog");
>  MODULE_AUTHOR("Jeff Westfahl <jeff.westfahl@ni.com>");
>  MODULE_AUTHOR("Kyle Roeschley <kyle.roeschley@ni.com>");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/nic7018_wdt.c b/drivers/watchdog/nic7018_wdt.c
> index 2a46cc662943..efe0f188c79a 100644
> --- a/drivers/watchdog/nic7018_wdt.c
> +++ b/drivers/watchdog/nic7018_wdt.c
> @@ -250,3 +250,4 @@ module_platform_driver(watchdog_driver);
>  MODULE_DESCRIPTION("National Instruments NIC7018 Watchdog driver");
>  MODULE_AUTHOR("Hui Chun Ong <hui.chun.ong@ni.com>");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/npcm_wdt.c b/drivers/watchdog/npcm_wdt.c
> index 9c773c3d6d5d..aa2022a44985 100644
> --- a/drivers/watchdog/npcm_wdt.c
> +++ b/drivers/watchdog/npcm_wdt.c
> @@ -248,3 +248,4 @@ module_platform_driver(npcm_wdt_driver);
>  MODULE_AUTHOR("Joel Stanley");
>  MODULE_DESCRIPTION("Watchdog driver for NPCM");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/of_xilinx_wdt.c b/drivers/watchdog/of_xilinx_wdt.c
> index 7fe4f7c3f7ce..8d93e2089c6d 100644
> --- a/drivers/watchdog/of_xilinx_wdt.c
> +++ b/drivers/watchdog/of_xilinx_wdt.c
> @@ -308,3 +308,4 @@ module_platform_driver(xwdt_driver);
>  MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
>  MODULE_DESCRIPTION("Xilinx Watchdog driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/omap_wdt.c b/drivers/watchdog/omap_wdt.c
> index 9b91882fe3c4..49565608924a 100644
> --- a/drivers/watchdog/omap_wdt.c
> +++ b/drivers/watchdog/omap_wdt.c
> @@ -375,4 +375,5 @@ module_platform_driver(omap_wdt_driver);
>  
>  MODULE_AUTHOR("George G. Davis");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:omap_wdt");
> diff --git a/drivers/watchdog/pm8916_wdt.c b/drivers/watchdog/pm8916_wdt.c
> index 2d3652004e39..f13c1c2bec07 100644
> --- a/drivers/watchdog/pm8916_wdt.c
> +++ b/drivers/watchdog/pm8916_wdt.c
> @@ -210,3 +210,4 @@ module_platform_driver(pm8916_wdt_driver);
>  MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
>  MODULE_DESCRIPTION("Qualcomm pm8916 watchdog driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/qcom-wdt.c b/drivers/watchdog/qcom-wdt.c
> index 7be7f87be28f..1ae02d3b5cb2 100644
> --- a/drivers/watchdog/qcom-wdt.c
> +++ b/drivers/watchdog/qcom-wdt.c
> @@ -272,3 +272,4 @@ module_platform_driver(qcom_watchdog_driver);
>  
>  MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/rave-sp-wdt.c b/drivers/watchdog/rave-sp-wdt.c
> index 2c95615b6354..7fac3fabfcfb 100644
> --- a/drivers/watchdog/rave-sp-wdt.c
> +++ b/drivers/watchdog/rave-sp-wdt.c
> @@ -329,6 +329,7 @@ module_platform_driver(rave_sp_wdt_driver);
>  
>  MODULE_DEVICE_TABLE(of, rave_sp_wdt_of_match);
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
>  MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
>  MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
> diff --git a/drivers/watchdog/renesas_wdt.c b/drivers/watchdog/renesas_wdt.c
> index 00662a8e039c..ec5b1ec6c292 100644
> --- a/drivers/watchdog/renesas_wdt.c
> +++ b/drivers/watchdog/renesas_wdt.c
> @@ -315,4 +315,5 @@ module_platform_driver(rwdt_driver);
>  
>  MODULE_DESCRIPTION("Renesas WDT Watchdog Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
> diff --git a/drivers/watchdog/retu_wdt.c b/drivers/watchdog/retu_wdt.c
> index 258dfcf9cbda..6a3e2b8e5226 100644
> --- a/drivers/watchdog/retu_wdt.c
> +++ b/drivers/watchdog/retu_wdt.c
> @@ -168,3 +168,4 @@ MODULE_DESCRIPTION("Retu watchdog");
>  MODULE_AUTHOR("Amit Kucheria");
>  MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/rn5t618_wdt.c b/drivers/watchdog/rn5t618_wdt.c
> index 234876047431..8f7bc6b1cb64 100644
> --- a/drivers/watchdog/rn5t618_wdt.c
> +++ b/drivers/watchdog/rn5t618_wdt.c
> @@ -191,3 +191,4 @@ module_platform_driver(rn5t618_wdt_driver);
>  MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
>  MODULE_DESCRIPTION("RN5T618 watchdog driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/rza_wdt.c b/drivers/watchdog/rza_wdt.c
> index 7b6c365f7cd3..31ff9acc9af2 100644
> --- a/drivers/watchdog/rza_wdt.c
> +++ b/drivers/watchdog/rza_wdt.c
> @@ -245,3 +245,4 @@ module_platform_driver(rza_wdt_driver);
>  MODULE_DESCRIPTION("Renesas RZ/A WDT Driver");
>  MODULE_AUTHOR("Chris Brandt <chris.brandt@renesas.com>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c
> index 2395f353e52d..5c1800d41411 100644
> --- a/drivers/watchdog/s3c2410_wdt.c
> +++ b/drivers/watchdog/s3c2410_wdt.c
> @@ -739,3 +739,4 @@ module_platform_driver(s3c2410wdt_driver);
>  MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
>  MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/sama5d4_wdt.c b/drivers/watchdog/sama5d4_wdt.c
> index d193a60430b2..725f68703f1e 100644
> --- a/drivers/watchdog/sama5d4_wdt.c
> +++ b/drivers/watchdog/sama5d4_wdt.c
> @@ -321,3 +321,4 @@ module_platform_driver(sama5d4_wdt_driver);
>  MODULE_AUTHOR("Atmel Corporation");
>  MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/sirfsoc_wdt.c b/drivers/watchdog/sirfsoc_wdt.c
> index 734cf2966ecb..bcb75d9978f0 100644
> --- a/drivers/watchdog/sirfsoc_wdt.c
> +++ b/drivers/watchdog/sirfsoc_wdt.c
> @@ -213,4 +213,5 @@ module_platform_driver(sirfsoc_wdt_driver);
>  MODULE_DESCRIPTION("SiRF SoC watchdog driver");
>  MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:sirfsoc-wdt");
> diff --git a/drivers/watchdog/softdog.c b/drivers/watchdog/softdog.c
> index 3e4885c1545e..8d50543f7340 100644
> --- a/drivers/watchdog/softdog.c
> +++ b/drivers/watchdog/softdog.c
> @@ -165,3 +165,4 @@ module_exit(softdog_exit);
>  MODULE_AUTHOR("Alan Cox");
>  MODULE_DESCRIPTION("Software Watchdog Device Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/sp5100_tco.c b/drivers/watchdog/sp5100_tco.c
> index 93bd302ae7c5..716b9c43baf1 100644
> --- a/drivers/watchdog/sp5100_tco.c
> +++ b/drivers/watchdog/sp5100_tco.c
> @@ -486,3 +486,4 @@ module_exit(sp5100_tco_exit);
>  MODULE_AUTHOR("Priyanka Gupta");
>  MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/sprd_wdt.c b/drivers/watchdog/sprd_wdt.c
> index edba4e278685..55e883e8dcf4 100644
> --- a/drivers/watchdog/sprd_wdt.c
> +++ b/drivers/watchdog/sprd_wdt.c
> @@ -384,3 +384,4 @@ module_platform_driver(sprd_watchdog_driver);
>  MODULE_AUTHOR("Eric Long <eric.long@spreadtrum.com>");
>  MODULE_DESCRIPTION("Spreadtrum Watchdog Timer Controller Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/st_lpc_wdt.c b/drivers/watchdog/st_lpc_wdt.c
> index 14ab6559c748..4a7e6b4694fa 100644
> --- a/drivers/watchdog/st_lpc_wdt.c
> +++ b/drivers/watchdog/st_lpc_wdt.c
> @@ -305,3 +305,4 @@ module_platform_driver(st_wdog_driver);
>  MODULE_AUTHOR("David Paris <david.paris@st.com>");
>  MODULE_DESCRIPTION("ST LPC Watchdog Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/stmp3xxx_rtc_wdt.c b/drivers/watchdog/stmp3xxx_rtc_wdt.c
> index 7caf3aa71c6a..f1dd033c2b4b 100644
> --- a/drivers/watchdog/stmp3xxx_rtc_wdt.c
> +++ b/drivers/watchdog/stmp3xxx_rtc_wdt.c
> @@ -150,4 +150,5 @@ module_platform_driver(stmp3xxx_wdt_driver);
>  
>  MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
> diff --git a/drivers/watchdog/stpmic1_wdt.c b/drivers/watchdog/stpmic1_wdt.c
> index 45d0c543466f..6cdbe4bf5aaa 100644
> --- a/drivers/watchdog/stpmic1_wdt.c
> +++ b/drivers/watchdog/stpmic1_wdt.c
> @@ -138,3 +138,4 @@ module_platform_driver(stpmic1_wdt_driver);
>  MODULE_DESCRIPTION("Watchdog driver for STPMIC1 device");
>  MODULE_AUTHOR("Pascal Paillet <p.paillet@st.com>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/sunxi_wdt.c b/drivers/watchdog/sunxi_wdt.c
> index 5f05a45ac187..dea10c79b817 100644
> --- a/drivers/watchdog/sunxi_wdt.c
> +++ b/drivers/watchdog/sunxi_wdt.c
> @@ -289,6 +289,7 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
>  		"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
>  
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
>  MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
>  MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
> diff --git a/drivers/watchdog/tangox_wdt.c b/drivers/watchdog/tangox_wdt.c
> index 1afb0e9d808c..bc9025d31977 100644
> --- a/drivers/watchdog/tangox_wdt.c
> +++ b/drivers/watchdog/tangox_wdt.c
> @@ -207,3 +207,4 @@ module_platform_driver(tangox_wdt_driver);
>  MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
>  MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/tegra_wdt.c b/drivers/watchdog/tegra_wdt.c
> index dfe06e506cad..06acd8251928 100644
> --- a/drivers/watchdog/tegra_wdt.c
> +++ b/drivers/watchdog/tegra_wdt.c
> @@ -276,3 +276,4 @@ module_platform_driver(tegra_wdt_driver);
>  MODULE_AUTHOR("NVIDIA Corporation");
>  MODULE_DESCRIPTION("Tegra Watchdog Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/tqmx86_wdt.c b/drivers/watchdog/tqmx86_wdt.c
> index 72d0b0adde38..35b8bfffa34d 100644
> --- a/drivers/watchdog/tqmx86_wdt.c
> +++ b/drivers/watchdog/tqmx86_wdt.c
> @@ -124,3 +124,4 @@ MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
>  MODULE_DESCRIPTION("TQMx86 Watchdog");
>  MODULE_ALIAS("platform:tqmx86-wdt");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/ts4800_wdt.c b/drivers/watchdog/ts4800_wdt.c
> index c137ad2bd5c3..b07f75f9049b 100644
> --- a/drivers/watchdog/ts4800_wdt.c
> +++ b/drivers/watchdog/ts4800_wdt.c
> @@ -200,4 +200,5 @@ module_platform_driver(ts4800_wdt_driver);
>  
>  MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:ts4800_wdt");
> diff --git a/drivers/watchdog/ts72xx_wdt.c b/drivers/watchdog/ts72xx_wdt.c
> index bf918f5fa131..efd74cfabbf8 100644
> --- a/drivers/watchdog/ts72xx_wdt.c
> +++ b/drivers/watchdog/ts72xx_wdt.c
> @@ -174,4 +174,5 @@ module_platform_driver(ts72xx_wdt_driver);
>  MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
>  MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:ts72xx-wdt");
> diff --git a/drivers/watchdog/twl4030_wdt.c b/drivers/watchdog/twl4030_wdt.c
> index 355e428c0b99..98bcebe3a0b7 100644
> --- a/drivers/watchdog/twl4030_wdt.c
> +++ b/drivers/watchdog/twl4030_wdt.c
> @@ -123,5 +123,6 @@ module_platform_driver(twl4030_wdt_driver);
>  
>  MODULE_AUTHOR("Nokia Corporation");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:twl4030_wdt");
>  
> diff --git a/drivers/watchdog/uniphier_wdt.c b/drivers/watchdog/uniphier_wdt.c
> index 8e9242c23022..3a6d2d3a8f3c 100644
> --- a/drivers/watchdog/uniphier_wdt.c
> +++ b/drivers/watchdog/uniphier_wdt.c
> @@ -255,3 +255,4 @@ MODULE_PARM_DESC(nowayout,
>  MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
>  MODULE_DESCRIPTION("UniPhier Watchdog Device Driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/via_wdt.c b/drivers/watchdog/via_wdt.c
> index eeb39f96e72e..899a4fe1d7b6 100644
> --- a/drivers/watchdog/via_wdt.c
> +++ b/drivers/watchdog/via_wdt.c
> @@ -259,3 +259,4 @@ module_pci_driver(wdt_driver);
>  MODULE_AUTHOR("Marc Vertes");
>  MODULE_DESCRIPTION("Driver for watchdog timer on VIA chipset");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/w83627hf_wdt.c b/drivers/watchdog/w83627hf_wdt.c
> index 38b31e9947aa..b66aea20a96e 100644
> --- a/drivers/watchdog/w83627hf_wdt.c
> +++ b/drivers/watchdog/w83627hf_wdt.c
> @@ -528,5 +528,6 @@ module_init(wdt_init);
>  module_exit(wdt_exit);
>  
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_AUTHOR("Pádraig  Brady <P@draigBrady.com>");
>  MODULE_DESCRIPTION("w83627hf/thf WDT driver");
> diff --git a/drivers/watchdog/watchdog_core.c b/drivers/watchdog/watchdog_core.c
> index 21e8085b848b..9deb22af5daa 100644
> --- a/drivers/watchdog/watchdog_core.c
> +++ b/drivers/watchdog/watchdog_core.c
> @@ -145,7 +145,7 @@ int watchdog_init_timeout(struct watchdog_device *wdd,
>  
>  	return ret;
>  }
> -EXPORT_SYMBOL_GPL(watchdog_init_timeout);
> +EXPORT_SYMBOL_NS_GPL(watchdog_init_timeout, WATCHDOG_CORE);
>  
>  static int watchdog_restart_notifier(struct notifier_block *nb,
>  				     unsigned long action, void *data)
> @@ -180,7 +180,7 @@ void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
>  {
>  	wdd->restart_nb.priority = priority;
>  }
> -EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
> +EXPORT_SYMBOL_NS_GPL(watchdog_set_restart_priority, WATCHDOG_CORE);
>  
>  static int __watchdog_register_device(struct watchdog_device *wdd)
>  {
> @@ -279,7 +279,7 @@ int watchdog_register_device(struct watchdog_device *wdd)
>  
>  	return ret;
>  }
> -EXPORT_SYMBOL_GPL(watchdog_register_device);
> +EXPORT_SYMBOL_NS_GPL(watchdog_register_device, WATCHDOG_CORE);
>  
>  static void __watchdog_unregister_device(struct watchdog_device *wdd)
>  {
> @@ -311,7 +311,7 @@ void watchdog_unregister_device(struct watchdog_device *wdd)
>  	mutex_unlock(&wtd_deferred_reg_mutex);
>  }
>  
> -EXPORT_SYMBOL_GPL(watchdog_unregister_device);
> +EXPORT_SYMBOL_NS_GPL(watchdog_unregister_device, WATCHDOG_CORE);
>  
>  static void devm_watchdog_unregister_device(struct device *dev, void *res)
>  {
> @@ -348,7 +348,7 @@ int devm_watchdog_register_device(struct device *dev,
>  
>  	return ret;
>  }
> -EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
> +EXPORT_SYMBOL_NS_GPL(devm_watchdog_register_device, WATCHDOG_CORE);
>  
>  static int __init watchdog_deferred_registration(void)
>  {
> diff --git a/drivers/watchdog/wdat_wdt.c b/drivers/watchdog/wdat_wdt.c
> index e7cf41aa26c3..027339a557ce 100644
> --- a/drivers/watchdog/wdat_wdt.c
> +++ b/drivers/watchdog/wdat_wdt.c
> @@ -523,4 +523,5 @@ module_platform_driver(wdat_wdt_driver);
>  MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
>  MODULE_DESCRIPTION("ACPI Hardware Watchdog (WDAT) driver");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:wdat_wdt");
> diff --git a/drivers/watchdog/wm831x_wdt.c b/drivers/watchdog/wm831x_wdt.c
> index 030ce240620d..a17e2ac251a1 100644
> --- a/drivers/watchdog/wm831x_wdt.c
> +++ b/drivers/watchdog/wm831x_wdt.c
> @@ -282,4 +282,5 @@ module_platform_driver(wm831x_wdt_driver);
>  MODULE_AUTHOR("Mark Brown");
>  MODULE_DESCRIPTION("WM831x Watchdog");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:wm831x-watchdog");
> diff --git a/drivers/watchdog/wm8350_wdt.c b/drivers/watchdog/wm8350_wdt.c
> index 33c62d51f00a..f81e661662e6 100644
> --- a/drivers/watchdog/wm8350_wdt.c
> +++ b/drivers/watchdog/wm8350_wdt.c
> @@ -175,4 +175,5 @@ module_platform_driver(wm8350_wdt_driver);
>  MODULE_AUTHOR("Mark Brown");
>  MODULE_DESCRIPTION("WM8350 Watchdog");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
>  MODULE_ALIAS("platform:wm8350-wdt");
> diff --git a/drivers/watchdog/xen_wdt.c b/drivers/watchdog/xen_wdt.c
> index b343f421dc72..8b21a4b51047 100644
> --- a/drivers/watchdog/xen_wdt.c
> +++ b/drivers/watchdog/xen_wdt.c
> @@ -206,3 +206,4 @@ module_exit(xen_wdt_cleanup_module);
>  MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
>  MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> diff --git a/drivers/watchdog/ziirave_wdt.c b/drivers/watchdog/ziirave_wdt.c
> index dec660c509b3..2d81c7b546f2 100644
> --- a/drivers/watchdog/ziirave_wdt.c
> +++ b/drivers/watchdog/ziirave_wdt.c
> @@ -751,3 +751,4 @@ module_i2c_driver(ziirave_wdt_driver);
>  MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
>  MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
>  MODULE_LICENSE("GPL");
> +MODULE_IMPORT_NS(WATCHDOG_CORE);
> -- 
> 2.23.0.187.g17f5b7556c-goog
> 

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

* Re: [PATCH v4 09/12] docs: Add documentation for Symbol Namespaces
  2019-09-03 15:06   ` [PATCH v4 09/12] docs: Add documentation for Symbol Namespaces Matthias Maennich
@ 2019-09-04  7:16     ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-09-04  7:16 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, linux-watchdog

On Tue, Sep 03, 2019 at 04:06:35PM +0100, Matthias Maennich wrote:
> Describe using Symbol Namespaces from a perspective of a user. I.e.
> module authors or subsystem maintainers.
> 
> Signed-off-by: Matthias Maennich <maennich@google.com>

Nice, it's good to have stuff to point people at.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-03 16:10     ` Guenter Roeck
@ 2019-09-04  8:45       ` Masahiro Yamada
  2019-09-04 12:12         ` Guenter Roeck
  0 siblings, 1 reply; 105+ messages in thread
From: Masahiro Yamada @ 2019-09-04  8:45 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Matthias Maennich, Linux Kernel Mailing List, Cc: Android Kernel,
	Arnd Bergmann, Greg Kroah-Hartman, Jessica Yu,
	Joel Fernandes (Google),
	Lucas De Marchi, maco, sspatil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb, usb-storage,
	linux-watchdog

On Wed, Sep 4, 2019 at 1:10 AM Guenter Roeck <linux@roeck-us.net> wrote:
>
> On Tue, Sep 03, 2019 at 04:06:38PM +0100, Matthias Maennich wrote:
> > Modules using symbols from the WATCHDOG_CORE namespace are required to
> > explicitly import the namespace. This patch was generated with the
> > following steps and serves as a reference to use the symbol namespace
> > feature:
> >
> >  1) Use EXPORT_SYMBOL_NS* macros instead of EXPORT_SYMBOL* for symbols
> >     in watchdog_core.c
> >  2) make  (see warnings during modpost about missing imports)
> >  3) make nsdeps
> >
> > I used 'allmodconfig' for the above steps to ensure all occurrences are
> > patched.
> >
> > Defining DEFAULT_SYMBOL_NAMESPACE in the Makefile is not trivial in this
> > case as not only watchdog_core is defined in drivers/watchdog/Makefile.
> > Hence this patch uses the variant of using the EXPORT_SYMBOL_NS* macros
> > to export into a different namespace.
> >
> > An alternative to this patch would be a single definition line before
> > any use of EXPORT_SYMBOL*:
> >  #define DEFAULT_SYMBOL_NAMESPACE WATCHDOG_CORE
> >
> > This patch serves as a reference on how to use the symbol namespaces.
> >
> > Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Signed-off-by: Matthias Maennich <maennich@google.com>
>
> As mentioned before, I am opposed to this set of changes. I don't see
> the point of restricting the use of exported symbols in WATCHDOG_CORE.
>
> Guenter


I agree.

I do not like this patch set either.

Anyway, the last two patches (usb-stroage, watchdog)
are useful to demonstrate
that this has a bad taste.


--
Best Regards
Masahiro Yamada

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

* Re: [PATCH v4 00/12] Symbol Namespaces
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
                     ` (11 preceding siblings ...)
  2019-09-03 15:06   ` [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace Matthias Maennich
@ 2019-09-04  9:37   ` Masahiro Yamada
  12 siblings, 0 replies; 105+ messages in thread
From: Masahiro Yamada @ 2019-09-04  9:37 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: Linux Kernel Mailing List, Cc: Android Kernel, Arnd Bergmann,
	Greg Kroah-Hartman, Jessica Yu, Joel Fernandes (Google),
	Lucas De Marchi, maco, sspatil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb, usb-storage,
	linux-watchdog

On Wed, Sep 4, 2019 at 12:07 AM Matthias Maennich <maennich@google.com> wrote:
>
> As of Linux 5.3-rc7, there are 31207 [1] exported symbols in the kernel.
> That is a growth of roughly 1000 symbols since 4.17 (30206 [2]). There
> seems to be some consensus amongst kernel devs that the export surface
> is too large, and hard to reason about.
>
> Generally, these symbols fall in one of these categories:
> 1) Symbols actually meant for drivers
> 2) Symbols that are only exported because functionality is split over
>    multiple modules, yet they really shouldn't be used by modules outside
>    of their own subsystem
> 3) Symbols really only meant for in-tree use
>
> When module developers try to upstream their code, it regularly turns
> out that they are using exported symbols that they really shouldn't be
> using. This problem is even bigger for drivers that are currently
> out-of-tree, which may be using many symbols that they shouldn't be
> using, and that break when those symbols are removed or modified.
>
> This patch allows subsystem maintainers to partition their exported
> symbols into separate namespaces, and module authors to import such
> namespaces only when needed.
>
> This allows subsystem maintainers to more easily limit availability of
> these namespaced symbols to other parts of the kernel. It can also be
> used to partition the set of exported symbols for documentation
> purposes; for example, a set of symbols that is really only used for
> debugging could be in a "SUBSYSTEM_DEBUG" namespace.


Why is the namespace helpful to limit the availability of symbols?
External modules would add whatever EXPORT_SYMBOL_NS() needed
to use the symbols.
Nothing would change except that it would increase the code size.




If we introduce the namespace, do symbols no longer
need to be globally unique?

Theoretically, we could do this:

EXPORT_SYMBOL_NS(foo, SUBSYSTEM_A);   (for Subsystem-A)
EXPORT_SYMBOL_NS(foo, SUBSYSTEM_B);   (for Subsystem-B)


But, I do not see benefit to do so.


We usually prefix symbol names with the sybsystem names
(usb_ ...  ,  watchdog_ ...).

By ensuring the symbol uniqueness,
it is really clear which functions are called.
You can easily grep the source code.
Editors can follow the code with tag files.

Adding namespace just introduces extra complexity.


Thanks.


>
> The series contains two RFC patches that do not need to be merged along
> with the rest of the series, but they serve as a reference for using the
> symbol namespaces. Especially, the watchdog subsystem might not be
> affected by the issues addressed by Symbol Namespaces. I left the patch
> in for reference anyway for demonstration purposes.
>
> I continued the work mainly done by Martijn Coenen.
>
> Changes in v2:
> - Rather than adding and evaluating separate sections __knsimport_NS,
>   use modinfo tags to declare the namespaces a module introduces.
>   Adjust modpost and the module loader accordingly.
> - Also add support for reading multiple modinfo values for the same tag
>   to allow list-like access to modinfo tags.
> - The macros in export.h have been cleaned up to avoid redundancy in the
>   macro parameters (ns, nspost, nspost2).
> - The introduction of relative references in the ksymtab entries caused
>   a rework of the macros to accommodate that configuration as well.
> - Alignment of kernel_symbol in the ksymtab needed to be fixed to allow
>   growing the kernel_symbol struct.
> - Modpost does now also append the namespace suffix to the symbol
>   entries in Module.symvers.
> - The configuration option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS allows
>   relaxing the enforcement of properly declared namespace imports at
>   module loading time.
> - Symbols can be collectively exported into a namespace by defining
>   DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile.
> - The requirement for a very recent coccinelle spatch has been lifted by
>   simplifying the script.
> - nsdeps does now ensures MODULE_IMPORT_NS statements are sorted when
>   patching the module source files.
> - Some minor bugs have been addressed in nsdeps to allow it to work with
>   modules that have more than one source file.
> - The RFC for the usb-storage symbols has been simplified by using
>   DEFAULT_SYMBOL_NAMESPACE=USB_STORAGE rather than explicitly exporting
>   each and every symbol into that new namespace.
>
> Changes in v3:
> - Reword the documentation for the
>   MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS option for clarification.
> - Fix printed required version of spatch in coccinelle script.
> - Adopt kbuild changes for modpost: .mod files are no longer generated
>   in .tmp_versions. Similarely, generate the .ns_deps files in the tree
>   along with the .mod files. Also, nsdeps now uses modules.order as
>   source for the list modules to consider.
> - Add an RFC patch to introduce the namespace WATCHDOG_CORE for symbols
>   exported in watchdog_core.c.
>
> Changes in v4:
> - scripts/nsdeps:
>   - exit on first error
>   - support out-of-tree builds O=...
> - scripts/mod/modpost: make the namespace a separate field when
>   exporting to Module.symvers (rather than symbol.NS)
> - scripts/export_report.pl: update for new Module.symvers format
> - include/linux/export.h: fixed style nits
> - kernel/module.c: ensure namespaces are imported before taking a
>   reference to the owner module
> - Documentation: document the Symbol Namespace feature and update
>   references to Module.symvers and EXPORT_SYMBOL*
>
> This patch series was developed against v5.3-rc7.
>
> [1] git grep "^EXPORT_SYMBOL\w*(" v5.3-rc7 | wc -l
> [2] git grep "^EXPORT_SYMBOL\w*(" v4.17    | wc -l
>
> Cc: arnd@arndb.de
> Cc: gregkh@linuxfoundation.org
> Cc: jeyu@kernel.org
> Cc: joel@joelfernandes.org
> Cc: lucas.de.marchi@gmail.com
> Cc: maco@android.com
> Cc: sspatil@google.com
> Cc: will@kernel.org
> Cc: yamada.masahiro@socionext.com
> Cc: linux-kbuild@vger.kernel.org
> Cc: linux-modules@vger.kernel.org
> Cc: linux-usb@vger.kernel.org
> Cc: usb-storage@lists.one-eyed-alien.net
> Cc: linux-watchdog@vger.kernel.org
>
>
> Matthias Maennich (12):
>   module: support reading multiple values per modinfo tag
>   export: explicitly align struct kernel_symbol
>   module: add support for symbol namespaces.
>   modpost: add support for symbol namespaces
>   module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
>   export: allow definition default namespaces in Makefiles or sources
>   modpost: add support for generating namespace dependencies
>   scripts: Coccinelle script for namespace dependencies.
>   docs: Add documentation for Symbol Namespaces
>   usb-storage: remove single-use define for debugging
>   RFC: usb-storage: export symbols in USB_STORAGE namespace
>   RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
>
>  .gitignore                                  |   1 +
>  Documentation/kbuild/modules.rst            |   7 +-
>  Documentation/kbuild/namespaces.rst         | 154 ++++++++++++++++++++
>  Documentation/kernel-hacking/hacking.rst    |  18 +++
>  MAINTAINERS                                 |   5 +
>  Makefile                                    |  14 +-
>  arch/m68k/include/asm/export.h              |   1 -
>  drivers/hwmon/ftsteutates.c                 |   1 +
>  drivers/hwmon/sch56xx-common.c              |   1 +
>  drivers/rtc/rtc-abx80x.c                    |   1 +
>  drivers/usb/storage/Makefile                |   2 +
>  drivers/usb/storage/alauda.c                |   1 +
>  drivers/usb/storage/cypress_atacb.c         |   1 +
>  drivers/usb/storage/datafab.c               |   1 +
>  drivers/usb/storage/debug.h                 |   2 -
>  drivers/usb/storage/ene_ub6250.c            |   1 +
>  drivers/usb/storage/freecom.c               |   1 +
>  drivers/usb/storage/isd200.c                |   1 +
>  drivers/usb/storage/jumpshot.c              |   1 +
>  drivers/usb/storage/karma.c                 |   1 +
>  drivers/usb/storage/onetouch.c              |   1 +
>  drivers/usb/storage/realtek_cr.c            |   1 +
>  drivers/usb/storage/scsiglue.c              |   2 +-
>  drivers/usb/storage/sddr09.c                |   1 +
>  drivers/usb/storage/sddr55.c                |   1 +
>  drivers/usb/storage/shuttle_usbat.c         |   1 +
>  drivers/usb/storage/uas.c                   |   1 +
>  drivers/watchdog/armada_37xx_wdt.c          |   1 +
>  drivers/watchdog/asm9260_wdt.c              |   1 +
>  drivers/watchdog/aspeed_wdt.c               |   1 +
>  drivers/watchdog/at91sam9_wdt.c             |   1 +
>  drivers/watchdog/atlas7_wdt.c               |   1 +
>  drivers/watchdog/bcm2835_wdt.c              |   1 +
>  drivers/watchdog/bcm47xx_wdt.c              |   1 +
>  drivers/watchdog/bcm7038_wdt.c              |   1 +
>  drivers/watchdog/bcm_kona_wdt.c             |   1 +
>  drivers/watchdog/bd70528_wdt.c              |   1 +
>  drivers/watchdog/cadence_wdt.c              |   1 +
>  drivers/watchdog/da9052_wdt.c               |   1 +
>  drivers/watchdog/da9055_wdt.c               |   1 +
>  drivers/watchdog/da9062_wdt.c               |   1 +
>  drivers/watchdog/da9063_wdt.c               |   1 +
>  drivers/watchdog/davinci_wdt.c              |   1 +
>  drivers/watchdog/digicolor_wdt.c            |   1 +
>  drivers/watchdog/dw_wdt.c                   |   1 +
>  drivers/watchdog/ebc-c384_wdt.c             |   1 +
>  drivers/watchdog/ep93xx_wdt.c               |   1 +
>  drivers/watchdog/ftwdt010_wdt.c             |   1 +
>  drivers/watchdog/gpio_wdt.c                 |   1 +
>  drivers/watchdog/hpwdt.c                    |   1 +
>  drivers/watchdog/i6300esb.c                 |   1 +
>  drivers/watchdog/iTCO_wdt.c                 |   1 +
>  drivers/watchdog/ie6xx_wdt.c                |   1 +
>  drivers/watchdog/imgpdc_wdt.c               |   1 +
>  drivers/watchdog/imx2_wdt.c                 |   1 +
>  drivers/watchdog/intel-mid_wdt.c            |   1 +
>  drivers/watchdog/it87_wdt.c                 |   1 +
>  drivers/watchdog/kempld_wdt.c               |   1 +
>  drivers/watchdog/lpc18xx_wdt.c              |   1 +
>  drivers/watchdog/max63xx_wdt.c              |   1 +
>  drivers/watchdog/max77620_wdt.c             |   1 +
>  drivers/watchdog/mei_wdt.c                  |   1 +
>  drivers/watchdog/mena21_wdt.c               |   1 +
>  drivers/watchdog/menf21bmc_wdt.c            |   1 +
>  drivers/watchdog/menz69_wdt.c               |   1 +
>  drivers/watchdog/meson_gxbb_wdt.c           |   1 +
>  drivers/watchdog/meson_wdt.c                |   1 +
>  drivers/watchdog/mlx_wdt.c                  |   1 +
>  drivers/watchdog/moxart_wdt.c               |   1 +
>  drivers/watchdog/mtk_wdt.c                  |   1 +
>  drivers/watchdog/ni903x_wdt.c               |   1 +
>  drivers/watchdog/nic7018_wdt.c              |   1 +
>  drivers/watchdog/npcm_wdt.c                 |   1 +
>  drivers/watchdog/of_xilinx_wdt.c            |   1 +
>  drivers/watchdog/omap_wdt.c                 |   1 +
>  drivers/watchdog/pm8916_wdt.c               |   1 +
>  drivers/watchdog/qcom-wdt.c                 |   1 +
>  drivers/watchdog/rave-sp-wdt.c              |   1 +
>  drivers/watchdog/renesas_wdt.c              |   1 +
>  drivers/watchdog/retu_wdt.c                 |   1 +
>  drivers/watchdog/rn5t618_wdt.c              |   1 +
>  drivers/watchdog/rza_wdt.c                  |   1 +
>  drivers/watchdog/s3c2410_wdt.c              |   1 +
>  drivers/watchdog/sama5d4_wdt.c              |   1 +
>  drivers/watchdog/sirfsoc_wdt.c              |   1 +
>  drivers/watchdog/softdog.c                  |   1 +
>  drivers/watchdog/sp5100_tco.c               |   1 +
>  drivers/watchdog/sprd_wdt.c                 |   1 +
>  drivers/watchdog/st_lpc_wdt.c               |   1 +
>  drivers/watchdog/stmp3xxx_rtc_wdt.c         |   1 +
>  drivers/watchdog/stpmic1_wdt.c              |   1 +
>  drivers/watchdog/sunxi_wdt.c                |   1 +
>  drivers/watchdog/tangox_wdt.c               |   1 +
>  drivers/watchdog/tegra_wdt.c                |   1 +
>  drivers/watchdog/tqmx86_wdt.c               |   1 +
>  drivers/watchdog/ts4800_wdt.c               |   1 +
>  drivers/watchdog/ts72xx_wdt.c               |   1 +
>  drivers/watchdog/twl4030_wdt.c              |   1 +
>  drivers/watchdog/uniphier_wdt.c             |   1 +
>  drivers/watchdog/via_wdt.c                  |   1 +
>  drivers/watchdog/w83627hf_wdt.c             |   1 +
>  drivers/watchdog/watchdog_core.c            |  10 +-
>  drivers/watchdog/wdat_wdt.c                 |   1 +
>  drivers/watchdog/wm831x_wdt.c               |   1 +
>  drivers/watchdog/wm8350_wdt.c               |   1 +
>  drivers/watchdog/xen_wdt.c                  |   1 +
>  drivers/watchdog/ziirave_wdt.c              |   1 +
>  include/asm-generic/export.h                |  14 +-
>  include/linux/export.h                      |  98 +++++++++++--
>  include/linux/module.h                      |   2 +
>  init/Kconfig                                |  13 ++
>  kernel/module.c                             |  67 ++++++++-
>  scripts/Makefile.modpost                    |   4 +-
>  scripts/coccinelle/misc/add_namespace.cocci |  23 +++
>  scripts/export_report.pl                    |   2 +-
>  scripts/mod/modpost.c                       | 150 ++++++++++++++++---
>  scripts/mod/modpost.h                       |   9 ++
>  scripts/nsdeps                              |  60 ++++++++
>  118 files changed, 697 insertions(+), 57 deletions(-)
>  create mode 100644 Documentation/kbuild/namespaces.rst
>  create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
>  create mode 100644 scripts/nsdeps
>
> --
> 2.23.0.187.g17f5b7556c-goog
>


-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH v4 08/12] scripts: Coccinelle script for namespace dependencies.
  2019-09-03 15:06   ` [PATCH v4 08/12] scripts: Coccinelle script for " Matthias Maennich
@ 2019-09-04  9:53     ` Masahiro Yamada
  2019-09-05 14:46       ` Matthias Maennich
  0 siblings, 1 reply; 105+ messages in thread
From: Masahiro Yamada @ 2019-09-04  9:53 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: Linux Kernel Mailing List, Cc: Android Kernel, Arnd Bergmann,
	Greg Kroah-Hartman, Jessica Yu, Joel Fernandes (Google),
	Lucas De Marchi, maco, sspatil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb, usb-storage,
	linux-watchdog, Julia Lawall

On Wed, Sep 4, 2019 at 12:07 AM Matthias Maennich <maennich@google.com> wrote:
>
> A script that uses the '<module>.ns_deps' files generated by modpost to
> automatically add the required symbol namespace dependencies to each
> module.
>
> Usage:
> 1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
>    DEFAULT_SYMBOL_NAMESPACE
> 2) Run 'make' (or 'make modules') and get warnings about modules not
>    importing that namespace.
> 3) Run 'make nsdeps' to automatically add required import statements
>    to said modules.
>
> This makes it easer for subsystem maintainers to introduce and maintain
> symbol namespaces into their codebase.
>
> Co-developed-by: Martijn Coenen <maco@android.com>
> Signed-off-by: Martijn Coenen <maco@android.com>
> Acked-by: Julia Lawall <julia.lawall@lip6.fr>
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Signed-off-by: Matthias Maennich <maennich@google.com>


Without any correct dependency,
this does not work.

$ make clean; make nsdeps
cat: modules.order: No such file or directory
cat: ./modules.order: No such file or directory


I do not see any point in the Makefile changes.



-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-04  8:45       ` Masahiro Yamada
@ 2019-09-04 12:12         ` Guenter Roeck
  2019-09-04 16:16           ` [usb-storage] " Matthew Dharm
  0 siblings, 1 reply; 105+ messages in thread
From: Guenter Roeck @ 2019-09-04 12:12 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Matthias Maennich, Linux Kernel Mailing List, Cc: Android Kernel,
	Arnd Bergmann, Greg Kroah-Hartman, Jessica Yu,
	Joel Fernandes (Google),
	Lucas De Marchi, maco, sspatil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb, usb-storage,
	linux-watchdog

On 9/4/19 1:45 AM, Masahiro Yamada wrote:
> On Wed, Sep 4, 2019 at 1:10 AM Guenter Roeck <linux@roeck-us.net> wrote:
>>
>> On Tue, Sep 03, 2019 at 04:06:38PM +0100, Matthias Maennich wrote:
>>> Modules using symbols from the WATCHDOG_CORE namespace are required to
>>> explicitly import the namespace. This patch was generated with the
>>> following steps and serves as a reference to use the symbol namespace
>>> feature:
>>>
>>>   1) Use EXPORT_SYMBOL_NS* macros instead of EXPORT_SYMBOL* for symbols
>>>      in watchdog_core.c
>>>   2) make  (see warnings during modpost about missing imports)
>>>   3) make nsdeps
>>>
>>> I used 'allmodconfig' for the above steps to ensure all occurrences are
>>> patched.
>>>
>>> Defining DEFAULT_SYMBOL_NAMESPACE in the Makefile is not trivial in this
>>> case as not only watchdog_core is defined in drivers/watchdog/Makefile.
>>> Hence this patch uses the variant of using the EXPORT_SYMBOL_NS* macros
>>> to export into a different namespace.
>>>
>>> An alternative to this patch would be a single definition line before
>>> any use of EXPORT_SYMBOL*:
>>>   #define DEFAULT_SYMBOL_NAMESPACE WATCHDOG_CORE
>>>
>>> This patch serves as a reference on how to use the symbol namespaces.
>>>
>>> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>> Signed-off-by: Matthias Maennich <maennich@google.com>
>>
>> As mentioned before, I am opposed to this set of changes. I don't see
>> the point of restricting the use of exported symbols in WATCHDOG_CORE.
>>
>> Guenter
> 
> 
> I agree.
> 
> I do not like this patch set either.
> 

Note that I don't object to the patch set in general. There may be symbols
which only need be exported in the context of a single subsystem or even
driver (if a driver consists of more than one module). For example, a mfd
driver may export symbols which should only be called by its client drivers.
In such a situation, it may well be beneficial to limit the use of exported
symbols.

I am not sure what good that does in practice (if I understand correctly,
a driver only has to declare that it wants to use a restricted use symbol
if it wants to use it), but that is a different question.

Guenter

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

* Re: [usb-storage] Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-04 12:12         ` Guenter Roeck
@ 2019-09-04 16:16           ` " Matthew Dharm
  2019-09-05 10:41             ` Jessica Yu
  0 siblings, 1 reply; 105+ messages in thread
From: Matthew Dharm @ 2019-09-04 16:16 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Masahiro Yamada, Matthias Maennich, Linux Kernel Mailing List,
	Cc: Android Kernel, Arnd Bergmann, Greg Kroah-Hartman,
	Jessica Yu, Joel Fernandes (Google),
	Lucas De Marchi, maco, sspatil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb,
	USB Mass Storage on Linux, linux-watchdog

On Wed, Sep 4, 2019 at 5:12 AM Guenter Roeck <linux@roeck-us.net> wrote:
>
> Note that I don't object to the patch set in general. There may be symbols
> which only need be exported in the context of a single subsystem or even
> driver (if a driver consists of more than one module). For example, a mfd
> driver may export symbols which should only be called by its client drivers.
> In such a situation, it may well be beneficial to limit the use of exported
> symbols.

I can appreciate this benefit.

> I am not sure what good that does in practice (if I understand correctly,
> a driver only has to declare that it wants to use a restricted use symbol
> if it wants to use it), but that is a different question.

I think this question implies that you are coming from the perspective
of "security" or wanting to restrict access to the underlying
functions, rather than wanting to clean-up the way symbols are handled
for manageability / maintainability purposes (which is the goal, as I
understand it).

HOWEVER, I have one question:  If these patches are included, and
someone wants to introduce a bit of code which needs to use two
symbols from different namespaces but with the same name, can that be
done?  That is, if driver A has symbol 'foo' and driver B has symbol
'foo' (both in their respective namespaces), and driver C wants to use
A.foo and B.foo, can that be supported?

Matt


-- 
Matthew Dharm
Former Maintainer, USB Mass Storage driver for Linux

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

* Re: [usb-storage] Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-04 16:16           ` [usb-storage] " Matthew Dharm
@ 2019-09-05 10:41             ` Jessica Yu
  2019-09-05 10:52               ` Arnd Bergmann
  2019-09-05 11:25               ` Matthias Maennich
  0 siblings, 2 replies; 105+ messages in thread
From: Jessica Yu @ 2019-09-05 10:41 UTC (permalink / raw)
  To: Matthew Dharm
  Cc: Guenter Roeck, Masahiro Yamada, Matthias Maennich,
	Linux Kernel Mailing List, Cc: Android Kernel, Arnd Bergmann,
	Greg Kroah-Hartman, Joel Fernandes (Google),
	Lucas De Marchi, maco, sspatil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb,
	USB Mass Storage on Linux, linux-watchdog

+++ Matthew Dharm [04/09/19 09:16 -0700]:
>On Wed, Sep 4, 2019 at 5:12 AM Guenter Roeck <linux@roeck-us.net> wrote:
>>
>> Note that I don't object to the patch set in general. There may be symbols
>> which only need be exported in the context of a single subsystem or even
>> driver (if a driver consists of more than one module). For example, a mfd
>> driver may export symbols which should only be called by its client drivers.
>> In such a situation, it may well be beneficial to limit the use of exported
>> symbols.
>
>I can appreciate this benefit.
>
>> I am not sure what good that does in practice (if I understand correctly,
>> a driver only has to declare that it wants to use a restricted use symbol
>> if it wants to use it), but that is a different question.
>
>I think this question implies that you are coming from the perspective
>of "security" or wanting to restrict access to the underlying
>functions, rather than wanting to clean-up the way symbols are handled
>for manageability / maintainability purposes (which is the goal, as I
>understand it).
>
>HOWEVER, I have one question:  If these patches are included, and
>someone wants to introduce a bit of code which needs to use two
>symbols from different namespaces but with the same name, can that be
>done?  That is, if driver A has symbol 'foo' and driver B has symbol
>'foo' (both in their respective namespaces), and driver C wants to use
>A.foo and B.foo, can that be supported?

As of now, we currently don't support this - modpost will warn if a
symbol is exported more than once (across modules + vmlinux), and the
module loader currently assumes exported symbol names are unique.  Do
you have a concrete use case? If there is a strong need for this, I
don't think it'd be too hard to implement.

Thanks,

Jessica


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

* Re: [usb-storage] Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-05 10:41             ` Jessica Yu
@ 2019-09-05 10:52               ` Arnd Bergmann
  2019-09-05 11:16                 ` Jessica Yu
  2019-09-05 11:25               ` Matthias Maennich
  1 sibling, 1 reply; 105+ messages in thread
From: Arnd Bergmann @ 2019-09-05 10:52 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Matthew Dharm, Guenter Roeck, Masahiro Yamada, Matthias Maennich,
	Linux Kernel Mailing List, Cc: Android Kernel,
	Greg Kroah-Hartman, Joel Fernandes (Google),
	Lucas De Marchi, Martijn Coenen, Sandeep Patil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb,
	USB Mass Storage on Linux, LINUXWATCHDOG

On Thu, Sep 5, 2019 at 12:41 PM Jessica Yu <jeyu@kernel.org> wrote:
> +++ Matthew Dharm [04/09/19 09:16 -0700]:
> >On Wed, Sep 4, 2019 at 5:12 AM Guenter Roeck <linux@roeck-us.net> wrote:

> >HOWEVER, I have one question:  If these patches are included, and
> >someone wants to introduce a bit of code which needs to use two
> >symbols from different namespaces but with the same name, can that be
> >done?  That is, if driver A has symbol 'foo' and driver B has symbol
> >'foo' (both in their respective namespaces), and driver C wants to use
> >A.foo and B.foo, can that be supported?
>
> As of now, we currently don't support this - modpost will warn if a
> symbol is exported more than once (across modules + vmlinux), and the
> module loader currently assumes exported symbol names are unique.  Do
> you have a concrete use case? If there is a strong need for this, I
> don't think it'd be too hard to implement.

I think what would prevent this from working in general is that having
two modules with the same exported symbol in different namespaces
won't link if you try to build both modules into the kernel itself.

      Arnd

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

* Re: [usb-storage] Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-05 10:52               ` Arnd Bergmann
@ 2019-09-05 11:16                 ` Jessica Yu
  2019-09-05 11:25                   ` Masahiro Yamada
  0 siblings, 1 reply; 105+ messages in thread
From: Jessica Yu @ 2019-09-05 11:16 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Matthew Dharm, Guenter Roeck, Masahiro Yamada, Matthias Maennich,
	Linux Kernel Mailing List, Cc: Android Kernel,
	Greg Kroah-Hartman, Joel Fernandes (Google),
	Lucas De Marchi, Martijn Coenen, Sandeep Patil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb,
	USB Mass Storage on Linux, LINUXWATCHDOG

+++ Arnd Bergmann [05/09/19 12:52 +0200]:
>On Thu, Sep 5, 2019 at 12:41 PM Jessica Yu <jeyu@kernel.org> wrote:
>> +++ Matthew Dharm [04/09/19 09:16 -0700]:
>> >On Wed, Sep 4, 2019 at 5:12 AM Guenter Roeck <linux@roeck-us.net> wrote:
>
>> >HOWEVER, I have one question:  If these patches are included, and
>> >someone wants to introduce a bit of code which needs to use two
>> >symbols from different namespaces but with the same name, can that be
>> >done?  That is, if driver A has symbol 'foo' and driver B has symbol
>> >'foo' (both in their respective namespaces), and driver C wants to use
>> >A.foo and B.foo, can that be supported?
>>
>> As of now, we currently don't support this - modpost will warn if a
>> symbol is exported more than once (across modules + vmlinux), and the
>> module loader currently assumes exported symbol names are unique.  Do
>> you have a concrete use case? If there is a strong need for this, I
>> don't think it'd be too hard to implement.
>
>I think what would prevent this from working in general is that having
>two modules with the same exported symbol in different namespaces
>won't link if you try to build both modules into the kernel itself.
>
>      Arnd

Ah yeah, you are right. I only tried building an identically named
exported symbol in a module and in the kernel, and there I got away
with a modpost warning. But this breaks when building the module into
the kernel, so I guess this is out of the question.

Thanks,

Jessica


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

* Re: [usb-storage] Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-05 11:16                 ` Jessica Yu
@ 2019-09-05 11:25                   ` Masahiro Yamada
  2019-09-05 12:00                     ` Greg Kroah-Hartman
  0 siblings, 1 reply; 105+ messages in thread
From: Masahiro Yamada @ 2019-09-05 11:25 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Arnd Bergmann, Matthew Dharm, Guenter Roeck, Matthias Maennich,
	Linux Kernel Mailing List, Cc: Android Kernel,
	Greg Kroah-Hartman, Joel Fernandes (Google),
	Lucas De Marchi, Martijn Coenen, Sandeep Patil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb,
	USB Mass Storage on Linux, LINUXWATCHDOG

On Thu, Sep 5, 2019 at 8:16 PM Jessica Yu <jeyu@kernel.org> wrote:
>
> +++ Arnd Bergmann [05/09/19 12:52 +0200]:
> >On Thu, Sep 5, 2019 at 12:41 PM Jessica Yu <jeyu@kernel.org> wrote:
> >> +++ Matthew Dharm [04/09/19 09:16 -0700]:
> >> >On Wed, Sep 4, 2019 at 5:12 AM Guenter Roeck <linux@roeck-us.net> wrote:
> >
> >> >HOWEVER, I have one question:  If these patches are included, and
> >> >someone wants to introduce a bit of code which needs to use two
> >> >symbols from different namespaces but with the same name, can that be
> >> >done?  That is, if driver A has symbol 'foo' and driver B has symbol
> >> >'foo' (both in their respective namespaces), and driver C wants to use
> >> >A.foo and B.foo, can that be supported?
> >>
> >> As of now, we currently don't support this - modpost will warn if a
> >> symbol is exported more than once (across modules + vmlinux), and the
> >> module loader currently assumes exported symbol names are unique.  Do
> >> you have a concrete use case? If there is a strong need for this, I
> >> don't think it'd be too hard to implement.
> >
> >I think what would prevent this from working in general is that having
> >two modules with the same exported symbol in different namespaces
> >won't link if you try to build both modules into the kernel itself.
> >
> >      Arnd
>
> Ah yeah, you are right. I only tried building an identically named
> exported symbol in a module and in the kernel, and there I got away
> with a modpost warning. But this breaks when building the module into
> the kernel, so I guess this is out of the question.
>
> Thanks,
>
> Jessica
>


The cover letter starts with
"As of Linux 5.3-rc7, there are 31207 [1] exported symbols in the kernel".

Whether or not we apply this patch set,
we will have to carefully maintain them
so that 31207 symbols are unique, anyway.
(And, we can do this with allmodconfig + modpost)

So, what is the point of the namespace,
where it does not loosen the scope of uniqueness?



--
Best Regards
Masahiro Yamada

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

* Re: [usb-storage] Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-05 10:41             ` Jessica Yu
  2019-09-05 10:52               ` Arnd Bergmann
@ 2019-09-05 11:25               ` Matthias Maennich
  1 sibling, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-05 11:25 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Matthew Dharm, Guenter Roeck, Masahiro Yamada,
	Linux Kernel Mailing List, Android Kernel Team, Arnd Bergmann,
	Greg Kroah-Hartman, Joel Fernandes (Google),
	Lucas De Marchi, maco, sspatil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb,
	USB Mass Storage on Linux, linux-watchdog

On Thu, Sep 05, 2019 at 12:41:47PM +0200, Jessica Yu wrote:
>+++ Matthew Dharm [04/09/19 09:16 -0700]:
>>On Wed, Sep 4, 2019 at 5:12 AM Guenter Roeck <linux@roeck-us.net> wrote:
>>>
>>>Note that I don't object to the patch set in general. There may be symbols
>>>which only need be exported in the context of a single subsystem or even
>>>driver (if a driver consists of more than one module). For example, a mfd
>>>driver may export symbols which should only be called by its client drivers.
>>>In such a situation, it may well be beneficial to limit the use of exported
>>>symbols.
>>
>>I can appreciate this benefit.
>>
>>>I am not sure what good that does in practice (if I understand correctly,
>>>a driver only has to declare that it wants to use a restricted use symbol
>>>if it wants to use it), but that is a different question.
>>
>>I think this question implies that you are coming from the perspective
>>of "security" or wanting to restrict access to the underlying
>>functions, rather than wanting to clean-up the way symbols are handled
>>for manageability / maintainability purposes (which is the goal, as I
>>understand it).

The goal of this patch set is to introduce structure into the exported
surface that goes beyond naming conventions like 'usb_*'. So, it is
rather about maintainability then security. In particular, creating the
visibility of which parts of the kernel use which other parts, might
help to find cases where suboptimal choices were made. Maybe already
during development/review.

As Guenter correctly noted, a module is able to declare that it wants to
use a namespace. One idea that came up earlier was to maybe restrict the
namespaces that can actually be imported by modules. But I would see
anything in that direction as beyond the scope of this series.

A nice side effect of having to declare the usage is that it shows up in
modinfo and module users can reason about how the module interacts with
the rest of the kernel.


>>HOWEVER, I have one question:  If these patches are included, and
>>someone wants to introduce a bit of code which needs to use two
>>symbols from different namespaces but with the same name, can that be
>>done?  That is, if driver A has symbol 'foo' and driver B has symbol
>>'foo' (both in their respective namespaces), and driver C wants to use
>>A.foo and B.foo, can that be supported?
>
>As of now, we currently don't support this - modpost will warn if a
>symbol is exported more than once (across modules + vmlinux), and the
>module loader currently assumes exported symbol names are unique.  Do
>you have a concrete use case? If there is a strong need for this, I
>don't think it'd be too hard to implement.

The implementation does not change the fact that symbol names need to be
unique. As Arnd just mentioned in the other thread: the linker will
already fail if two builtin symbols use the same name. It is rather a
tag attached to the symbol.

Cheers,
Matthias

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

* Re: [usb-storage] Re: [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace
  2019-09-05 11:25                   ` Masahiro Yamada
@ 2019-09-05 12:00                     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 105+ messages in thread
From: Greg Kroah-Hartman @ 2019-09-05 12:00 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Jessica Yu, Arnd Bergmann, Matthew Dharm, Guenter Roeck,
	Matthias Maennich, Linux Kernel Mailing List, Cc: Android Kernel,
	Joel Fernandes (Google),
	Lucas De Marchi, Martijn Coenen, Sandeep Patil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb,
	USB Mass Storage on Linux, LINUXWATCHDOG

On Thu, Sep 05, 2019 at 08:25:32PM +0900, Masahiro Yamada wrote:
> On Thu, Sep 5, 2019 at 8:16 PM Jessica Yu <jeyu@kernel.org> wrote:
> >
> > +++ Arnd Bergmann [05/09/19 12:52 +0200]:
> > >On Thu, Sep 5, 2019 at 12:41 PM Jessica Yu <jeyu@kernel.org> wrote:
> > >> +++ Matthew Dharm [04/09/19 09:16 -0700]:
> > >> >On Wed, Sep 4, 2019 at 5:12 AM Guenter Roeck <linux@roeck-us.net> wrote:
> > >
> > >> >HOWEVER, I have one question:  If these patches are included, and
> > >> >someone wants to introduce a bit of code which needs to use two
> > >> >symbols from different namespaces but with the same name, can that be
> > >> >done?  That is, if driver A has symbol 'foo' and driver B has symbol
> > >> >'foo' (both in their respective namespaces), and driver C wants to use
> > >> >A.foo and B.foo, can that be supported?
> > >>
> > >> As of now, we currently don't support this - modpost will warn if a
> > >> symbol is exported more than once (across modules + vmlinux), and the
> > >> module loader currently assumes exported symbol names are unique.  Do
> > >> you have a concrete use case? If there is a strong need for this, I
> > >> don't think it'd be too hard to implement.
> > >
> > >I think what would prevent this from working in general is that having
> > >two modules with the same exported symbol in different namespaces
> > >won't link if you try to build both modules into the kernel itself.
> > >
> > >      Arnd
> >
> > Ah yeah, you are right. I only tried building an identically named
> > exported symbol in a module and in the kernel, and there I got away
> > with a modpost warning. But this breaks when building the module into
> > the kernel, so I guess this is out of the question.
> >
> > Thanks,
> >
> > Jessica
> >
> 
> 
> The cover letter starts with
> "As of Linux 5.3-rc7, there are 31207 [1] exported symbols in the kernel".
> 
> Whether or not we apply this patch set,
> we will have to carefully maintain them
> so that 31207 symbols are unique, anyway.
> (And, we can do this with allmodconfig + modpost)
> 
> So, what is the point of the namespace,
> where it does not loosen the scope of uniqueness?

It does not "loosen" anything.  The problem is, we have 31207 exported
symbols and no one has any idea how they are "clustered" :)

Ideally we would have separate namespaces where we could "keep" others
from accessing symbols that are required to be global, but we do not
want anyone else to use them.  But we have C, and we need to live with
that.

This is the "next best thing".  This provides a namespace and a marking
by the user of that exported symbol that they really do know what they
are doing.

The USB storage exports are a concrete example of this in this patchset.
Those symbols are to only be used by other USB storage drivers, and they
are marked that way here.

This also gives us the ability to start to actually understand what our
different exported symbols are for.  Right now the USB core exports a
ton of stuff, but who is expected to use what?  Right now any USB driver
can use any of those functions and no one notices.  After this patchset
is merged, I will start to "carve up" the USB exports into different
"namespaces".  One example will be the functions only for use by USB
host controller drivers.  Then if someone submits a new driver that says
"use the USB host controller driver namespace", and that driver is _not_
a USB host controller driver, it's a huge red flag that something really
isn't correct here.

Some other USB functions will probably get marked "USB core only" or
some such thing, and then to use them you have to say you are importing
from the "usb core only" which again, is a huge flag that you are doing
something wrong.

So on it's own, this patchset doesn't do much.  But it gives us the
building blocks on which to start to make sense of those 31000+
different exported symbols.

As an aside, this is something that I know I and others have been
wanting to see happen for 10+ years now, and I'm very happy to see it
show up in a workable form as we have known this is a issue for a very
long time.

Hope this helps explain things better,

greg k-h

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

* Re: [PATCH v4 08/12] scripts: Coccinelle script for namespace dependencies.
  2019-09-04  9:53     ` Masahiro Yamada
@ 2019-09-05 14:46       ` Matthias Maennich
  0 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-05 14:46 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Linux Kernel Mailing List, Cc: Android Kernel, Arnd Bergmann,
	Greg Kroah-Hartman, Jessica Yu, Joel Fernandes (Google),
	Lucas De Marchi, maco, sspatil, Will Deacon,
	Linux Kbuild mailing list, linux-modules, linux-usb, usb-storage,
	linux-watchdog, Julia Lawall

On Wed, Sep 04, 2019 at 06:53:25PM +0900, Masahiro Yamada wrote:
>On Wed, Sep 4, 2019 at 12:07 AM Matthias Maennich <maennich@google.com> wrote:
>>
>> A script that uses the '<module>.ns_deps' files generated by modpost to
>> automatically add the required symbol namespace dependencies to each
>> module.
>>
>> Usage:
>> 1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
>>    DEFAULT_SYMBOL_NAMESPACE
>> 2) Run 'make' (or 'make modules') and get warnings about modules not
>>    importing that namespace.
>> 3) Run 'make nsdeps' to automatically add required import statements
>>    to said modules.
>>
>> This makes it easer for subsystem maintainers to introduce and maintain
>> symbol namespaces into their codebase.
>>
>> Co-developed-by: Martijn Coenen <maco@android.com>
>> Signed-off-by: Martijn Coenen <maco@android.com>
>> Acked-by: Julia Lawall <julia.lawall@lip6.fr>
>> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>> Signed-off-by: Matthias Maennich <maennich@google.com>
>
>
>Without any correct dependency,
>this does not work.

You are right, 'nsdeps' needs to depend on 'modules' to make that work.
I will fix that in the next version.

>$ make clean; make nsdeps
>cat: modules.order: No such file or directory
>cat: ./modules.order: No such file or directory
>
>
>I do not see any point in the Makefile changes.

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

* [PATCH v5 00/11] Symbol Namespaces
       [not found] <20180716122125.175792-1-maco@android.com>
  2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
  2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
@ 2019-09-06 10:32 ` " Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 01/11] module: support reading multiple values per modinfo tag Matthias Maennich
                     ` (11 more replies)
  2 siblings, 12 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

As of Linux 5.3-rc7, there are 31207 [1] exported symbols in the kernel.
That is a growth of roughly 1000 symbols since 4.17 (30206 [2]). There
seems to be some consensus amongst kernel devs that the export surface
is too large, and hard to reason about.

Generally, these symbols fall in one of these categories:
1) Symbols actually meant for drivers
2) Symbols that are only exported because functionality is split over
   multiple modules, yet they really shouldn't be used by modules outside
   of their own subsystem
3) Symbols really only meant for in-tree use

When module developers try to upstream their code, it regularly turns
out that they are using exported symbols that they really shouldn't be
using. This problem is even bigger for drivers that are currently
out-of-tree, which may be using many symbols that they shouldn't be
using, and that break when those symbols are removed or modified.

This patch allows subsystem maintainers to partition their exported
symbols into separate namespaces, and module authors to import such
namespaces only when needed.

This allows subsystem maintainers to more easily limit availability of
these namespaced symbols to other parts of the kernel. It can also be
used to partition the set of exported symbols for documentation
purposes; for example, a set of symbols that is really only used for
debugging could be in a "SUBSYSTEM_DEBUG" namespace.

I continued the work mainly done by Martijn Coenen.

Changes in v2:
- Rather than adding and evaluating separate sections __knsimport_NS,
  use modinfo tags to declare the namespaces a module introduces.
  Adjust modpost and the module loader accordingly.
- Also add support for reading multiple modinfo values for the same tag
  to allow list-like access to modinfo tags.
- The macros in export.h have been cleaned up to avoid redundancy in the
  macro parameters (ns, nspost, nspost2).
- The introduction of relative references in the ksymtab entries caused
  a rework of the macros to accommodate that configuration as well.
- Alignment of kernel_symbol in the ksymtab needed to be fixed to allow
  growing the kernel_symbol struct.
- Modpost does now also append the namespace suffix to the symbol
  entries in Module.symvers.
- The configuration option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS allows
  relaxing the enforcement of properly declared namespace imports at
  module loading time.
- Symbols can be collectively exported into a namespace by defining
  DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile.
- The requirement for a very recent coccinelle spatch has been lifted by
  simplifying the script.
- nsdeps does now ensures MODULE_IMPORT_NS statements are sorted when
  patching the module source files.
- Some minor bugs have been addressed in nsdeps to allow it to work with
  modules that have more than one source file.
- The RFC for the usb-storage symbols has been simplified by using
  DEFAULT_SYMBOL_NAMESPACE=USB_STORAGE rather than explicitly exporting
  each and every symbol into that new namespace.

Changes in v3:
- Reword the documentation for the
  MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS option for clarification.
- Fix printed required version of spatch in coccinelle script.
- Adopt kbuild changes for modpost: .mod files are no longer generated
  in .tmp_versions. Similarely, generate the .ns_deps files in the tree
  along with the .mod files. Also, nsdeps now uses modules.order as
  source for the list modules to consider.
- Add an RFC patch to introduce the namespace WATCHDOG_CORE for symbols
  exported in watchdog_core.c.

Changes in v4:
- scripts/nsdeps:
  - exit on first error
  - support out-of-tree builds O=...
- scripts/export_report.pl: update for new Module.symvers format
- scripts/mod/modpost: make the namespace a separate field when
  exporting to Module.symvers (rather than symbol.NS)
- include/linux/export.h: fixed style nits
- kernel/module.c: ensure namespaces are imported before taking a
  reference to the owner module
- Documentation: document the Symbol Namespace feature and update
  references to Module.symvers and EXPORT_SYMBOL*

Changes in v5:
- Makefile: let 'nsdeps' depend on 'modules' to allow
  `make clean; make nsdeps` to work
- scripts/nsdeps: drop 'exit on first error' again as it just makes more
  problems than it solves
- drop the watchdog RFC patch for now

This patch series was developed against v5.3-rc7.

[1] git grep "^EXPORT_SYMBOL\w*(" v5.3-rc7 | wc -l
[2] git grep "^EXPORT_SYMBOL\w*(" v4.17    | wc -l

Cc: arnd@arndb.de
Cc: gregkh@linuxfoundation.org
Cc: jeyu@kernel.org
Cc: joel@joelfernandes.org
Cc: lucas.de.marchi@gmail.com
Cc: maco@android.com
Cc: sspatil@google.com
Cc: will@kernel.org
Cc: yamada.masahiro@socionext.com
Cc: linux-kbuild@vger.kernel.org
Cc: linux-modules@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: usb-storage@lists.one-eyed-alien.net
Cc: kernel-team@android.com

Matthias Maennich (11):
  module: support reading multiple values per modinfo tag
  export: explicitly align struct kernel_symbol
  module: add support for symbol namespaces.
  modpost: add support for symbol namespaces
  module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  export: allow definition default namespaces in Makefiles or sources
  modpost: add support for generating namespace dependencies
  scripts: Coccinelle script for namespace dependencies.
  docs: Add documentation for Symbol Namespaces
  usb-storage: remove single-use define for debugging
  usb-storage: export symbols in USB_STORAGE namespace

 .gitignore                                  |   1 +
 Documentation/kbuild/modules.rst            |   7 +-
 Documentation/kbuild/namespaces.rst         | 154 ++++++++++++++++++++
 Documentation/kernel-hacking/hacking.rst    |  18 +++
 MAINTAINERS                                 |   5 +
 Makefile                                    |  14 +-
 arch/m68k/include/asm/export.h              |   1 -
 drivers/usb/storage/Makefile                |   2 +
 drivers/usb/storage/alauda.c                |   1 +
 drivers/usb/storage/cypress_atacb.c         |   1 +
 drivers/usb/storage/datafab.c               |   1 +
 drivers/usb/storage/debug.h                 |   2 -
 drivers/usb/storage/ene_ub6250.c            |   1 +
 drivers/usb/storage/freecom.c               |   1 +
 drivers/usb/storage/isd200.c                |   1 +
 drivers/usb/storage/jumpshot.c              |   1 +
 drivers/usb/storage/karma.c                 |   1 +
 drivers/usb/storage/onetouch.c              |   1 +
 drivers/usb/storage/realtek_cr.c            |   1 +
 drivers/usb/storage/scsiglue.c              |   2 +-
 drivers/usb/storage/sddr09.c                |   1 +
 drivers/usb/storage/sddr55.c                |   1 +
 drivers/usb/storage/shuttle_usbat.c         |   1 +
 drivers/usb/storage/uas.c                   |   1 +
 include/asm-generic/export.h                |  14 +-
 include/linux/export.h                      |  98 +++++++++++--
 include/linux/module.h                      |   2 +
 init/Kconfig                                |  13 ++
 kernel/module.c                             |  67 ++++++++-
 scripts/Makefile.modpost                    |   4 +-
 scripts/coccinelle/misc/add_namespace.cocci |  23 +++
 scripts/export_report.pl                    |   2 +-
 scripts/mod/modpost.c                       | 150 ++++++++++++++++---
 scripts/mod/modpost.h                       |   9 ++
 scripts/nsdeps                              |  58 ++++++++
 35 files changed, 608 insertions(+), 52 deletions(-)
 create mode 100644 Documentation/kbuild/namespaces.rst
 create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
 create mode 100644 scripts/nsdeps

-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 01/11] module: support reading multiple values per modinfo tag
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 02/11] export: explicitly align struct kernel_symbol Matthias Maennich
                     ` (10 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

Similar to modpost's get_next_modinfo(), introduce get_next_modinfo() in
kernel/module.c to acquire any further values associated with the same
modinfo tag name. That is useful for any tags that have multiple
occurrences (such as 'alias'), but is in particular introduced here as
part of the symbol namespaces patch series to read the (potentially)
multiple namespaces a module is importing.

Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 kernel/module.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index 9ee93421269c..3ee507c0a92f 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2481,7 +2481,8 @@ static char *next_string(char *string, unsigned long *secsize)
 	return string;
 }
 
-static char *get_modinfo(struct load_info *info, const char *tag)
+static char *get_next_modinfo(const struct load_info *info, const char *tag,
+			      char *prev)
 {
 	char *p;
 	unsigned int taglen = strlen(tag);
@@ -2492,13 +2493,25 @@ static char *get_modinfo(struct load_info *info, const char *tag)
 	 * get_modinfo() calls made before rewrite_section_headers()
 	 * must use sh_offset, as sh_addr isn't set!
 	 */
-	for (p = (char *)info->hdr + infosec->sh_offset; p; p = next_string(p, &size)) {
+	char *modinfo = (char *)info->hdr + infosec->sh_offset;
+
+	if (prev) {
+		size -= prev - modinfo;
+		modinfo = next_string(prev, &size);
+	}
+
+	for (p = modinfo; p; p = next_string(p, &size)) {
 		if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
 			return p + taglen + 1;
 	}
 	return NULL;
 }
 
+static char *get_modinfo(const struct load_info *info, const char *tag)
+{
+	return get_next_modinfo(info, tag, NULL);
+}
+
 static void setup_modinfo(struct module *mod, struct load_info *info)
 {
 	struct module_attribute *attr;
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 02/11] export: explicitly align struct kernel_symbol
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 01/11] module: support reading multiple values per modinfo tag Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 03/11] module: add support for symbol namespaces Matthias Maennich
                     ` (9 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

This change allows growing struct kernel_symbol without wasting bytes to
alignment. It also concretized the alignment of ksymtab entries if
relative references are used for ksymtab entries.

struct kernel_symbol was already implicitly being aligned to the word
size, except on x86_64 and m68k, where it is aligned to 16 and 2 bytes,
respectively.

As far as I can tell there is no requirement for aligning struct
kernel_symbol to 16 bytes on x86_64, but gcc aligns structs to their
size, and the linker aligns the custom __ksymtab sections to the largest
data type contained within, so setting KSYM_ALIGN to 16 was necessary to
stay consistent with the code generated for non-ASM EXPORT_SYMBOL(). Now
that non-ASM EXPORT_SYMBOL() explicitly aligns to word size (8),
KSYM_ALIGN is no longer necessary.

In case of relative references, the alignment has been changed
accordingly to not waste space when adding new struct members.

As for m68k, struct kernel_symbol is aligned to 2 bytes even though the
structure itself is 8 bytes; using a 4-byte alignment shouldn't hurt.

I manually verified the output of the __ksymtab sections didn't change
on x86, x86_64, arm, arm64 and m68k. As expected, the section contents
didn't change, and the ELF section alignment only changed on x86_64 and
m68k. Feedback from other archs more than welcome.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 arch/m68k/include/asm/export.h | 1 -
 include/asm-generic/export.h   | 8 +++-----
 include/linux/export.h         | 3 ++-
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/arch/m68k/include/asm/export.h b/arch/m68k/include/asm/export.h
index 0af20f48bd07..b53008b67ce1 100644
--- a/arch/m68k/include/asm/export.h
+++ b/arch/m68k/include/asm/export.h
@@ -1,3 +1,2 @@
-#define KSYM_ALIGN 2
 #define KCRC_ALIGN 2
 #include <asm-generic/export.h>
diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 294d6ae785d4..63f54907317b 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -4,15 +4,13 @@
 #ifndef KSYM_FUNC
 #define KSYM_FUNC(x) x
 #endif
-#ifdef CONFIG_64BIT
-#ifndef KSYM_ALIGN
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+#define KSYM_ALIGN 4
+#elif defined(CONFIG_64BIT)
 #define KSYM_ALIGN 8
-#endif
 #else
-#ifndef KSYM_ALIGN
 #define KSYM_ALIGN 4
 #endif
-#endif
 #ifndef KCRC_ALIGN
 #define KCRC_ALIGN 4
 #endif
diff --git a/include/linux/export.h b/include/linux/export.h
index fd8711ed9ac4..28a4d2150689 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -52,7 +52,7 @@ extern struct module __this_module;
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	__ADDRESSABLE(sym)						\
 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
-	    "	.balign	8					\n"	\
+	    "	.balign 4					\n"	\
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
@@ -66,6 +66,7 @@ struct kernel_symbol {
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	static const struct kernel_symbol __ksymtab_##sym		\
 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
+	__aligned(sizeof(void *))					\
 	= { (unsigned long)&sym, __kstrtab_##sym }
 
 struct kernel_symbol {
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 03/11] module: add support for symbol namespaces.
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 01/11] module: support reading multiple values per modinfo tag Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 02/11] export: explicitly align struct kernel_symbol Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 04/11] modpost: " Matthias Maennich
                     ` (8 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

The EXPORT_SYMBOL_NS() and EXPORT_SYMBOL_NS_GPL() macros can be used to
export a symbol to a specific namespace.  There are no _GPL_FUTURE and
_UNUSED variants because these are currently unused, and I'm not sure
they are necessary.

I didn't add EXPORT_SYMBOL_NS() for ASM exports; this patch sets the
namespace of ASM exports to NULL by default. In case of relative
references, it will be relocatable to NULL. If there's a need, this
should be pretty easy to add.

A module that wants to use a symbol exported to a namespace must add a
MODULE_IMPORT_NS() statement to their module code; otherwise, modpost
will complain when building the module, and the kernel module loader
will emit an error and fail when loading the module.

MODULE_IMPORT_NS() adds a modinfo tag 'import_ns' to the module. That
tag can be observed by the modinfo command, modpost and kernel/module.c
at the time of loading the module.

The ELF symbols are renamed to include the namespace with an asm label;
for example, symbol 'usb_stor_suspend' in namespace USB_STORAGE becomes
'usb_stor_suspend.USB_STORAGE'.  This allows modpost to do namespace
checking, without having to go through all the effort of parsing ELF and
relocation records just to get to the struct kernel_symbols.

On x86_64 I saw no difference in binary size (compression), but at
runtime this will require a word of memory per export to hold the
namespace. An alternative could be to store namespaced symbols in their
own section and use a separate 'struct namespaced_kernel_symbol' for
that section, at the cost of making the module loader more complex.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/asm-generic/export.h |  6 +--
 include/linux/export.h       | 91 +++++++++++++++++++++++++++++-------
 include/linux/module.h       |  2 +
 kernel/module.c              | 43 +++++++++++++++++
 4 files changed, 123 insertions(+), 19 deletions(-)

diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 63f54907317b..e2b5d0f569d3 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -17,11 +17,11 @@
 
 .macro __put, val, name
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
-	.long	\val - ., \name - .
+	.long	\val - ., \name - ., 0 - .
 #elif defined(CONFIG_64BIT)
-	.quad	\val, \name
+	.quad	\val, \name, 0
 #else
-	.long	\val, \name
+	.long	\val, \name, 0
 #endif
 .endm
 
diff --git a/include/linux/export.h b/include/linux/export.h
index 28a4d2150689..d59461e71478 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -20,6 +20,8 @@ extern struct module __this_module;
 
 #ifdef CONFIG_MODULES
 
+#define NS_SEPARATOR "."
+
 #if defined(__KERNEL__) && !defined(__GENKSYMS__)
 #ifdef CONFIG_MODVERSIONS
 /* Mark the CRC weak since genksyms apparently decides not to
@@ -29,13 +31,13 @@ extern struct module __this_module;
 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
 	    "	.weak	__crc_" #sym "				\n"	\
 	    "	.long	__crc_" #sym " - .			\n"	\
-	    "	.previous					\n");
+	    "	.previous					\n")
 #else
 #define __CRC_SYMBOL(sym, sec)						\
 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
 	    "	.weak	__crc_" #sym "				\n"	\
 	    "	.long	__crc_" #sym "				\n"	\
-	    "	.previous					\n");
+	    "	.previous					\n")
 #endif
 #else
 #define __CRC_SYMBOL(sym, sec)
@@ -49,6 +51,16 @@ extern struct module __this_module;
  * absolute relocations that require runtime processing on relocatable
  * kernels.
  */
+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
+	__ADDRESSABLE(sym)						\
+	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
+	    "	.balign	4					\n"	\
+	    "__ksymtab_" #sym NS_SEPARATOR #ns ":		\n"	\
+	    "	.long	" #sym "- .				\n"	\
+	    "	.long	__kstrtab_" #sym "- .			\n"	\
+	    "	.long	__kstrtab_ns_" #sym "- .		\n"	\
+	    "	.previous					\n")
+
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	__ADDRESSABLE(sym)						\
 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
@@ -56,32 +68,53 @@ extern struct module __this_module;
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
+	    "	.long	0 - .					\n"	\
 	    "	.previous					\n")
 
 struct kernel_symbol {
 	int value_offset;
 	int name_offset;
+	int namespace_offset;
 };
 #else
+#define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
+	static const struct kernel_symbol __ksymtab_##sym##__##ns	\
+	asm("__ksymtab_" #sym NS_SEPARATOR #ns)				\
+	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
+	__aligned(sizeof(void *))					\
+	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym }
+
 #define __KSYMTAB_ENTRY(sym, sec)					\
 	static const struct kernel_symbol __ksymtab_##sym		\
+	asm("__ksymtab_" #sym)						\
 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
 	__aligned(sizeof(void *))					\
-	= { (unsigned long)&sym, __kstrtab_##sym }
+	= { (unsigned long)&sym, __kstrtab_##sym, NULL }
 
 struct kernel_symbol {
 	unsigned long value;
 	const char *name;
+	const char *namespace;
 };
 #endif
 
-/* For every exported symbol, place a struct in the __ksymtab section */
-#define ___EXPORT_SYMBOL(sym, sec)					\
+#define ___export_symbol_common(sym, sec)				\
 	extern typeof(sym) sym;						\
-	__CRC_SYMBOL(sym, sec)						\
+	__CRC_SYMBOL(sym, sec);						\
 	static const char __kstrtab_##sym[]				\
 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
-	= #sym;								\
+	= #sym								\
+
+/* For every exported symbol, place a struct in the __ksymtab section */
+#define ___EXPORT_SYMBOL_NS(sym, sec, ns)				\
+	___export_symbol_common(sym, sec);				\
+	static const char __kstrtab_ns_##sym[]				\
+	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
+	= #ns;								\
+	__KSYMTAB_ENTRY_NS(sym, sec, ns)
+
+#define ___EXPORT_SYMBOL(sym, sec)					\
+	___export_symbol_common(sym, sec);				\
 	__KSYMTAB_ENTRY(sym, sec)
 
 #if defined(__DISABLE_EXPORTS)
@@ -91,6 +124,7 @@ struct kernel_symbol {
  * be reused in other execution contexts such as the UEFI stub or the
  * decompressor.
  */
+#define __EXPORT_SYMBOL_NS(sym, sec, ns)
 #define __EXPORT_SYMBOL(sym, sec)
 
 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
@@ -117,18 +151,26 @@ struct kernel_symbol {
 #define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
 #define __cond_export_sym_0(sym, sec) /* nothing */
 
+#define __EXPORT_SYMBOL_NS(sym, sec, ns)				\
+	__ksym_marker(sym);						\
+	__cond_export_ns_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
+#define __cond_export_ns_sym(sym, sec, ns, conf)			\
+	___cond_export_ns_sym(sym, sec, ns, conf)
+#define ___cond_export_ns_sym(sym, sec, ns, enabled)			\
+	__cond_export_ns_sym_##enabled(sym, sec, ns)
+#define __cond_export_ns_sym_1(sym, sec, ns) ___EXPORT_SYMBOL_NS(sym, sec, ns)
+#define __cond_export_ns_sym_0(sym, sec, ns) /* nothing */
+
 #else
+#define __EXPORT_SYMBOL_NS ___EXPORT_SYMBOL_NS
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
 #endif
 
-#define EXPORT_SYMBOL(sym)					\
-	__EXPORT_SYMBOL(sym, "")
-
-#define EXPORT_SYMBOL_GPL(sym)					\
-	__EXPORT_SYMBOL(sym, "_gpl")
-
-#define EXPORT_SYMBOL_GPL_FUTURE(sym)				\
-	__EXPORT_SYMBOL(sym, "_gpl_future")
+#define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
+#define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
+#define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
+#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL_NS(sym, "", ns)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL_NS(sym, "_gpl", ns)
 
 #ifdef CONFIG_UNUSED_SYMBOLS
 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
@@ -138,11 +180,28 @@ struct kernel_symbol {
 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
 #endif
 
-#endif	/* __GENKSYMS__ */
+#endif	/* __KERNEL__ && !__GENKSYMS__ */
+
+#if defined(__GENKSYMS__)
+/*
+ * When we're running genksyms, ignore the namespace and make the _NS
+ * variants look like the normal ones. There are two reasons for this:
+ * 1) In the normal definition of EXPORT_SYMBOL_NS, the 'ns' macro
+ *    argument is itself not expanded because it's always tokenized or
+ *    concatenated; but when running genksyms, a blank definition of the
+ *    macro does allow the argument to be expanded; if a namespace
+ *    happens to collide with a #define, this can cause issues.
+ * 2) There's no need to modify genksyms to deal with the _NS variants
+ */
+#define EXPORT_SYMBOL_NS(sym, ns) EXPORT_SYMBOL(sym)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns) EXPORT_SYMBOL_GPL(sym)
+#endif
 
 #else /* !CONFIG_MODULES... */
 
 #define EXPORT_SYMBOL(sym)
+#define EXPORT_SYMBOL_NS(sym, ns)
+#define EXPORT_SYMBOL_NS_GPL(sym, ns)
 #define EXPORT_SYMBOL_GPL(sym)
 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
 #define EXPORT_UNUSED_SYMBOL(sym)
diff --git a/include/linux/module.h b/include/linux/module.h
index 1455812dd325..b3611e749f72 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -280,6 +280,8 @@ struct notifier_block;
 
 #ifdef CONFIG_MODULES
 
+#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)
+
 extern int modules_disabled; /* for sysctl */
 /* Get/put a kernel symbol (calls must be symmetric) */
 void *__symbol_get(const char *symbol);
diff --git a/kernel/module.c b/kernel/module.c
index 3ee507c0a92f..6bb9b938f9c7 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -544,6 +544,15 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
 #endif
 }
 
+static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
+{
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	return offset_to_ptr(&sym->namespace_offset);
+#else
+	return sym->namespace;
+#endif
+}
+
 static int cmp_name(const void *va, const void *vb)
 {
 	const char *a;
@@ -1379,6 +1388,34 @@ static inline int same_magic(const char *amagic, const char *bmagic,
 }
 #endif /* CONFIG_MODVERSIONS */
 
+static char *get_modinfo(const struct load_info *info, const char *tag);
+static char *get_next_modinfo(const struct load_info *info, const char *tag,
+			      char *prev);
+
+static int verify_namespace_is_imported(const struct load_info *info,
+					const struct kernel_symbol *sym,
+					struct module *mod)
+{
+	const char *namespace;
+	char *imported_namespace;
+
+	namespace = kernel_symbol_namespace(sym);
+	if (namespace) {
+		imported_namespace = get_modinfo(info, "import_ns");
+		while (imported_namespace) {
+			if (strcmp(namespace, imported_namespace) == 0)
+				return 0;
+			imported_namespace = get_next_modinfo(
+				info, "import_ns", imported_namespace);
+		}
+		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
+		       mod->name, kernel_symbol_name(sym), namespace);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+
 /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
 static const struct kernel_symbol *resolve_symbol(struct module *mod,
 						  const struct load_info *info,
@@ -1407,6 +1444,12 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
 		goto getname;
 	}
 
+	err = verify_namespace_is_imported(info, sym, mod);
+	if (err) {
+		sym = ERR_PTR(err);
+		goto getname;
+	}
+
 	err = ref_module(mod, owner);
 	if (err) {
 		sym = ERR_PTR(err);
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 04/11] modpost: add support for symbol namespaces
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (2 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 03/11] module: add support for symbol namespaces Matthias Maennich
@ 2019-09-06 10:32   ` " Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 05/11] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
                     ` (7 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

Add support for symbols that are exported into namespaces. For that,
extract any namespace suffix from the symbol name. In addition, emit a
warning whenever a module refers to an exported symbol without
explicitly importing the namespace that it is defined in. This patch
consistently adds the namespace suffix to symbol names exported into
Module.symvers.

Example warning emitted by modpost in case of the above violation:

 WARNING: module ums-usbat uses symbol usb_stor_resume from namespace
 USB_STORAGE, but does not import it.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 Documentation/kbuild/modules.rst |   7 ++-
 scripts/export_report.pl         |   2 +-
 scripts/mod/modpost.c            | 104 ++++++++++++++++++++++++++-----
 scripts/mod/modpost.h            |   7 +++
 4 files changed, 101 insertions(+), 19 deletions(-)

diff --git a/Documentation/kbuild/modules.rst b/Documentation/kbuild/modules.rst
index 24e763482650..d2ae799237fd 100644
--- a/Documentation/kbuild/modules.rst
+++ b/Documentation/kbuild/modules.rst
@@ -470,9 +470,12 @@ build.
 
 	The syntax of the Module.symvers file is::
 
-		<CRC>	    <Symbol>	       <module>
+	<CRC>       <Symbol>          <Namespace>  <Module>                         <Export Type>
 
-		0x2d036834  scsi_remove_host   drivers/scsi/scsi_mod
+	0xe1cc2a05  usb_stor_suspend  USB_STORAGE  drivers/usb/storage/usb-storage  EXPORT_SYMBOL_GPL
+
+	The fields are separated by tabs and values may be empty (e.g.
+	if no namespace is defined for an exported symbol).
 
 	For a kernel build without CONFIG_MODVERSIONS enabled, the CRC
 	would read 0x00000000.
diff --git a/scripts/export_report.pl b/scripts/export_report.pl
index 7d3030d03a25..548330e8c4e7 100755
--- a/scripts/export_report.pl
+++ b/scripts/export_report.pl
@@ -94,7 +94,7 @@ if (defined $opt{'o'}) {
 #
 while ( <$module_symvers> ) {
 	chomp;
-	my (undef, $symbol, $module, $gpl) = split;
+	my (undef, $symbol, $namespace, $module, $gpl) = split('\t');
 	$SYMBOL { $symbol } =  [ $module , "0" , $symbol, $gpl];
 }
 close($module_symvers);
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index f277e116e0eb..c2d49afaea1c 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -164,6 +164,7 @@ struct symbol {
 	struct module *module;
 	unsigned int crc;
 	int crc_valid;
+	const char *namespace;
 	unsigned int weak:1;
 	unsigned int vmlinux:1;    /* 1 if symbol is defined in vmlinux */
 	unsigned int kernel:1;     /* 1 if symbol is from kernel
@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
 	return NULL;
 }
 
+static bool contains_namespace(struct namespace_list *list,
+			       const char *namespace)
+{
+	struct namespace_list *ns_entry;
+
+	for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
+		if (strcmp(ns_entry->namespace, namespace) == 0)
+			return true;
+
+	return false;
+}
+
+static void add_namespace(struct namespace_list **list, const char *namespace)
+{
+	struct namespace_list *ns_entry;
+
+	if (!contains_namespace(*list, namespace)) {
+		ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
+					 strlen(namespace) + 1));
+		strcpy(ns_entry->namespace, namespace);
+		ns_entry->next = *list;
+		*list = ns_entry;
+	}
+}
+
+static bool module_imports_namespace(struct module *module,
+				     const char *namespace)
+{
+	return contains_namespace(module->imported_namespaces, namespace);
+}
+
 static const struct {
 	const char *str;
 	enum export export;
@@ -312,23 +344,39 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
 		return export_unknown;
 }
 
+static const char *sym_extract_namespace(const char **symname)
+{
+	size_t n;
+	char *dupsymname;
+
+	n = strcspn(*symname, ".");
+	if (n < strlen(*symname) - 1) {
+		dupsymname = NOFAIL(strdup(*symname));
+		dupsymname[n] = '\0';
+		*symname = dupsymname;
+		return dupsymname + n + 1;
+	}
+
+	return NULL;
+}
+
 /**
  * Add an exported symbol - it may have already been added without a
  * CRC, in this case just update the CRC
  **/
-static struct symbol *sym_add_exported(const char *name, struct module *mod,
-				       enum export export)
+static struct symbol *sym_add_exported(const char *name, const char *namespace,
+				       struct module *mod, enum export export)
 {
 	struct symbol *s = find_symbol(name);
 
 	if (!s) {
 		s = new_symbol(name, mod, export);
+		s->namespace = namespace;
 	} else {
 		if (!s->preloaded) {
-			warn("%s: '%s' exported twice. Previous export "
-			     "was in %s%s\n", mod->name, name,
-			     s->module->name,
-			     is_vmlinux(s->module->name) ?"":".ko");
+			warn("%s: '%s' exported twice. Previous export was in %s%s\n",
+			     mod->name, name, s->module->name,
+			     is_vmlinux(s->module->name) ? "" : ".ko");
 		} else {
 			/* In case Module.symvers was out of date */
 			s->module = mod;
@@ -620,6 +668,7 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
 	unsigned int crc;
 	enum export export;
 	bool is_crc = false;
+	const char *name, *namespace;
 
 	if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
 	    strstarts(symname, "__ksymtab"))
@@ -691,8 +740,9 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
 	default:
 		/* All exported symbols */
 		if (strstarts(symname, "__ksymtab_")) {
-			sym_add_exported(symname + strlen("__ksymtab_"), mod,
-					export);
+			name = symname + strlen("__ksymtab_");
+			namespace = sym_extract_namespace(&name);
+			sym_add_exported(name, namespace, mod, export);
 		}
 		if (strcmp(symname, "init_module") == 0)
 			mod->has_init = 1;
@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
 	const char *symname;
 	char *version;
 	char *license;
+	char *namespace;
 	struct module *mod;
 	struct elf_info info = { };
 	Elf_Sym *sym;
@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
 		license = get_next_modinfo(&info, "license", license);
 	}
 
+	namespace = get_modinfo(&info, "import_ns");
+	while (namespace) {
+		add_namespace(&mod->imported_namespaces, namespace);
+		namespace = get_next_modinfo(&info, "import_ns", namespace);
+	}
+
 	for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
 		symname = remove_dot(info.strtab + sym->st_name);
 
@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
 			basename++;
 		else
 			basename = mod->name;
+
+		if (exp->namespace &&
+		    !module_imports_namespace(mod, exp->namespace)) {
+			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
+			     basename, exp->name, exp->namespace);
+		}
+
 		if (!mod->gpl_compatible)
 			check_for_gpl_usage(exp->export, basename, exp->name);
 		check_for_unused(exp->export, basename, exp->name);
@@ -2341,7 +2405,7 @@ static void read_dump(const char *fname, unsigned int kernel)
 		return;
 
 	while ((line = get_next_line(&pos, file, size))) {
-		char *symname, *modname, *d, *export, *end;
+		char *symname, *namespace, *modname, *d, *export, *end;
 		unsigned int crc;
 		struct module *mod;
 		struct symbol *s;
@@ -2349,7 +2413,10 @@ static void read_dump(const char *fname, unsigned int kernel)
 		if (!(symname = strchr(line, '\t')))
 			goto fail;
 		*symname++ = '\0';
-		if (!(modname = strchr(symname, '\t')))
+		if (!(namespace = strchr(symname, '\t')))
+			goto fail;
+		*namespace++ = '\0';
+		if (!(modname = strchr(namespace, '\t')))
 			goto fail;
 		*modname++ = '\0';
 		if ((export = strchr(modname, '\t')) != NULL)
@@ -2366,7 +2433,8 @@ static void read_dump(const char *fname, unsigned int kernel)
 			mod = new_module(modname);
 			mod->skip = 1;
 		}
-		s = sym_add_exported(symname, mod, export_no(export));
+		s = sym_add_exported(symname, namespace, mod,
+				     export_no(export));
 		s->kernel    = kernel;
 		s->preloaded = 1;
 		sym_update_crc(symname, mod, crc, export_no(export));
@@ -2395,16 +2463,20 @@ static void write_dump(const char *fname)
 {
 	struct buffer buf = { };
 	struct symbol *symbol;
+	const char *namespace;
 	int n;
 
 	for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
 		symbol = symbolhash[n];
 		while (symbol) {
-			if (dump_sym(symbol))
-				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
-					symbol->crc, symbol->name,
-					symbol->module->name,
-					export_str(symbol->export));
+			if (dump_sym(symbol)) {
+				namespace = symbol->namespace;
+				buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
+					   symbol->crc, symbol->name,
+					   namespace ? namespace : "",
+					   symbol->module->name,
+					   export_str(symbol->export));
+			}
 			symbol = symbol->next;
 		}
 	}
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 8453d6ac2f77..9626bf3e7424 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -109,6 +109,11 @@ buf_printf(struct buffer *buf, const char *fmt, ...);
 void
 buf_write(struct buffer *buf, const char *s, int len);
 
+struct namespace_list {
+	struct namespace_list *next;
+	char namespace[0];
+};
+
 struct module {
 	struct module *next;
 	const char *name;
@@ -121,6 +126,8 @@ struct module {
 	struct buffer dev_table_buf;
 	char	     srcversion[25];
 	int is_dot_o;
+	// Actual imported namespaces
+	struct namespace_list *imported_namespaces;
 };
 
 struct elf_info {
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 05/11] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (3 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 04/11] modpost: " Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 06/11] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
                     ` (6 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

If MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is enabled (default=n), the
requirement for modules to import all namespaces that are used by
the module is relaxed.

Enabling this option effectively allows (invalid) modules to be loaded
while only a warning is emitted.

Disabling this option keeps the enforcement at module loading time and
loading is denied if the module's imports are not satisfactory.

Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 init/Kconfig    | 13 +++++++++++++
 kernel/module.c | 11 +++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/init/Kconfig b/init/Kconfig
index bd7d650d4a99..cc28561288a7 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -2119,6 +2119,19 @@ config MODULE_COMPRESS_XZ
 
 endchoice
 
+config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
+	bool "Allow loading of modules with missing namespace imports"
+	help
+	  Symbols exported with EXPORT_SYMBOL_NS*() are considered exported in
+	  a namespace. A module that makes use of a symbol exported with such a
+	  namespace is required to import the namespace via MODULE_IMPORT_NS().
+	  There is no technical reason to enforce correct namespace imports,
+	  but it creates consistency between symbols defining namespaces and
+	  users importing namespaces they make use of. This option relaxes this
+	  requirement and lifts the enforcement when loading a module.
+
+	  If unsure, say N.
+
 config TRIM_UNUSED_KSYMS
 	bool "Trim unused exported kernel symbols"
 	depends on MODULES && !UNUSED_SYMBOLS
diff --git a/kernel/module.c b/kernel/module.c
index 6bb9b938f9c7..f76efcf2043e 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -1408,9 +1408,16 @@ static int verify_namespace_is_imported(const struct load_info *info,
 			imported_namespace = get_next_modinfo(
 				info, "import_ns", imported_namespace);
 		}
-		pr_err("%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
-		       mod->name, kernel_symbol_name(sym), namespace);
+#ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
+		pr_warn(
+#else
+		pr_err(
+#endif
+			"%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
+			mod->name, kernel_symbol_name(sym), namespace);
+#ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
 		return -EINVAL;
+#endif
 	}
 	return 0;
 }
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 06/11] export: allow definition default namespaces in Makefiles or sources
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (4 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 05/11] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 07/11] modpost: add support for generating namespace dependencies Matthias Maennich
                     ` (5 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

To avoid excessive usage of EXPORT_SYMBOL_NS(sym, MY_NAMESPACE), where
MY_NAMESPACE will always be the namespace we are exporting to, allow
exporting all definitions of EXPORT_SYMBOL() and friends by defining
DEFAULT_SYMBOL_NAMESPACE.

For example, to export all symbols defined in usb-common into the
namespace USB_COMMON, add a line like this to drivers/usb/common/Makefile:

  ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON

That is equivalent to changing all EXPORT_SYMBOL(sym) definitions to
EXPORT_SYMBOL_NS(sym, USB_COMMON). Subsequently all symbol namespaces
functionality will apply.

Another way of making use of this feature is to define the namespace
within source or header files similar to how TRACE_SYSTEM defines are
used:
  #undef DEFAULT_SYMBOL_NAMESPACE
  #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON

Please note that, as opposed to TRACE_SYSTEM, DEFAULT_SYMBOL_NAMESPACE
has to be defined before including include/linux/export.h.

If DEFAULT_SYMBOL_NAMESPACE is defined, a symbol can still be exported
to another namespace by using EXPORT_SYMBOL_NS() and friends with
explicitly specifying the namespace.

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 include/linux/export.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/export.h b/include/linux/export.h
index d59461e71478..2c5468d8ea9a 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -166,6 +166,12 @@ struct kernel_symbol {
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
 #endif
 
+#ifdef DEFAULT_SYMBOL_NAMESPACE
+#undef __EXPORT_SYMBOL
+#define __EXPORT_SYMBOL(sym, sec)				\
+	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
+#endif
+
 #define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
 #define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
 #define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 07/11] modpost: add support for generating namespace dependencies
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (5 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 06/11] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 08/11] scripts: Coccinelle script for " Matthias Maennich
                     ` (4 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

This patch adds an option to modpost to generate a <module>.ns_deps file
per module, containing the namespace dependencies for that module.

E.g. if the linked module my-module.ko would depend on the symbol
myfunc.MY_NS in the namespace MY_NS, the my-module.ns_deps file created
by modpost would contain the entry MY_NS to express the namespace
dependency of my-module imposed by using the symbol myfunc.

These files can subsequently be used by static analysis tools (like
coccinelle scripts) to address issues with missing namespace imports. A
later patch of this series will introduce such a script 'nsdeps' and a
corresponding make target to automatically add missing
MODULE_IMPORT_NS() definitions to the module's sources. For that it uses
the information provided in the generated .ns_deps files.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 .gitignore            |  1 +
 Makefile              |  2 +-
 scripts/mod/modpost.c | 54 +++++++++++++++++++++++++++++++++++++++----
 scripts/mod/modpost.h |  2 ++
 4 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2030c7a4d2f8..9ee63aa2a3fb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,6 +32,7 @@
 *.lzo
 *.mod
 *.mod.c
+*.ns_deps
 *.o
 *.o.*
 *.order
diff --git a/Makefile b/Makefile
index 0cbe8717bdb3..0cdb957ae2c3 100644
--- a/Makefile
+++ b/Makefile
@@ -1669,7 +1669,7 @@ clean: $(clean-dirs)
 		-o -name '*.ko.*' \
 		-o -name '*.dtb' -o -name '*.dtb.S' -o -name '*.dt.yaml' \
 		-o -name '*.dwo' -o -name '*.lst' \
-		-o -name '*.su' -o -name '*.mod' \
+		-o -name '*.su' -o -name '*.mod' -o -name '*.ns_deps' \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
 		-o -name '*.lex.c' -o -name '*.tab.[ch]' \
 		-o -name '*.asn1.[ch]' \
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index c2d49afaea1c..be72da25fe7c 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -38,6 +38,8 @@ static int sec_mismatch_count = 0;
 static int sec_mismatch_fatal = 0;
 /* ignore missing files */
 static int ignore_missing_files;
+/* write namespace dependencies */
+static int write_namespace_deps;
 
 enum export {
 	export_plain,      export_unused,     export_gpl,
@@ -2176,10 +2178,15 @@ static int check_exports(struct module *mod)
 		else
 			basename = mod->name;
 
-		if (exp->namespace &&
-		    !module_imports_namespace(mod, exp->namespace)) {
-			warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
-			     basename, exp->name, exp->namespace);
+		if (exp->namespace) {
+			add_namespace(&mod->required_namespaces,
+				      exp->namespace);
+
+			if (!write_namespace_deps &&
+			    !module_imports_namespace(mod, exp->namespace)) {
+				warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
+				     basename, exp->name, exp->namespace);
+			}
 		}
 
 		if (!mod->gpl_compatible)
@@ -2484,6 +2491,31 @@ static void write_dump(const char *fname)
 	free(buf.p);
 }
 
+static void write_namespace_deps_files(void)
+{
+	struct module *mod;
+	struct namespace_list *ns;
+	struct buffer ns_deps_buf = {};
+
+	for (mod = modules; mod; mod = mod->next) {
+		char fname[PATH_MAX];
+
+		if (mod->skip)
+			continue;
+
+		ns_deps_buf.pos = 0;
+
+		for (ns = mod->required_namespaces; ns; ns = ns->next)
+			buf_printf(&ns_deps_buf, "%s\n", ns->namespace);
+
+		if (ns_deps_buf.pos == 0)
+			continue;
+
+		sprintf(fname, "%s.ns_deps", mod->name);
+		write_if_changed(&ns_deps_buf, fname);
+	}
+}
+
 struct ext_sym_list {
 	struct ext_sym_list *next;
 	const char *file;
@@ -2500,7 +2532,7 @@ int main(int argc, char **argv)
 	struct ext_sym_list *extsym_iter;
 	struct ext_sym_list *extsym_start = NULL;
 
-	while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
+	while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awEd")) != -1) {
 		switch (opt) {
 		case 'i':
 			kernel_read = optarg;
@@ -2541,6 +2573,9 @@ int main(int argc, char **argv)
 		case 'E':
 			sec_mismatch_fatal = 1;
 			break;
+		case 'd':
+			write_namespace_deps = 1;
+			break;
 		default:
 			exit(1);
 		}
@@ -2575,6 +2610,9 @@ int main(int argc, char **argv)
 
 		err |= check_modname_len(mod);
 		err |= check_exports(mod);
+		if (write_namespace_deps)
+			continue;
+
 		add_header(&buf, mod);
 		add_intree_flag(&buf, !external_module);
 		add_retpoline(&buf);
@@ -2587,6 +2625,12 @@ int main(int argc, char **argv)
 		sprintf(fname, "%s.mod.c", mod->name);
 		write_if_changed(&buf, fname);
 	}
+
+	if (write_namespace_deps) {
+		write_namespace_deps_files();
+		return 0;
+	}
+
 	if (dump_write)
 		write_dump(dump_write);
 	if (sec_mismatch_count && sec_mismatch_fatal)
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 9626bf3e7424..92a926d375d2 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -126,6 +126,8 @@ struct module {
 	struct buffer dev_table_buf;
 	char	     srcversion[25];
 	int is_dot_o;
+	// Required namespace dependencies
+	struct namespace_list *required_namespaces;
 	// Actual imported namespaces
 	struct namespace_list *imported_namespaces;
 };
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 08/11] scripts: Coccinelle script for namespace dependencies.
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (6 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 07/11] modpost: add support for generating namespace dependencies Matthias Maennich
@ 2019-09-06 10:32   ` " Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 09/11] docs: Add documentation for Symbol Namespaces Matthias Maennich
                     ` (3 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage, Julia Lawall

A script that uses the '<module>.ns_deps' files generated by modpost to
automatically add the required symbol namespace dependencies to each
module.

Usage:
1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
   DEFAULT_SYMBOL_NAMESPACE
2) Run 'make' (or 'make modules') and get warnings about modules not
   importing that namespace.
3) Run 'make nsdeps' to automatically add required import statements
   to said modules.

This makes it easer for subsystem maintainers to introduce and maintain
symbol namespaces into their codebase.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 MAINTAINERS                                 |  5 ++
 Makefile                                    | 12 +++++
 scripts/Makefile.modpost                    |  4 +-
 scripts/coccinelle/misc/add_namespace.cocci | 23 ++++++++
 scripts/nsdeps                              | 58 +++++++++++++++++++++
 5 files changed, 101 insertions(+), 1 deletion(-)
 create mode 100644 scripts/coccinelle/misc/add_namespace.cocci
 create mode 100644 scripts/nsdeps

diff --git a/MAINTAINERS b/MAINTAINERS
index e7a47b5210fd..3b170c11a0e6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11436,6 +11436,11 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
 F:	tools/include/nolibc/
 
+NSDEPS
+M:	Matthias Maennich <maennich@google.com>
+S:	Maintained
+F:	scripts/nsdeps
+
 NTB AMD DRIVER
 M:	Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
 L:	linux-ntb@googlegroups.com
diff --git a/Makefile b/Makefile
index 0cdb957ae2c3..014d55b400ca 100644
--- a/Makefile
+++ b/Makefile
@@ -1500,6 +1500,9 @@ help:
 	@echo  '  headerdep       - Detect inclusion cycles in headers'
 	@echo  '  coccicheck      - Check with Coccinelle'
 	@echo  ''
+	@echo  'Tools:'
+	@echo  '  nsdeps          - Generate missing symbol namespace dependencies'
+	@echo  ''
 	@echo  'Kernel selftest:'
 	@echo  '  kselftest       - Build and run kernel selftest (run as root)'
 	@echo  '                    Build, install, and boot kernel before'
@@ -1687,6 +1690,15 @@ quiet_cmd_tags = GEN     $@
 tags TAGS cscope gtags: FORCE
 	$(call cmd,tags)
 
+# Script to generate missing namespace dependencies
+# ---------------------------------------------------------------------------
+
+PHONY += nsdeps
+
+nsdeps: modules
+	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost nsdeps
+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@
+
 # Scripts to check various things for consistency
 # ---------------------------------------------------------------------------
 
diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost
index 26e6574ecd08..743fe3a2e885 100644
--- a/scripts/Makefile.modpost
+++ b/scripts/Makefile.modpost
@@ -56,7 +56,8 @@ MODPOST = scripts/mod/modpost						\
 	$(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS)))	\
 	$(if $(KBUILD_EXTMOD),-o $(modulesymfile))			\
 	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)			\
-	$(if $(KBUILD_MODPOST_WARN),-w)
+	$(if $(KBUILD_MODPOST_WARN),-w)					\
+	$(if $(filter nsdeps,$(MAKECMDGOALS)),-d)
 
 ifdef MODPOST_VMLINUX
 
@@ -134,6 +135,7 @@ $(modules): %.ko :%.o %.mod.o FORCE
 
 targets += $(modules)
 
+nsdeps: __modpost
 
 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
 # ---------------------------------------------------------------------------
diff --git a/scripts/coccinelle/misc/add_namespace.cocci b/scripts/coccinelle/misc/add_namespace.cocci
new file mode 100644
index 000000000000..c832bb6445a8
--- /dev/null
+++ b/scripts/coccinelle/misc/add_namespace.cocci
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+/// Adds missing MODULE_IMPORT_NS statements to source files
+///
+/// This script is usually called from scripts/nsdeps with -D ns=<namespace> to
+/// add a missing namespace tag to a module source file.
+///
+
+@has_ns_import@
+declarer name MODULE_IMPORT_NS;
+identifier virtual.ns;
+@@
+MODULE_IMPORT_NS(ns);
+
+// Add missing imports, but only adjacent to a MODULE_LICENSE statement.
+// That ensures we are adding it only to the main module source file.
+@do_import depends on !has_ns_import@
+declarer name MODULE_LICENSE;
+expression license;
+identifier virtual.ns;
+@@
+MODULE_LICENSE(license);
++ MODULE_IMPORT_NS(ns);
diff --git a/scripts/nsdeps b/scripts/nsdeps
new file mode 100644
index 000000000000..ac2b6031dd13
--- /dev/null
+++ b/scripts/nsdeps
@@ -0,0 +1,58 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Linux kernel symbol namespace import generator
+#
+# This script requires a minimum spatch version.
+SPATCH_REQ_VERSION="1.0.4"
+
+DIR="$(dirname $(readlink -f $0))/.."
+SPATCH="`which ${SPATCH:=spatch}`"
+if [ ! -x "$SPATCH" ]; then
+	echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
+	exit 1
+fi
+
+SPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh)
+SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
+SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
+
+if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then
+	echo "spatch needs to be version $SPATCH_REQ_VERSION or higher"
+	exit 1
+fi
+
+generate_deps_for_ns() {
+	$SPATCH --very-quiet --in-place --sp-file \
+		$srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2
+}
+
+generate_deps() {
+	local mod_name=`basename $@ .ko`
+	local mod_file=`echo $@ | sed -e 's/\.ko/\.mod/'`
+	local ns_deps_file=`echo $@ | sed -e 's/\.ko/\.ns_deps/'`
+	if [ ! -f "$ns_deps_file" ]; then return; fi
+	local mod_source_files=`cat $mod_file | sed -n 1p                      \
+					      | sed -e 's/\.o/\.c/g'           \
+					      | sed "s/[^ ]* */${srctree}\/&/g"`
+	for ns in `cat $ns_deps_file`; do
+		echo "Adding namespace $ns to module $mod_name (if needed)."
+		generate_deps_for_ns $ns $mod_source_files
+		# sort the imports
+		for source_file in $mod_source_files; do
+			sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp
+			offset=$(wc -l ${source_file}.tmp | awk '{print $1;}')
+			cat $source_file | grep MODULE_IMPORT_NS | sort -u >> ${source_file}.tmp
+			tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp
+			if ! diff -q ${source_file} ${source_file}.tmp; then
+				mv ${source_file}.tmp ${source_file}
+			else
+				rm ${source_file}.tmp
+			fi
+		done
+	done
+}
+
+for f in `cat $objtree/modules.order`; do
+	generate_deps $f
+done
+
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 09/11] docs: Add documentation for Symbol Namespaces
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (7 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 08/11] scripts: Coccinelle script for " Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-06 10:32   ` [PATCH v5 10/11] usb-storage: remove single-use define for debugging Matthias Maennich
                     ` (2 subsequent siblings)
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

Describe using Symbol Namespaces from a perspective of a user. I.e.
module authors or subsystem maintainers.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 Documentation/kbuild/namespaces.rst      | 154 +++++++++++++++++++++++
 Documentation/kernel-hacking/hacking.rst |  18 +++
 2 files changed, 172 insertions(+)
 create mode 100644 Documentation/kbuild/namespaces.rst

diff --git a/Documentation/kbuild/namespaces.rst b/Documentation/kbuild/namespaces.rst
new file mode 100644
index 000000000000..982ed7b568ac
--- /dev/null
+++ b/Documentation/kbuild/namespaces.rst
@@ -0,0 +1,154 @@
+=================
+Symbol Namespaces
+=================
+
+The following document describes how to use Symbol Namespaces to structure the
+export surface of in-kernel symbols exported through the family of
+EXPORT_SYMBOL() macros.
+
+.. Table of Contents
+
+	=== 1 Introduction
+	=== 2 How to define Symbol Namespaces
+	   --- 2.1 Using the EXPORT_SYMBOL macros
+	   --- 2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
+	=== 3 How to use Symbols exported in Namespaces
+	=== 4 Loading Modules that use namespaced Symbols
+	=== 5 Automatically creating MODULE_IMPORT_NS statements
+
+1. Introduction
+===============
+
+Symbol Namespaces have been introduced as a means to structure the export
+surface of the in-kernel API. It allows subsystem maintainers to partition
+their exported symbols into separate namespaces. That is useful for
+documentation purposes (think of the SUBSYSTEM_DEBUG namespace) as well as for
+limiting the availability of a set of symbols for use in other parts of the
+kernel. As of today, modules that make use of symbols exported into namespaces,
+are required to import the namespace. Otherwise the kernel will, depending on
+its configuration, reject loading the module or warn about a missing import.
+
+2. How to define Symbol Namespaces
+==================================
+
+Symbols can be exported into namespace using different methods. All of them are
+changing the way EXPORT_SYMBOL and friends are instrumented to create ksymtab
+entries.
+
+2.1 Using the EXPORT_SYMBOL macros
+==================================
+
+In addition to the macros EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(), that allow
+exporting of kernel symbols to the kernel symbol table, variants of these are
+available to export symbols into a certain namespace: EXPORT_SYMBOL_NS() and
+EXPORT_SYMBOL_NS_GPL(). They take one additional argument: the namespace.
+Please note that due to macro expansion that argument needs to be a
+preprocessor symbol. E.g. to export the symbol `usb_stor_suspend` into the
+namespace `USB_STORAGE`, use::
+
+	EXPORT_SYMBOL_NS(usb_stor_suspend, USB_STORAGE);
+
+The corresponding ksymtab entry struct `kernel_symbol` will have the member
+`namespace` set accordingly. A symbol that is exported without a namespace will
+refer to `NULL`. There is no default namespace if none is defined. `modpost`
+and kernel/module.c make use the namespace at build time or module load time,
+respectively.
+
+2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
+=============================================
+
+Defining namespaces for all symbols of a subsystem can be very verbose and may
+become hard to maintain. Therefore a default define (DEFAULT_SYMBOL_NAMESPACE)
+is been provided, that, if set, will become the default for all EXPORT_SYMBOL()
+and EXPORT_SYMBOL_GPL() macro expansions that do not specify a namespace.
+
+There are multiple ways of specifying this define and it depends on the
+subsystem and the maintainer's preference, which one to use. The first option
+is to define the default namespace in the `Makefile` of the subsystem. E.g. to
+export all symbols defined in usb-common into the namespace USB_COMMON, add a
+line like this to drivers/usb/common/Makefile::
+
+	ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON
+
+That will affect all EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL() statements. A
+symbol exported with EXPORT_SYMBOL_NS() while this definition is present, will
+still be exported into the namespace that is passed as the namespace argument
+as this argument has preference over a default symbol namespace.
+
+A second option to define the default namespace is directly in the compilation
+unit as preprocessor statement. The above example would then read::
+
+	#undef  DEFAULT_SYMBOL_NAMESPACE
+	#define DEFAULT_SYMBOL_NAMESPACE USB_COMMON
+
+within the corresponding compilation unit before any EXPORT_SYMBOL macro is
+used.
+
+3. How to use Symbols exported in Namespaces
+============================================
+
+In order to use symbols that are exported into namespaces, kernel modules need
+to explicitly import these namespaces. Otherwise the kernel might reject to
+load the module. The module code is required to use the macro MODULE_IMPORT_NS
+for the namespaces it uses symbols from. E.g. a module using the
+usb_stor_suspend symbol from above, needs to import the namespace USB_STORAGE
+using a statement like::
+
+	MODULE_IMPORT_NS(USB_STORAGE);
+
+This will create a `modinfo` tag in the module for each imported namespace.
+This has the side effect, that the imported namespaces of a module can be
+inspected with modinfo::
+
+	$ modinfo drivers/usb/storage/ums-karma.ko
+	[...]
+	import_ns:      USB_STORAGE
+	[...]
+
+
+It is advisable to add the MODULE_IMPORT_NS() statement close to other module
+metadata definitions like MODULE_AUTHOR() or MODULE_LICENSE(). Refer to section
+5. for a way to create missing import statements automatically.
+
+4. Loading Modules that use namespaced Symbols
+==============================================
+
+At module loading time (e.g. `insmod`), the kernel will check each symbol
+referenced from the module for its availability and whether the namespace it
+might be exported to has been imported by the module. The default behaviour of
+the kernel is to reject loading modules that don't specify sufficient imports.
+An error will be logged and loading will be failed with EINVAL. In order to
+allow loading of modules that don't satisfy this precondition, a configuration
+option is available: Setting MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS=y will
+enable loading regardless, but will emit a warning.
+
+5. Automatically creating MODULE_IMPORT_NS statements
+=====================================================
+
+Missing namespaces imports can easily be detected at build time. In fact,
+modpost will emit a warning if a module uses a symbol from a namespace
+without importing it.
+MODULE_IMPORT_NS() statements will usually be added at a definite location
+(along with other module meta data). To make the life of module authors (and
+subsystem maintainers) easier, a script and make target is available to fixup
+missing imports. Fixing missing imports can be done with::
+
+	$ make nsdeps
+
+A typical scenario for module authors would be::
+
+	- write code that depends on a symbol from a not imported namespace
+	- `make`
+	- notice the warning of modpost telling about a missing import
+	- run `make nsdeps` to add the import to the correct code location
+
+For subsystem maintainers introducing a namespace, the steps are very similar.
+Again, `make nsdeps` will eventually add the missing namespace imports for
+in-tree modules::
+
+	- move or add symbols to a namespace (e.g. with EXPORT_SYMBOL_NS())
+	- `make` (preferably with an allmodconfig to cover all in-kernel
+	  modules)
+	- notice the warning of modpost telling about a missing import
+	- run `make nsdeps` to add the import to the correct code location
+
diff --git a/Documentation/kernel-hacking/hacking.rst b/Documentation/kernel-hacking/hacking.rst
index 5891a701a159..a3ddb213a5e1 100644
--- a/Documentation/kernel-hacking/hacking.rst
+++ b/Documentation/kernel-hacking/hacking.rst
@@ -594,6 +594,24 @@ internal implementation issue, and not really an interface. Some
 maintainers and developers may however require EXPORT_SYMBOL_GPL()
 when adding any new APIs or functionality.
 
+:c:func:`EXPORT_SYMBOL_NS()`
+----------------------------
+
+Defined in ``include/linux/export.h``
+
+This is the variant of `EXPORT_SYMBOL()` that allows specifying a symbol
+namespace. Symbol Namespaces are documented in
+``Documentation/kbuild/namespaces.rst``.
+
+:c:func:`EXPORT_SYMBOL_NS_GPL()`
+--------------------------------
+
+Defined in ``include/linux/export.h``
+
+This is the variant of `EXPORT_SYMBOL_GPL()` that allows specifying a symbol
+namespace. Symbol Namespaces are documented in
+``Documentation/kbuild/namespaces.rst``.
+
 Routines and Conventions
 ========================
 
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 10/11] usb-storage: remove single-use define for debugging
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (8 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 09/11] docs: Add documentation for Symbol Namespaces Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-06 12:59     ` Jessica Yu
  2019-09-06 10:32   ` [PATCH v5 11/11] usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
  2019-09-09  8:35   ` [PATCH v5 00/11] Symbol Namespaces Jessica Yu
  11 siblings, 1 reply; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

USB_STORAGE was defined as "usb-storage: " and used in a single location
as argument to printk. In order to be able to use the name
'USB_STORAGE', drop the definition and use the string directly for the
printk call.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/usb/storage/debug.h    | 2 --
 drivers/usb/storage/scsiglue.c | 2 +-
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/usb/storage/debug.h b/drivers/usb/storage/debug.h
index 6d64f342f587..16ce06039a4d 100644
--- a/drivers/usb/storage/debug.h
+++ b/drivers/usb/storage/debug.h
@@ -29,8 +29,6 @@
 
 #include <linux/kernel.h>
 
-#define USB_STORAGE "usb-storage: "
-
 #ifdef CONFIG_USB_STORAGE_DEBUG
 void usb_stor_show_command(const struct us_data *us, struct scsi_cmnd *srb);
 void usb_stor_show_sense(const struct us_data *us, unsigned char key,
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 05b80211290d..df4de8323eff 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -379,7 +379,7 @@ static int queuecommand_lck(struct scsi_cmnd *srb,
 
 	/* check for state-transition errors */
 	if (us->srb != NULL) {
-		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
+		printk(KERN_ERR "usb-storage: Error in %s: us->srb = %p\n",
 			__func__, us->srb);
 		return SCSI_MLQUEUE_HOST_BUSY;
 	}
-- 
2.23.0.187.g17f5b7556c-goog


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

* [PATCH v5 11/11] usb-storage: export symbols in USB_STORAGE namespace
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (9 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 10/11] usb-storage: remove single-use define for debugging Matthias Maennich
@ 2019-09-06 10:32   ` Matthias Maennich
  2019-09-09  8:35   ` [PATCH v5 00/11] Symbol Namespaces Jessica Yu
  11 siblings, 0 replies; 105+ messages in thread
From: Matthias Maennich @ 2019-09-06 10:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: kernel-team, maennich, arnd, gregkh, jeyu, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

Modules using these symbols are required to explicitly import the
namespace. This patch was generated with the following steps and serves
as a reference to use the symbol namespace feature:

 1) Define DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile
 2) make  (see warnings during modpost about missing imports)
 3) make nsdeps

Instead of a DEFAULT_SYMBOL_NAMESPACE definition, the EXPORT_SYMBOL_NS
variants can be used to explicitly specify the namespace. The advantage
of the method used here is that newly added symbols are automatically
exported and existing ones are exported without touching their
respective EXPORT_SYMBOL macro expansion.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 drivers/usb/storage/Makefile        | 2 ++
 drivers/usb/storage/alauda.c        | 1 +
 drivers/usb/storage/cypress_atacb.c | 1 +
 drivers/usb/storage/datafab.c       | 1 +
 drivers/usb/storage/ene_ub6250.c    | 1 +
 drivers/usb/storage/freecom.c       | 1 +
 drivers/usb/storage/isd200.c        | 1 +
 drivers/usb/storage/jumpshot.c      | 1 +
 drivers/usb/storage/karma.c         | 1 +
 drivers/usb/storage/onetouch.c      | 1 +
 drivers/usb/storage/realtek_cr.c    | 1 +
 drivers/usb/storage/sddr09.c        | 1 +
 drivers/usb/storage/sddr55.c        | 1 +
 drivers/usb/storage/shuttle_usbat.c | 1 +
 drivers/usb/storage/uas.c           | 1 +
 15 files changed, 16 insertions(+)

diff --git a/drivers/usb/storage/Makefile b/drivers/usb/storage/Makefile
index a67ddcbb4e24..46635fa4a340 100644
--- a/drivers/usb/storage/Makefile
+++ b/drivers/usb/storage/Makefile
@@ -8,6 +8,8 @@
 
 ccflags-y := -I $(srctree)/drivers/scsi
 
+ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_STORAGE
+
 obj-$(CONFIG_USB_UAS)		+= uas.o
 obj-$(CONFIG_USB_STORAGE)	+= usb-storage.o
 
diff --git a/drivers/usb/storage/alauda.c b/drivers/usb/storage/alauda.c
index 6b8edf6178df..ddab2cd3d2e7 100644
--- a/drivers/usb/storage/alauda.c
+++ b/drivers/usb/storage/alauda.c
@@ -36,6 +36,7 @@
 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * Status bytes
diff --git a/drivers/usb/storage/cypress_atacb.c b/drivers/usb/storage/cypress_atacb.c
index 4825902377eb..a6f3267bbef6 100644
--- a/drivers/usb/storage/cypress_atacb.c
+++ b/drivers/usb/storage/cypress_atacb.c
@@ -22,6 +22,7 @@
 MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
 MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
index 09353be199be..588818483f4b 100644
--- a/drivers/usb/storage/datafab.c
+++ b/drivers/usb/storage/datafab.c
@@ -54,6 +54,7 @@
 MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
 MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 struct datafab_info {
 	unsigned long   sectors;	/* total sector count */
diff --git a/drivers/usb/storage/ene_ub6250.c b/drivers/usb/storage/ene_ub6250.c
index c26129d5b943..8b1b73065421 100644
--- a/drivers/usb/storage/ene_ub6250.c
+++ b/drivers/usb/storage/ene_ub6250.c
@@ -26,6 +26,7 @@
 
 MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 MODULE_FIRMWARE(SD_INIT1_FIRMWARE);
 MODULE_FIRMWARE(SD_INIT2_FIRMWARE);
 MODULE_FIRMWARE(SD_RW_FIRMWARE);
diff --git a/drivers/usb/storage/freecom.c b/drivers/usb/storage/freecom.c
index 4f542df37a44..34e7eaff1174 100644
--- a/drivers/usb/storage/freecom.c
+++ b/drivers/usb/storage/freecom.c
@@ -29,6 +29,7 @@
 MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor");
 MODULE_AUTHOR("David Brown <usb-storage@davidb.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #ifdef CONFIG_USB_STORAGE_DEBUG
 static void pdump(struct us_data *us, void *ibuffer, int length);
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index 2b474d60b4db..c4da3fd6eff9 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -53,6 +53,7 @@
 MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
 MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int isd200_Initialization(struct us_data *us);
 
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
index 917f170c4124..229bf0c1afc9 100644
--- a/drivers/usb/storage/jumpshot.c
+++ b/drivers/usb/storage/jumpshot.c
@@ -51,6 +51,7 @@
 MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
 MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/karma.c b/drivers/usb/storage/karma.c
index 395cf8fb5870..05cec81dcd3f 100644
--- a/drivers/usb/storage/karma.c
+++ b/drivers/usb/storage/karma.c
@@ -23,6 +23,7 @@
 MODULE_DESCRIPTION("Driver for Rio Karma");
 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>, Keith Bennett <keith@mcs.st-and.ac.uk>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #define RIO_PREFIX "RIOP\x00"
 #define RIO_PREFIX_LEN 5
diff --git a/drivers/usb/storage/onetouch.c b/drivers/usb/storage/onetouch.c
index 39a5009a41a6..a989fe930e21 100644
--- a/drivers/usb/storage/onetouch.c
+++ b/drivers/usb/storage/onetouch.c
@@ -25,6 +25,7 @@
 MODULE_DESCRIPTION("Maxtor USB OneTouch hard drive button driver");
 MODULE_AUTHOR("Nick Sillik <n.sillik@temple.edu>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 #define ONETOUCH_PKT_LEN        0x02
 #define ONETOUCH_BUTTON         KEY_PROG1
diff --git a/drivers/usb/storage/realtek_cr.c b/drivers/usb/storage/realtek_cr.c
index 1d9ce9cbc831..3789698d9d3c 100644
--- a/drivers/usb/storage/realtek_cr.c
+++ b/drivers/usb/storage/realtek_cr.c
@@ -35,6 +35,7 @@
 MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
 MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int auto_delink_en = 1;
 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
diff --git a/drivers/usb/storage/sddr09.c b/drivers/usb/storage/sddr09.c
index bc9da736bdfc..51bcd4a43690 100644
--- a/drivers/usb/storage/sddr09.c
+++ b/drivers/usb/storage/sddr09.c
@@ -47,6 +47,7 @@
 MODULE_DESCRIPTION("Driver for SanDisk SDDR-09 SmartMedia reader");
 MODULE_AUTHOR("Andries Brouwer <aeb@cwi.nl>, Robert Baruch <autophile@starband.net>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 static int usb_stor_sddr09_dpcm_init(struct us_data *us);
 static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us);
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index b8527c55335b..ba955d65eb0e 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -29,6 +29,7 @@
 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
 MODULE_AUTHOR("Simon Munton");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /*
  * The table of devices
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 854498e1012c..54aa1392c9ca 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -48,6 +48,7 @@
 MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 
 /* Supported device types */
 #define USBAT_DEV_HP8200	0x01
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index 047c5922618f..bf80d6f81f58 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -1219,5 +1219,6 @@ static struct usb_driver uas_driver = {
 module_usb_driver(uas_driver);
 
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(USB_STORAGE);
 MODULE_AUTHOR(
 	"Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");
-- 
2.23.0.187.g17f5b7556c-goog


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

* Re: [PATCH v5 10/11] usb-storage: remove single-use define for debugging
  2019-09-06 10:32   ` [PATCH v5 10/11] usb-storage: remove single-use define for debugging Matthias Maennich
@ 2019-09-06 12:59     ` Jessica Yu
  2019-09-06 13:22       ` Greg KH
  0 siblings, 1 reply; 105+ messages in thread
From: Jessica Yu @ 2019-09-06 12:59 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, gregkh, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

+++ Matthias Maennich [06/09/19 11:32 +0100]:
>USB_STORAGE was defined as "usb-storage: " and used in a single location
>as argument to printk. In order to be able to use the name
>'USB_STORAGE', drop the definition and use the string directly for the
>printk call.
>
>Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>Signed-off-by: Matthias Maennich <maennich@google.com>

Greg, didn't you pick this patch up for usb-next already (for the 5.4
merge window)? If that's the case, I could apply the series omitting
this patch (once there are no more comments or complaints).

Thanks!

Jessica


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

* Re: [PATCH v5 10/11] usb-storage: remove single-use define for debugging
  2019-09-06 12:59     ` Jessica Yu
@ 2019-09-06 13:22       ` Greg KH
  0 siblings, 0 replies; 105+ messages in thread
From: Greg KH @ 2019-09-06 13:22 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Matthias Maennich, linux-kernel, kernel-team, arnd, joel,
	lucas.de.marchi, maco, sspatil, will, yamada.masahiro,
	linux-kbuild, linux-modules, linux-usb, usb-storage

On Fri, Sep 06, 2019 at 02:59:42PM +0200, Jessica Yu wrote:
> +++ Matthias Maennich [06/09/19 11:32 +0100]:
> > USB_STORAGE was defined as "usb-storage: " and used in a single location
> > as argument to printk. In order to be able to use the name
> > 'USB_STORAGE', drop the definition and use the string directly for the
> > printk call.
> > 
> > Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Signed-off-by: Matthias Maennich <maennich@google.com>
> 
> Greg, didn't you pick this patch up for usb-next already (for the 5.4
> merge window)? If that's the case, I could apply the series omitting
> this patch (once there are no more comments or complaints).

Yes, I already have this in my tree, but there's no problem with you
taking it in yours as I think it's needed for the one after this to
build properly.

git will handle it properly when merging :)

thanks!

greg k-h

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

* Re: [PATCH v5 00/11] Symbol Namespaces
  2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
                     ` (10 preceding siblings ...)
  2019-09-06 10:32   ` [PATCH v5 11/11] usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
@ 2019-09-09  8:35   ` Jessica Yu
  11 siblings, 0 replies; 105+ messages in thread
From: Jessica Yu @ 2019-09-09  8:35 UTC (permalink / raw)
  To: Matthias Maennich
  Cc: linux-kernel, kernel-team, arnd, gregkh, joel, lucas.de.marchi,
	maco, sspatil, will, yamada.masahiro, linux-kbuild,
	linux-modules, linux-usb, usb-storage

+++ Matthias Maennich [06/09/19 11:32 +0100]:
>As of Linux 5.3-rc7, there are 31207 [1] exported symbols in the kernel.
>That is a growth of roughly 1000 symbols since 4.17 (30206 [2]). There
>seems to be some consensus amongst kernel devs that the export surface
>is too large, and hard to reason about.
>
>Generally, these symbols fall in one of these categories:
>1) Symbols actually meant for drivers
>2) Symbols that are only exported because functionality is split over
>   multiple modules, yet they really shouldn't be used by modules outside
>   of their own subsystem
>3) Symbols really only meant for in-tree use
>
>When module developers try to upstream their code, it regularly turns
>out that they are using exported symbols that they really shouldn't be
>using. This problem is even bigger for drivers that are currently
>out-of-tree, which may be using many symbols that they shouldn't be
>using, and that break when those symbols are removed or modified.
>
>This patch allows subsystem maintainers to partition their exported
>symbols into separate namespaces, and module authors to import such
>namespaces only when needed.
>
>This allows subsystem maintainers to more easily limit availability of
>these namespaced symbols to other parts of the kernel. It can also be
>used to partition the set of exported symbols for documentation
>purposes; for example, a set of symbols that is really only used for
>debugging could be in a "SUBSYSTEM_DEBUG" namespace.
>
>I continued the work mainly done by Martijn Coenen.
>
>Changes in v2:
>- Rather than adding and evaluating separate sections __knsimport_NS,
>  use modinfo tags to declare the namespaces a module introduces.
>  Adjust modpost and the module loader accordingly.
>- Also add support for reading multiple modinfo values for the same tag
>  to allow list-like access to modinfo tags.
>- The macros in export.h have been cleaned up to avoid redundancy in the
>  macro parameters (ns, nspost, nspost2).
>- The introduction of relative references in the ksymtab entries caused
>  a rework of the macros to accommodate that configuration as well.
>- Alignment of kernel_symbol in the ksymtab needed to be fixed to allow
>  growing the kernel_symbol struct.
>- Modpost does now also append the namespace suffix to the symbol
>  entries in Module.symvers.
>- The configuration option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS allows
>  relaxing the enforcement of properly declared namespace imports at
>  module loading time.
>- Symbols can be collectively exported into a namespace by defining
>  DEFAULT_SYMBOL_NAMESPACE in the corresponding Makefile.
>- The requirement for a very recent coccinelle spatch has been lifted by
>  simplifying the script.
>- nsdeps does now ensures MODULE_IMPORT_NS statements are sorted when
>  patching the module source files.
>- Some minor bugs have been addressed in nsdeps to allow it to work with
>  modules that have more than one source file.
>- The RFC for the usb-storage symbols has been simplified by using
>  DEFAULT_SYMBOL_NAMESPACE=USB_STORAGE rather than explicitly exporting
>  each and every symbol into that new namespace.
>
>Changes in v3:
>- Reword the documentation for the
>  MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS option for clarification.
>- Fix printed required version of spatch in coccinelle script.
>- Adopt kbuild changes for modpost: .mod files are no longer generated
>  in .tmp_versions. Similarely, generate the .ns_deps files in the tree
>  along with the .mod files. Also, nsdeps now uses modules.order as
>  source for the list modules to consider.
>- Add an RFC patch to introduce the namespace WATCHDOG_CORE for symbols
>  exported in watchdog_core.c.
>
>Changes in v4:
>- scripts/nsdeps:
>  - exit on first error
>  - support out-of-tree builds O=...
>- scripts/export_report.pl: update for new Module.symvers format
>- scripts/mod/modpost: make the namespace a separate field when
>  exporting to Module.symvers (rather than symbol.NS)
>- include/linux/export.h: fixed style nits
>- kernel/module.c: ensure namespaces are imported before taking a
>  reference to the owner module
>- Documentation: document the Symbol Namespace feature and update
>  references to Module.symvers and EXPORT_SYMBOL*
>
>Changes in v5:
>- Makefile: let 'nsdeps' depend on 'modules' to allow
>  `make clean; make nsdeps` to work
>- scripts/nsdeps: drop 'exit on first error' again as it just makes more
>  problems than it solves
>- drop the watchdog RFC patch for now
>
>This patch series was developed against v5.3-rc7.

Great work Matthias!

I think this patchset is shaping up nicely. As the merge window is
coming up soon, I'd like to queue this up in modules-next by the end
of today to allow for some testing and "soak" time in linux-next. If
there are any more complaints, please speak up.

Thanks!

Jessica

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

end of thread, back to index

Thread overview: 105+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20180716122125.175792-1-maco@android.com>
2019-08-13 12:16 ` [PATCH v2 0/10] Symbol namespaces - RFC Matthias Maennich
2019-08-13 12:16   ` [PATCH v2 01/10] module: support reading multiple values per modinfo tag Matthias Maennich
2019-08-13 12:40     ` Greg KH
2019-08-13 12:16   ` [PATCH v2 02/10] export: explicitly align struct kernel_symbol Matthias Maennich
2019-08-13 12:41     ` Greg KH
2019-08-13 12:17   ` [PATCH v2 03/10] module: add support for symbol namespaces Matthias Maennich
2019-08-13 15:26     ` Greg KH
2019-08-13 12:17   ` [PATCH v2 04/10] modpost: " Matthias Maennich
2019-08-13 15:27     ` Greg KH
2019-08-13 12:17   ` [PATCH v2 05/10] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
2019-08-13 18:17     ` Greg KH
2019-08-13 20:15     ` Saravana Kannan
2019-08-14 12:54       ` Matthias Maennich
2019-08-14 17:34         ` Saravana Kannan
2019-08-13 12:17   ` [PATCH v2 06/10] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
2019-08-13 18:16     ` Greg KH
2019-08-13 18:16     ` Greg KH
2019-08-13 12:17   ` [PATCH v2 07/10] modpost: add support for generating namespace dependencies Matthias Maennich
2019-08-13 18:21     ` Greg KH
2019-08-13 12:17   ` [PATCH v2 08/10] scripts: Coccinelle script for " Matthias Maennich
2019-08-13 12:31     ` Julia Lawall
2019-08-13 12:44     ` Greg KH
2019-08-14  6:36     ` [Cocci] " Himanshu Jha
2019-08-14  8:03       ` Matthias Maennich
2019-08-14 12:00     ` [v2 " Markus Elfring
2019-08-15 13:50     ` Markus Elfring
2019-08-22  9:18       ` Matthias Maennich
2019-08-22 11:00         ` Markus Elfring
2019-08-13 12:17   ` [PATCH v2 09/10] usb-storage: remove single-use define for debugging Matthias Maennich
2019-08-13 12:42     ` Greg KH
2019-08-13 13:12       ` Greg KH
2019-08-13 12:17   ` [PATCH v2 10/10] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
2019-08-13 12:45     ` Greg KH
2019-08-13 12:47     ` Greg KH
2019-08-13 15:02       ` Matthias Maennich
     [not found]   ` <20190821114955.12788-1-maennich@google.com>
2019-08-21 11:49     ` [PATCH v3 01/11] module: support reading multiple values per modinfo tag Matthias Maennich
2019-08-21 11:49     ` [PATCH v3 02/11] export: explicitly align struct kernel_symbol Matthias Maennich
2019-08-21 11:49     ` [PATCH v3 03/11] module: add support for symbol namespaces Matthias Maennich
2019-08-27 15:37       ` Jessica Yu
2019-08-27 16:04         ` Matthias Maennich
2019-08-21 11:49     ` [PATCH v3 04/11] modpost: " Matthias Maennich
2019-08-26 16:21       ` Jessica Yu
2019-08-27 14:41         ` Matthias Maennich
2019-08-28  9:43           ` Jessica Yu
2019-08-28  9:55             ` Matthias Maennich
2019-08-28 10:16               ` Jessica Yu
2019-08-21 11:49     ` [PATCH v3 05/11] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
2019-08-21 11:49     ` [PATCH v3 06/11] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
2019-08-28 10:49       ` Jessica Yu
2019-08-28 10:56         ` Matthias Maennich
2019-08-21 11:49     ` [PATCH v3 07/11] modpost: add support for generating namespace dependencies Matthias Maennich
2019-08-21 11:49     ` [PATCH v3 08/11] scripts: Coccinelle script for " Matthias Maennich
2019-08-22  6:09       ` [v3 " Markus Elfring
2019-08-29 12:13       ` [PATCH v3 " Jessica Yu
2019-08-21 11:49     ` [PATCH v3 09/11] usb-storage: remove single-use define for debugging Matthias Maennich
2019-08-21 12:37       ` Greg KH
2019-08-21 13:21       ` Thomas Gleixner
2019-08-21 13:32         ` Greg KH
2019-08-21 11:49     ` [PATCH v3 10/11] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
2019-08-21 12:38       ` Greg KH
2019-08-21 14:36         ` Jessica Yu
2019-08-21 23:13       ` Christoph Hellwig
2019-08-22  8:32         ` Matthias Maennich
2019-09-03 15:06 ` [PATCH v4 00/12] Symbol Namespaces Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 01/12] module: support reading multiple values per modinfo tag Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 02/12] export: explicitly align struct kernel_symbol Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 03/12] module: add support for symbol namespaces Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 04/12] modpost: " Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 05/12] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 06/12] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 07/12] modpost: add support for generating namespace dependencies Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 08/12] scripts: Coccinelle script for " Matthias Maennich
2019-09-04  9:53     ` Masahiro Yamada
2019-09-05 14:46       ` Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 09/12] docs: Add documentation for Symbol Namespaces Matthias Maennich
2019-09-04  7:16     ` Greg KH
2019-09-03 15:06   ` [PATCH v4 10/12] usb-storage: remove single-use define for debugging Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 11/12] RFC: usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
2019-09-03 15:06   ` [PATCH v4 12/12] RFC: watchdog: export core symbols in WATCHDOG_CORE namespace Matthias Maennich
2019-09-03 16:10     ` Guenter Roeck
2019-09-04  8:45       ` Masahiro Yamada
2019-09-04 12:12         ` Guenter Roeck
2019-09-04 16:16           ` [usb-storage] " Matthew Dharm
2019-09-05 10:41             ` Jessica Yu
2019-09-05 10:52               ` Arnd Bergmann
2019-09-05 11:16                 ` Jessica Yu
2019-09-05 11:25                   ` Masahiro Yamada
2019-09-05 12:00                     ` Greg Kroah-Hartman
2019-09-05 11:25               ` Matthias Maennich
2019-09-04  9:37   ` [PATCH v4 00/12] Symbol Namespaces Masahiro Yamada
2019-09-06 10:32 ` [PATCH v5 00/11] " Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 01/11] module: support reading multiple values per modinfo tag Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 02/11] export: explicitly align struct kernel_symbol Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 03/11] module: add support for symbol namespaces Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 04/11] modpost: " Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 05/11] module: add config option MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 06/11] export: allow definition default namespaces in Makefiles or sources Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 07/11] modpost: add support for generating namespace dependencies Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 08/11] scripts: Coccinelle script for " Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 09/11] docs: Add documentation for Symbol Namespaces Matthias Maennich
2019-09-06 10:32   ` [PATCH v5 10/11] usb-storage: remove single-use define for debugging Matthias Maennich
2019-09-06 12:59     ` Jessica Yu
2019-09-06 13:22       ` Greg KH
2019-09-06 10:32   ` [PATCH v5 11/11] usb-storage: export symbols in USB_STORAGE namespace Matthias Maennich
2019-09-09  8:35   ` [PATCH v5 00/11] Symbol Namespaces Jessica Yu

Linux-USB Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-usb/0 linux-usb/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-usb linux-usb/ https://lore.kernel.org/linux-usb \
		linux-usb@vger.kernel.org linux-usb@archiver.kernel.org
	public-inbox-index linux-usb


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-usb


AGPL code for this site: git clone https://public-inbox.org/ public-inbox