如何定义内核模块之间的依赖关系?

gur*_* th 5 linux-device-driver linux-kernel

如何定义内核模块的依赖关系,

例:

got module1 and module2.
Run Code Online (Sandbox Code Playgroud)

我怎么说说内核module2应该在之后加载module1还是module2依赖于module1

注意:module2没有使用来自module1的任何符号,但是在我的用例中,顺序仍然很重要。因此不要与内核中的moddep有关。

小智 6

从linux 4.4内核开始(可能是更早的版本),可以使用软依赖性来指定在请求加载模块之前或之后加载内核模块。可以在modprobe.d(5)联机帮助页中描述的配置文件中设置这些软依赖关系,或者可以直接使用MODULE_SOFTDEP宏在内核模块的代码中直接指定这些依赖项。

为了完成装载module2module1通过修改的代码module2,添加此行的函数的外部module2代码:

MODULE_SOFTDEP("pre: module1")

要通过修改module1代码来实现相同目的,可以使用以下行:

MODULE_SOFTDEP("post: module2")


小智 5

引用来自depmod的手册页:

   Linux kernel modules can provide services (called "symbols") for other
   modules to use (using one of the EXPORT_SYMBOL variants in the code).
   If a second module uses this symbol, that second module clearly depends
   on the first module. These dependencies can get quite complex.

   depmod creates a list of module dependencies by reading each module
   under /lib/modules/version and determining what symbols it exports and
   what symbols it needs. By default, this list is written to modules.dep,
   and a binary hashed version named modules.dep.bin, in the same
   directory. If filenames are given on the command line, only those
   modules are examined (which is rarely useful unless all modules are
   listed).  depmod also creates a list of symbols provided by modules in
   the file named modules.symbols and its binary hashed version,
   modules.symbols.bin. Finally, depmod will output a file named
   modules.devname if modules supply special device names (devname) that
   should be populated in /dev on boot (by a utility such as udev).
Run Code Online (Sandbox Code Playgroud)


jjm*_*jjm 3

为了简单的解决方案,您可以在第一个模块中添加符号并在第二个模块 init 中检查该符号。如果符号未使用导出

EXPORT_SYMBOL 
Run Code Online (Sandbox Code Playgroud)

您可以从第二个模块初始化本身返回。

详细信息请参见标题

      Linux kernel modules can provide services (called "symbols") for other
   modules to use (using one of the EXPORT_SYMBOL variants in the code).
   If a second module uses this symbol, that second module clearly depends
   on the first module. These dependencies can get quite complex.

   depmod creates a list of module dependencies by reading each module
   under /lib/modules/version and determining what symbols it exports and
   what symbols it needs. By default, this list is written to modules.dep,
   and a binary hashed version named modules.dep.bin, in the same
   directory. If filenames are given on the command line, only those
   modules are examined (which is rarely useful unless all modules are
   listed).  depmod also creates a list of symbols provided by modules in
   the file named modules.symbols and its binary hashed version,
   modules.symbols.bin. Finally, depmod will output a file named
   modules.devname if modules supply special device names (devname) that
   should be populated in /dev on boot (by a utility such as udev).
Run Code Online (Sandbox Code Playgroud)