【问题标题】:How do I define dependency among kernel modules?如何定义内核模块之间的依赖关系?
【发布时间】:2019-09-10 14:46:31
【问题描述】:

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

例子:

got module1 and module2.

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

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

【问题讨论】:

  • 我们不能手动做吗?
  • @srinivasth 你能提供更多关于你试图解决的问题的信息吗?
  • 你说的是外部模块(又名文件)还是内置模块?

标签: linux-kernel linux-device-driver


【解决方案1】:

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

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

MODULE_SOFTDEP("pre: module1")

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

MODULE_SOFTDEP("post: module2")

【讨论】:

  • 应该注意的是,如果 module1 是内置的,那么使用“MODULE_SOFTDEP”宏所有的赌注都会被取消,你可能最好使用延迟探测,或者伪造/创建module1 对 module2 中某个符号的某种依赖。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    导出的
    EXPORT_SYMBOL 
    

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

    详情请看标题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 2012-03-09
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 2015-12-28
      相关资源
      最近更新 更多