162306a36Sopenharmony_ci================= 262306a36Sopenharmony_ciSymbol Namespaces 362306a36Sopenharmony_ci================= 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciThe following document describes how to use Symbol Namespaces to structure the 662306a36Sopenharmony_ciexport surface of in-kernel symbols exported through the family of 762306a36Sopenharmony_ciEXPORT_SYMBOL() macros. 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci.. Table of Contents 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci === 1 Introduction 1262306a36Sopenharmony_ci === 2 How to define Symbol Namespaces 1362306a36Sopenharmony_ci --- 2.1 Using the EXPORT_SYMBOL macros 1462306a36Sopenharmony_ci --- 2.2 Using the DEFAULT_SYMBOL_NAMESPACE define 1562306a36Sopenharmony_ci === 3 How to use Symbols exported in Namespaces 1662306a36Sopenharmony_ci === 4 Loading Modules that use namespaced Symbols 1762306a36Sopenharmony_ci === 5 Automatically creating MODULE_IMPORT_NS statements 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci1. Introduction 2062306a36Sopenharmony_ci=============== 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ciSymbol Namespaces have been introduced as a means to structure the export 2362306a36Sopenharmony_cisurface of the in-kernel API. It allows subsystem maintainers to partition 2462306a36Sopenharmony_citheir exported symbols into separate namespaces. That is useful for 2562306a36Sopenharmony_cidocumentation purposes (think of the SUBSYSTEM_DEBUG namespace) as well as for 2662306a36Sopenharmony_cilimiting the availability of a set of symbols for use in other parts of the 2762306a36Sopenharmony_cikernel. As of today, modules that make use of symbols exported into namespaces, 2862306a36Sopenharmony_ciare required to import the namespace. Otherwise the kernel will, depending on 2962306a36Sopenharmony_ciits configuration, reject loading the module or warn about a missing import. 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci2. How to define Symbol Namespaces 3262306a36Sopenharmony_ci================================== 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciSymbols can be exported into namespace using different methods. All of them are 3562306a36Sopenharmony_cichanging the way EXPORT_SYMBOL and friends are instrumented to create ksymtab 3662306a36Sopenharmony_cientries. 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci2.1 Using the EXPORT_SYMBOL macros 3962306a36Sopenharmony_ci================================== 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ciIn addition to the macros EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(), that allow 4262306a36Sopenharmony_ciexporting of kernel symbols to the kernel symbol table, variants of these are 4362306a36Sopenharmony_ciavailable to export symbols into a certain namespace: EXPORT_SYMBOL_NS() and 4462306a36Sopenharmony_ciEXPORT_SYMBOL_NS_GPL(). They take one additional argument: the namespace. 4562306a36Sopenharmony_ciPlease note that due to macro expansion that argument needs to be a 4662306a36Sopenharmony_cipreprocessor symbol. E.g. to export the symbol ``usb_stor_suspend`` into the 4762306a36Sopenharmony_cinamespace ``USB_STORAGE``, use:: 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci EXPORT_SYMBOL_NS(usb_stor_suspend, USB_STORAGE); 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ciThe corresponding ksymtab entry struct ``kernel_symbol`` will have the member 5262306a36Sopenharmony_ci``namespace`` set accordingly. A symbol that is exported without a namespace will 5362306a36Sopenharmony_cirefer to ``NULL``. There is no default namespace if none is defined. ``modpost`` 5462306a36Sopenharmony_ciand kernel/module/main.c make use the namespace at build time or module load 5562306a36Sopenharmony_citime, respectively. 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci2.2 Using the DEFAULT_SYMBOL_NAMESPACE define 5862306a36Sopenharmony_ci============================================= 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ciDefining namespaces for all symbols of a subsystem can be very verbose and may 6162306a36Sopenharmony_cibecome hard to maintain. Therefore a default define (DEFAULT_SYMBOL_NAMESPACE) 6262306a36Sopenharmony_ciis been provided, that, if set, will become the default for all EXPORT_SYMBOL() 6362306a36Sopenharmony_ciand EXPORT_SYMBOL_GPL() macro expansions that do not specify a namespace. 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ciThere are multiple ways of specifying this define and it depends on the 6662306a36Sopenharmony_cisubsystem and the maintainer's preference, which one to use. The first option 6762306a36Sopenharmony_ciis to define the default namespace in the ``Makefile`` of the subsystem. E.g. to 6862306a36Sopenharmony_ciexport all symbols defined in usb-common into the namespace USB_COMMON, add a 6962306a36Sopenharmony_ciline like this to drivers/usb/common/Makefile:: 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=USB_COMMON 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ciThat will affect all EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL() statements. A 7462306a36Sopenharmony_cisymbol exported with EXPORT_SYMBOL_NS() while this definition is present, will 7562306a36Sopenharmony_cistill be exported into the namespace that is passed as the namespace argument 7662306a36Sopenharmony_cias this argument has preference over a default symbol namespace. 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ciA second option to define the default namespace is directly in the compilation 7962306a36Sopenharmony_ciunit as preprocessor statement. The above example would then read:: 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci #undef DEFAULT_SYMBOL_NAMESPACE 8262306a36Sopenharmony_ci #define DEFAULT_SYMBOL_NAMESPACE USB_COMMON 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ciwithin the corresponding compilation unit before any EXPORT_SYMBOL macro is 8562306a36Sopenharmony_ciused. 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci3. How to use Symbols exported in Namespaces 8862306a36Sopenharmony_ci============================================ 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ciIn order to use symbols that are exported into namespaces, kernel modules need 9162306a36Sopenharmony_cito explicitly import these namespaces. Otherwise the kernel might reject to 9262306a36Sopenharmony_ciload the module. The module code is required to use the macro MODULE_IMPORT_NS 9362306a36Sopenharmony_cifor the namespaces it uses symbols from. E.g. a module using the 9462306a36Sopenharmony_ciusb_stor_suspend symbol from above, needs to import the namespace USB_STORAGE 9562306a36Sopenharmony_ciusing a statement like:: 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci MODULE_IMPORT_NS(USB_STORAGE); 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ciThis will create a ``modinfo`` tag in the module for each imported namespace. 10062306a36Sopenharmony_ciThis has the side effect, that the imported namespaces of a module can be 10162306a36Sopenharmony_ciinspected with modinfo:: 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci $ modinfo drivers/usb/storage/ums-karma.ko 10462306a36Sopenharmony_ci [...] 10562306a36Sopenharmony_ci import_ns: USB_STORAGE 10662306a36Sopenharmony_ci [...] 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ciIt is advisable to add the MODULE_IMPORT_NS() statement close to other module 11062306a36Sopenharmony_cimetadata definitions like MODULE_AUTHOR() or MODULE_LICENSE(). Refer to section 11162306a36Sopenharmony_ci5. for a way to create missing import statements automatically. 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci4. Loading Modules that use namespaced Symbols 11462306a36Sopenharmony_ci============================================== 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ciAt module loading time (e.g. ``insmod``), the kernel will check each symbol 11762306a36Sopenharmony_cireferenced from the module for its availability and whether the namespace it 11862306a36Sopenharmony_cimight be exported to has been imported by the module. The default behaviour of 11962306a36Sopenharmony_cithe kernel is to reject loading modules that don't specify sufficient imports. 12062306a36Sopenharmony_ciAn error will be logged and loading will be failed with EINVAL. In order to 12162306a36Sopenharmony_ciallow loading of modules that don't satisfy this precondition, a configuration 12262306a36Sopenharmony_cioption is available: Setting MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS=y will 12362306a36Sopenharmony_cienable loading regardless, but will emit a warning. 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci5. Automatically creating MODULE_IMPORT_NS statements 12662306a36Sopenharmony_ci===================================================== 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ciMissing namespaces imports can easily be detected at build time. In fact, 12962306a36Sopenharmony_cimodpost will emit a warning if a module uses a symbol from a namespace 13062306a36Sopenharmony_ciwithout importing it. 13162306a36Sopenharmony_ciMODULE_IMPORT_NS() statements will usually be added at a definite location 13262306a36Sopenharmony_ci(along with other module meta data). To make the life of module authors (and 13362306a36Sopenharmony_cisubsystem maintainers) easier, a script and make target is available to fixup 13462306a36Sopenharmony_cimissing imports. Fixing missing imports can be done with:: 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci $ make nsdeps 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciA typical scenario for module authors would be:: 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci - write code that depends on a symbol from a not imported namespace 14162306a36Sopenharmony_ci - ``make`` 14262306a36Sopenharmony_ci - notice the warning of modpost telling about a missing import 14362306a36Sopenharmony_ci - run ``make nsdeps`` to add the import to the correct code location 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciFor subsystem maintainers introducing a namespace, the steps are very similar. 14662306a36Sopenharmony_ciAgain, ``make nsdeps`` will eventually add the missing namespace imports for 14762306a36Sopenharmony_ciin-tree modules:: 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci - move or add symbols to a namespace (e.g. with EXPORT_SYMBOL_NS()) 15062306a36Sopenharmony_ci - ``make`` (preferably with an allmodconfig to cover all in-kernel 15162306a36Sopenharmony_ci modules) 15262306a36Sopenharmony_ci - notice the warning of modpost telling about a missing import 15362306a36Sopenharmony_ci - run ``make nsdeps`` to add the import to the correct code location 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ciYou can also run nsdeps for external module builds. A typical usage is:: 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci $ make -C <path_to_kernel_src> M=$PWD nsdeps 158