xref: /linux/Documentation/core-api/symbol-namespaces.rst (revision db59d74e5da144111fc133fb1bf72e6392bdb04e)
1=================
2Symbol Namespaces
3=================
4
5The following document describes how to use Symbol Namespaces to structure the
6export surface of in-kernel symbols exported through the family of
7EXPORT_SYMBOL() macros.
8
9.. Table of Contents
10
11	=== 1 Introduction
12	=== 2 How to define Symbol Namespaces
13	   --- 2.1 Using the EXPORT_SYMBOL macros
14	   --- 2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
15	=== 3 How to use Symbols exported in Namespaces
16	=== 4 Loading Modules that use namespaced Symbols
17	=== 5 Automatically creating MODULE_IMPORT_NS statements
18
191. Introduction
20===============
21
22Symbol Namespaces have been introduced as a means to structure the export
23surface of the in-kernel API. It allows subsystem maintainers to partition
24their exported symbols into separate namespaces. That is useful for
25documentation purposes (think of the SUBSYSTEM_DEBUG namespace) as well as for
26limiting the availability of a set of symbols for use in other parts of the
27kernel. As of today, modules that make use of symbols exported into namespaces,
28are required to import the namespace. Otherwise the kernel will, depending on
29its configuration, reject loading the module or warn about a missing import.
30
31Additionally, it is possible to put symbols into a module namespace, strictly
32limiting which modules are allowed to use these symbols.
33
342. How to define Symbol Namespaces
35==================================
36
37Symbols can be exported into namespace using different methods. All of them are
38changing the way EXPORT_SYMBOL and friends are instrumented to create ksymtab
39entries.
40
412.1 Using the EXPORT_SYMBOL macros
42==================================
43
44In addition to the macros EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(), that allow
45exporting of kernel symbols to the kernel symbol table, variants of these are
46available to export symbols into a certain namespace: EXPORT_SYMBOL_NS() and
47EXPORT_SYMBOL_NS_GPL(). They take one additional argument: the namespace as a
48string constant. Note that this string must not contain whitespaces.
49E.g. to export the symbol ``usb_stor_suspend`` into the
50namespace ``USB_STORAGE``, use::
51
52	EXPORT_SYMBOL_NS(usb_stor_suspend, "USB_STORAGE");
53
54The corresponding ksymtab entry struct ``kernel_symbol`` will have the member
55``namespace`` set accordingly. A symbol that is exported without a namespace will
56refer to ``NULL``. There is no default namespace if none is defined. ``modpost``
57and kernel/module/main.c make use the namespace at build time or module load
58time, respectively.
59
602.2 Using the DEFAULT_SYMBOL_NAMESPACE define
61=============================================
62
63Defining namespaces for all symbols of a subsystem can be very verbose and may
64become hard to maintain. Therefore a default define (DEFAULT_SYMBOL_NAMESPACE)
65is been provided, that, if set, will become the default for all EXPORT_SYMBOL()
66and EXPORT_SYMBOL_GPL() macro expansions that do not specify a namespace.
67
68There are multiple ways of specifying this define and it depends on the
69subsystem and the maintainer's preference, which one to use. The first option
70is to define the default namespace in the ``Makefile`` of the subsystem. E.g. to
71export all symbols defined in usb-common into the namespace USB_COMMON, add a
72line like this to drivers/usb/common/Makefile::
73
74	ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE='"USB_COMMON"'
75
76That will affect all EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL() statements. A
77symbol exported with EXPORT_SYMBOL_NS() while this definition is present, will
78still be exported into the namespace that is passed as the namespace argument
79as this argument has preference over a default symbol namespace.
80
81A second option to define the default namespace is directly in the compilation
82unit as preprocessor statement. The above example would then read::
83
84	#define DEFAULT_SYMBOL_NAMESPACE "USB_COMMON"
85
86within the corresponding compilation unit before the #include for
87<linux/export.h>. Typically it's placed before the first #include statement.
88
892.3 Using the EXPORT_SYMBOL_GPL_FOR_MODULES() macro
90===================================================
91
92Symbols exported using this macro are put into a module namespace. This
93namespace cannot be imported.
94
95The macro takes a comma separated list of module names, allowing only those
96modules to access this symbol. Simple tail-globs are supported.
97
98For example:
99
100  EXPORT_SYMBOL_GPL_FOR_MODULES(preempt_notifier_inc, "kvm,kvm-*")
101
102will limit usage of this symbol to modules whoes name matches the given
103patterns.
104
1053. How to use Symbols exported in Namespaces
106============================================
107
108In order to use symbols that are exported into namespaces, kernel modules need
109to explicitly import these namespaces. Otherwise the kernel might reject to
110load the module. The module code is required to use the macro MODULE_IMPORT_NS
111for the namespaces it uses symbols from. E.g. a module using the
112usb_stor_suspend symbol from above, needs to import the namespace USB_STORAGE
113using a statement like::
114
115	MODULE_IMPORT_NS("USB_STORAGE");
116
117This will create a ``modinfo`` tag in the module for each imported namespace.
118This has the side effect, that the imported namespaces of a module can be
119inspected with modinfo::
120
121	$ modinfo drivers/usb/storage/ums-karma.ko
122	[...]
123	import_ns:      USB_STORAGE
124	[...]
125
126
127It is advisable to add the MODULE_IMPORT_NS() statement close to other module
128metadata definitions like MODULE_AUTHOR() or MODULE_LICENSE(). Refer to section
1295. for a way to create missing import statements automatically.
130
1314. Loading Modules that use namespaced Symbols
132==============================================
133
134At module loading time (e.g. ``insmod``), the kernel will check each symbol
135referenced from the module for its availability and whether the namespace it
136might be exported to has been imported by the module. The default behaviour of
137the kernel is to reject loading modules that don't specify sufficient imports.
138An error will be logged and loading will be failed with EINVAL. In order to
139allow loading of modules that don't satisfy this precondition, a configuration
140option is available: Setting MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS=y will
141enable loading regardless, but will emit a warning.
142
1435. Automatically creating MODULE_IMPORT_NS statements
144=====================================================
145
146Missing namespaces imports can easily be detected at build time. In fact,
147modpost will emit a warning if a module uses a symbol from a namespace
148without importing it.
149MODULE_IMPORT_NS() statements will usually be added at a definite location
150(along with other module meta data). To make the life of module authors (and
151subsystem maintainers) easier, a script and make target is available to fixup
152missing imports. Fixing missing imports can be done with::
153
154	$ make nsdeps
155
156A typical scenario for module authors would be::
157
158	- write code that depends on a symbol from a not imported namespace
159	- ``make``
160	- notice the warning of modpost telling about a missing import
161	- run ``make nsdeps`` to add the import to the correct code location
162
163For subsystem maintainers introducing a namespace, the steps are very similar.
164Again, ``make nsdeps`` will eventually add the missing namespace imports for
165in-tree modules::
166
167	- move or add symbols to a namespace (e.g. with EXPORT_SYMBOL_NS())
168	- ``make`` (preferably with an allmodconfig to cover all in-kernel
169	  modules)
170	- notice the warning of modpost telling about a missing import
171	- run ``make nsdeps`` to add the import to the correct code location
172
173You can also run nsdeps for external module builds. A typical usage is::
174
175	$ make -C <path_to_kernel_src> M=$PWD nsdeps
176
177Note: it will happily generate an import statement for the module namespace;
178which will not work and generates build and runtime failures.
179