0001 =================
0002 Symbol Namespaces
0003 =================
0004
0005 The following document describes how to use Symbol Namespaces to structure the
0006 export surface of in-kernel symbols exported through the family of
0007 EXPORT_SYMBOL() macros.
0008
0009 .. Table of Contents
0010
0011 === 1 Introduction
0012 === 2 How to define Symbol Namespaces
0013 --- 2.1 Using the EXPORT_SYMBOL macros
0014 --- 2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
0015 === 3 How to use Symbols exported in Namespaces
0016 === 4 Loading Modules that use namespaced Symbols
0017 === 5 Automatically creating MODULE_IMPORT_NS statements
0018
0019 1. Introduction
0020 ===============
0021
0022 Symbol Namespaces have been introduced as a means to structure the export
0023 surface of the in-kernel API. It allows subsystem maintainers to partition
0024 their exported symbols into separate namespaces. That is useful for
0025 documentation purposes (think of the SUBSYSTEM_DEBUG namespace) as well as for
0026 limiting the availability of a set of symbols for use in other parts of the
0027 kernel. As of today, modules that make use of symbols exported into namespaces,
0028 are required to import the namespace. Otherwise the kernel will, depending on
0029 its configuration, reject loading the module or warn about a missing import.
0030
0031 2. How to define Symbol Namespaces
0032 ==================================
0033
0034 Symbols can be exported into namespace using different methods. All of them are
0035 changing the way EXPORT_SYMBOL and friends are instrumented to create ksymtab
0036 entries.
0037
0038 2.1 Using the EXPORT_SYMBOL macros
0039 ==================================
0040
0041 In addition to the macros EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(), that allow
0042 exporting of kernel symbols to the kernel symbol table, variants of these are
0043 available to export symbols into a certain namespace: EXPORT_SYMBOL_NS() and
0044 EXPORT_SYMBOL_NS_GPL(). They take one additional argument: the namespace.
0045 Please note that due to macro expansion that argument needs to be a
0046 preprocessor symbol. E.g. to export the symbol ``usb_stor_suspend`` into the
0047 namespace ``USB_STORAGE``, use::
0048
0049 EXPORT_SYMBOL_NS(usb_stor_suspend, USB_STORAGE);
0050
0051 The corresponding ksymtab entry struct ``kernel_symbol`` will have the member
0052 ``namespace`` set accordingly. A symbol that is exported without a namespace will
0053 refer to ``NULL``. There is no default namespace if none is defined. ``modpost``
0054 and kernel/module/main.c make use the namespace at build time or module load
0055 time, respectively.
0056
0057 2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
0058 =============================================
0059
0060 Defining namespaces for all symbols of a subsystem can be very verbose and may
0061 become hard to maintain. Therefore a default define (DEFAULT_SYMBOL_NAMESPACE)
0062 is been provided, that, if set, will become the default for all EXPORT_SYMBOL()
0063 and EXPORT_SYMBOL_GPL() macro expansions that do not specify a namespace.
0064
0065 There are multiple ways of specifying this define and it depends on the
0066 subsystem and the maintainer's preference, which one to use. The first option
0067 is to define the default namespace in the ``Makefile`` of the subsystem. E.g. to
0068 export all symbols defined in usb-common into the namespace USB_COMMON, add a
0069 line like this to drivers/usb/common/Makefile::
0070
0071 ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON
0072
0073 That will affect all EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL() statements. A
0074 symbol exported with EXPORT_SYMBOL_NS() while this definition is present, will
0075 still be exported into the namespace that is passed as the namespace argument
0076 as this argument has preference over a default symbol namespace.
0077
0078 A second option to define the default namespace is directly in the compilation
0079 unit as preprocessor statement. The above example would then read::
0080
0081 #undef DEFAULT_SYMBOL_NAMESPACE
0082 #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON
0083
0084 within the corresponding compilation unit before any EXPORT_SYMBOL macro is
0085 used.
0086
0087 3. How to use Symbols exported in Namespaces
0088 ============================================
0089
0090 In order to use symbols that are exported into namespaces, kernel modules need
0091 to explicitly import these namespaces. Otherwise the kernel might reject to
0092 load the module. The module code is required to use the macro MODULE_IMPORT_NS
0093 for the namespaces it uses symbols from. E.g. a module using the
0094 usb_stor_suspend symbol from above, needs to import the namespace USB_STORAGE
0095 using a statement like::
0096
0097 MODULE_IMPORT_NS(USB_STORAGE);
0098
0099 This will create a ``modinfo`` tag in the module for each imported namespace.
0100 This has the side effect, that the imported namespaces of a module can be
0101 inspected with modinfo::
0102
0103 $ modinfo drivers/usb/storage/ums-karma.ko
0104 [...]
0105 import_ns: USB_STORAGE
0106 [...]
0107
0108
0109 It is advisable to add the MODULE_IMPORT_NS() statement close to other module
0110 metadata definitions like MODULE_AUTHOR() or MODULE_LICENSE(). Refer to section
0111 5. for a way to create missing import statements automatically.
0112
0113 4. Loading Modules that use namespaced Symbols
0114 ==============================================
0115
0116 At module loading time (e.g. ``insmod``), the kernel will check each symbol
0117 referenced from the module for its availability and whether the namespace it
0118 might be exported to has been imported by the module. The default behaviour of
0119 the kernel is to reject loading modules that don't specify sufficient imports.
0120 An error will be logged and loading will be failed with EINVAL. In order to
0121 allow loading of modules that don't satisfy this precondition, a configuration
0122 option is available: Setting MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS=y will
0123 enable loading regardless, but will emit a warning.
0124
0125 5. Automatically creating MODULE_IMPORT_NS statements
0126 =====================================================
0127
0128 Missing namespaces imports can easily be detected at build time. In fact,
0129 modpost will emit a warning if a module uses a symbol from a namespace
0130 without importing it.
0131 MODULE_IMPORT_NS() statements will usually be added at a definite location
0132 (along with other module meta data). To make the life of module authors (and
0133 subsystem maintainers) easier, a script and make target is available to fixup
0134 missing imports. Fixing missing imports can be done with::
0135
0136 $ make nsdeps
0137
0138 A typical scenario for module authors would be::
0139
0140 - write code that depends on a symbol from a not imported namespace
0141 - ``make``
0142 - notice the warning of modpost telling about a missing import
0143 - run ``make nsdeps`` to add the import to the correct code location
0144
0145 For subsystem maintainers introducing a namespace, the steps are very similar.
0146 Again, ``make nsdeps`` will eventually add the missing namespace imports for
0147 in-tree modules::
0148
0149 - move or add symbols to a namespace (e.g. with EXPORT_SYMBOL_NS())
0150 - ``make`` (preferably with an allmodconfig to cover all in-kernel
0151 modules)
0152 - notice the warning of modpost telling about a missing import
0153 - run ``make nsdeps`` to add the import to the correct code location
0154
0155 You can also run nsdeps for external module builds. A typical usage is::
0156
0157 $ make -C <path_to_kernel_src> M=$PWD nsdeps