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

How do I define dependency among kernel modules?

如何在内核中为模块定义依赖关系,

示例:

got module1 and module2.

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

注意:模块 2 没有使用模块 1 中的任何符号,但在我的用例中顺序仍然很重要。所以不要与内核中的moddep相关。

引自 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).

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

导出符号
EXPORT_SYMBOL 

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

详情来自header

      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).

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

要通过修改 module2 的代码在 module1 之后完成加载 module2,请在函数外部将此行添加到 module2 代码中:

MODULE_SOFTDEP("pre: module1")

要通过修改 module1 代码来完成相同的操作,您可以使用此行:

MODULE_SOFTDEP("post: module2")