7

How one can define a dependency for modules in kernel,

Example:

got module1 and module2.

How do I say say kernel module2 should be loaded after module1 or module2 is dependent of module1?

Note : module2 is not using any symbol from module1, but still order is important in my use case. so don't relate with moddep in kernel.

guru th
  • 149
  • 1
  • 1
  • 14

3 Answers3

13

As of the linux 4.4 kernel (and maybe earlier) soft dependencies can be used to specify that a kernel module be loaded before or after a module is requested to be loaded. Those soft dependencies can be setup in a configuration file as described in the modprobe.d (5) manpage, or they can be specified directly in the code for the kernel module directly using the MODULE_SOFTDEP macro.

To accomplish loading module2 after module1 by modifying the code of module2, add this line outside of a function to the module2 code:

MODULE_SOFTDEP("pre: module1")

To accomplish the same by modifying the module1 code, you would use this line:

MODULE_SOFTDEP("post: module2")

Robert McLean
  • 196
  • 1
  • 8
  • 1
    It should be noted, that if module1 is a built-in, then all bets are off using the "MODULE_SOFTDEP" macro, and you are probably better off using a deferred probe, or maybe faking/creating some kind of dependency of module1 on a symbol in module2. – Robert McLean Feb 24 '22 at 20:22
5

Quote from man page of 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).
Chris Tsui
  • 452
  • 3
  • 10
3

For easy solution, you can add symbol in first module and check that symbol in second module init. If symbol is not exported using

EXPORT_SYMBOL 

you can return from second module initialization itself.

For details, from 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).
jjm
  • 431
  • 3
  • 19