xref: /OK3568_Linux_fs/kernel/Documentation/core-api/symbol-namespaces.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=================
2*4882a593SmuzhiyunSymbol Namespaces
3*4882a593Smuzhiyun=================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThe following document describes how to use Symbol Namespaces to structure the
6*4882a593Smuzhiyunexport surface of in-kernel symbols exported through the family of
7*4882a593SmuzhiyunEXPORT_SYMBOL() macros.
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun.. Table of Contents
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun	=== 1 Introduction
12*4882a593Smuzhiyun	=== 2 How to define Symbol Namespaces
13*4882a593Smuzhiyun	   --- 2.1 Using the EXPORT_SYMBOL macros
14*4882a593Smuzhiyun	   --- 2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
15*4882a593Smuzhiyun	=== 3 How to use Symbols exported in Namespaces
16*4882a593Smuzhiyun	=== 4 Loading Modules that use namespaced Symbols
17*4882a593Smuzhiyun	=== 5 Automatically creating MODULE_IMPORT_NS statements
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun1. Introduction
20*4882a593Smuzhiyun===============
21*4882a593Smuzhiyun
22*4882a593SmuzhiyunSymbol Namespaces have been introduced as a means to structure the export
23*4882a593Smuzhiyunsurface of the in-kernel API. It allows subsystem maintainers to partition
24*4882a593Smuzhiyuntheir exported symbols into separate namespaces. That is useful for
25*4882a593Smuzhiyundocumentation purposes (think of the SUBSYSTEM_DEBUG namespace) as well as for
26*4882a593Smuzhiyunlimiting the availability of a set of symbols for use in other parts of the
27*4882a593Smuzhiyunkernel. As of today, modules that make use of symbols exported into namespaces,
28*4882a593Smuzhiyunare required to import the namespace. Otherwise the kernel will, depending on
29*4882a593Smuzhiyunits configuration, reject loading the module or warn about a missing import.
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun2. How to define Symbol Namespaces
32*4882a593Smuzhiyun==================================
33*4882a593Smuzhiyun
34*4882a593SmuzhiyunSymbols can be exported into namespace using different methods. All of them are
35*4882a593Smuzhiyunchanging the way EXPORT_SYMBOL and friends are instrumented to create ksymtab
36*4882a593Smuzhiyunentries.
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun2.1 Using the EXPORT_SYMBOL macros
39*4882a593Smuzhiyun==================================
40*4882a593Smuzhiyun
41*4882a593SmuzhiyunIn addition to the macros EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(), that allow
42*4882a593Smuzhiyunexporting of kernel symbols to the kernel symbol table, variants of these are
43*4882a593Smuzhiyunavailable to export symbols into a certain namespace: EXPORT_SYMBOL_NS() and
44*4882a593SmuzhiyunEXPORT_SYMBOL_NS_GPL(). They take one additional argument: the namespace.
45*4882a593SmuzhiyunPlease note that due to macro expansion that argument needs to be a
46*4882a593Smuzhiyunpreprocessor symbol. E.g. to export the symbol `usb_stor_suspend` into the
47*4882a593Smuzhiyunnamespace `USB_STORAGE`, use::
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun	EXPORT_SYMBOL_NS(usb_stor_suspend, USB_STORAGE);
50*4882a593Smuzhiyun
51*4882a593SmuzhiyunThe corresponding ksymtab entry struct `kernel_symbol` will have the member
52*4882a593Smuzhiyun`namespace` set accordingly. A symbol that is exported without a namespace will
53*4882a593Smuzhiyunrefer to `NULL`. There is no default namespace if none is defined. `modpost`
54*4882a593Smuzhiyunand kernel/module.c make use the namespace at build time or module load time,
55*4882a593Smuzhiyunrespectively.
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
58*4882a593Smuzhiyun=============================================
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunDefining namespaces for all symbols of a subsystem can be very verbose and may
61*4882a593Smuzhiyunbecome hard to maintain. Therefore a default define (DEFAULT_SYMBOL_NAMESPACE)
62*4882a593Smuzhiyunis been provided, that, if set, will become the default for all EXPORT_SYMBOL()
63*4882a593Smuzhiyunand EXPORT_SYMBOL_GPL() macro expansions that do not specify a namespace.
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunThere are multiple ways of specifying this define and it depends on the
66*4882a593Smuzhiyunsubsystem and the maintainer's preference, which one to use. The first option
67*4882a593Smuzhiyunis to define the default namespace in the `Makefile` of the subsystem. E.g. to
68*4882a593Smuzhiyunexport all symbols defined in usb-common into the namespace USB_COMMON, add a
69*4882a593Smuzhiyunline like this to drivers/usb/common/Makefile::
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun	ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON
72*4882a593Smuzhiyun
73*4882a593SmuzhiyunThat will affect all EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL() statements. A
74*4882a593Smuzhiyunsymbol exported with EXPORT_SYMBOL_NS() while this definition is present, will
75*4882a593Smuzhiyunstill be exported into the namespace that is passed as the namespace argument
76*4882a593Smuzhiyunas this argument has preference over a default symbol namespace.
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunA second option to define the default namespace is directly in the compilation
79*4882a593Smuzhiyununit as preprocessor statement. The above example would then read::
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun	#undef  DEFAULT_SYMBOL_NAMESPACE
82*4882a593Smuzhiyun	#define DEFAULT_SYMBOL_NAMESPACE USB_COMMON
83*4882a593Smuzhiyun
84*4882a593Smuzhiyunwithin the corresponding compilation unit before any EXPORT_SYMBOL macro is
85*4882a593Smuzhiyunused.
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun3. How to use Symbols exported in Namespaces
88*4882a593Smuzhiyun============================================
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunIn order to use symbols that are exported into namespaces, kernel modules need
91*4882a593Smuzhiyunto explicitly import these namespaces. Otherwise the kernel might reject to
92*4882a593Smuzhiyunload the module. The module code is required to use the macro MODULE_IMPORT_NS
93*4882a593Smuzhiyunfor the namespaces it uses symbols from. E.g. a module using the
94*4882a593Smuzhiyunusb_stor_suspend symbol from above, needs to import the namespace USB_STORAGE
95*4882a593Smuzhiyunusing a statement like::
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun	MODULE_IMPORT_NS(USB_STORAGE);
98*4882a593Smuzhiyun
99*4882a593SmuzhiyunThis will create a `modinfo` tag in the module for each imported namespace.
100*4882a593SmuzhiyunThis has the side effect, that the imported namespaces of a module can be
101*4882a593Smuzhiyuninspected with modinfo::
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun	$ modinfo drivers/usb/storage/ums-karma.ko
104*4882a593Smuzhiyun	[...]
105*4882a593Smuzhiyun	import_ns:      USB_STORAGE
106*4882a593Smuzhiyun	[...]
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun
109*4882a593SmuzhiyunIt is advisable to add the MODULE_IMPORT_NS() statement close to other module
110*4882a593Smuzhiyunmetadata definitions like MODULE_AUTHOR() or MODULE_LICENSE(). Refer to section
111*4882a593Smuzhiyun5. for a way to create missing import statements automatically.
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun4. Loading Modules that use namespaced Symbols
114*4882a593Smuzhiyun==============================================
115*4882a593Smuzhiyun
116*4882a593SmuzhiyunAt module loading time (e.g. `insmod`), the kernel will check each symbol
117*4882a593Smuzhiyunreferenced from the module for its availability and whether the namespace it
118*4882a593Smuzhiyunmight be exported to has been imported by the module. The default behaviour of
119*4882a593Smuzhiyunthe kernel is to reject loading modules that don't specify sufficient imports.
120*4882a593SmuzhiyunAn error will be logged and loading will be failed with EINVAL. In order to
121*4882a593Smuzhiyunallow loading of modules that don't satisfy this precondition, a configuration
122*4882a593Smuzhiyunoption is available: Setting MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS=y will
123*4882a593Smuzhiyunenable loading regardless, but will emit a warning.
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun5. Automatically creating MODULE_IMPORT_NS statements
126*4882a593Smuzhiyun=====================================================
127*4882a593Smuzhiyun
128*4882a593SmuzhiyunMissing namespaces imports can easily be detected at build time. In fact,
129*4882a593Smuzhiyunmodpost will emit a warning if a module uses a symbol from a namespace
130*4882a593Smuzhiyunwithout importing it.
131*4882a593SmuzhiyunMODULE_IMPORT_NS() statements will usually be added at a definite location
132*4882a593Smuzhiyun(along with other module meta data). To make the life of module authors (and
133*4882a593Smuzhiyunsubsystem maintainers) easier, a script and make target is available to fixup
134*4882a593Smuzhiyunmissing imports. Fixing missing imports can be done with::
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun	$ make nsdeps
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunA typical scenario for module authors would be::
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun	- write code that depends on a symbol from a not imported namespace
141*4882a593Smuzhiyun	- `make`
142*4882a593Smuzhiyun	- notice the warning of modpost telling about a missing import
143*4882a593Smuzhiyun	- run `make nsdeps` to add the import to the correct code location
144*4882a593Smuzhiyun
145*4882a593SmuzhiyunFor subsystem maintainers introducing a namespace, the steps are very similar.
146*4882a593SmuzhiyunAgain, `make nsdeps` will eventually add the missing namespace imports for
147*4882a593Smuzhiyunin-tree modules::
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun	- move or add symbols to a namespace (e.g. with EXPORT_SYMBOL_NS())
150*4882a593Smuzhiyun	- `make` (preferably with an allmodconfig to cover all in-kernel
151*4882a593Smuzhiyun	  modules)
152*4882a593Smuzhiyun	- notice the warning of modpost telling about a missing import
153*4882a593Smuzhiyun	- run `make nsdeps` to add the import to the correct code location
154*4882a593Smuzhiyun
155*4882a593SmuzhiyunYou can also run nsdeps for external module builds. A typical usage is::
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun	$ make -C <path_to_kernel_src> M=$PWD nsdeps
158