Skip to content

Commit 707f853

Browse files
Peter Zijlstramasahir0y
authored andcommitted
module: Provide EXPORT_SYMBOL_GPL_FOR_MODULES() helper
Helper macro to more easily limit the export of a symbol to a given list of modules. Eg: EXPORT_SYMBOL_GPL_FOR_MODULES(preempt_notifier_inc, "kvm"); will limit the use of said function to kvm.ko, any other module trying to use this symbol will refure to load (and get modpost build failures). Requested-by: Masahiro Yamada <[email protected]> Requested-by: Christoph Hellwig <[email protected]> Signed-off-by: Peter Zijlstra <[email protected]> Reviewed-by: Petr Pavlu <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 0267cbf commit 707f853

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Documentation/core-api/symbol-namespaces.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ kernel. As of today, modules that make use of symbols exported into namespaces,
2828
are required to import the namespace. Otherwise the kernel will, depending on
2929
its configuration, reject loading the module or warn about a missing import.
3030

31+
Additionally, it is possible to put symbols into a module namespace, strictly
32+
limiting which modules are allowed to use these symbols.
33+
3134
2. How to define Symbol Namespaces
3235
==================================
3336

@@ -83,6 +86,22 @@ unit as preprocessor statement. The above example would then read::
8386
within the corresponding compilation unit before the #include for
8487
<linux/export.h>. Typically it's placed before the first #include statement.
8588

89+
2.3 Using the EXPORT_SYMBOL_GPL_FOR_MODULES() macro
90+
===================================================
91+
92+
Symbols exported using this macro are put into a module namespace. This
93+
namespace cannot be imported.
94+
95+
The macro takes a comma separated list of module names, allowing only those
96+
modules to access this symbol. Simple tail-globs are supported.
97+
98+
For example:
99+
100+
EXPORT_SYMBOL_GPL_FOR_MODULES(preempt_notifier_inc, "kvm,kvm-*")
101+
102+
will limit usage of this symbol to modules whoes name matches the given
103+
patterns.
104+
86105
3. How to use Symbols exported in Namespaces
87106
============================================
88107

@@ -154,3 +173,6 @@ in-tree modules::
154173
You can also run nsdeps for external module builds. A typical usage is::
155174

156175
$ make -C <path_to_kernel_src> M=$PWD nsdeps
176+
177+
Note: it will happily generate an import statement for the module namespace;
178+
which will not work and generates build and runtime failures.

include/linux/export.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
.long sym
2525
#endif
2626

27-
#define ___EXPORT_SYMBOL(sym, license, ns) \
27+
/*
28+
* LLVM integrated assembler cam merge adjacent string literals (like
29+
* C and GNU-as) passed to '.ascii', but not to '.asciz' and chokes on:
30+
*
31+
* .asciz "MODULE_" "kvm" ;
32+
*/
33+
#define ___EXPORT_SYMBOL(sym, license, ns...) \
2834
.section ".export_symbol","a" ASM_NL \
2935
__export_symbol_##sym: ASM_NL \
3036
.asciz license ASM_NL \
31-
.asciz ns ASM_NL \
37+
.ascii ns "\0" ASM_NL \
3238
__EXPORT_SYMBOL_REF(sym) ASM_NL \
3339
.previous
3440

@@ -85,4 +91,6 @@
8591
#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", ns)
8692
#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "GPL", ns)
8793

94+
#define EXPORT_SYMBOL_GPL_FOR_MODULES(sym, mods) __EXPORT_SYMBOL(sym, "GPL", "module:" mods)
95+
8896
#endif /* _LINUX_EXPORT_H */

0 commit comments

Comments
 (0)