How do I define dependency among kernel modules?

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


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

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