12

If I have two modules which are being statically linked in. One modules' module_init function depends on another module's module_init function having already run. Is there a way to force one module to load before the other?

Also, is the first module's init function guaranteed to finish before the second one is invoked?

Lastly, if the answer to the above is NO, what is the recommended way of synchronizing the two module init calls to make sure I don't run into issues?

caf
  • 233,326
  • 40
  • 323
  • 462
John Ulvr
  • 636
  • 1
  • 8
  • 8
  • 1) Which language/toolchain, 2) Do you mean linked to a .so or a .a? – Erik Apr 14 '11 at 21:07
  • 1
    Do you mean two Linux kernel modules? How are they statically linked in? You can't statically link two objects together both with a "module_init" function. – Owen Fraser-Green Apr 14 '11 at 21:54
  • 1
    I believe owen has it, seeing as the `module_init` symbol is an integral part of the Linux kernel module system. @John you should edit your question and include the `linux-kernel` tag - the `linux` tag is (unfortunately) dominated by userspace issues, often not even Linux specific. – Thomas M. DuBuisson Apr 14 '11 at 22:34

2 Answers2

13

Is there a way to force one module to load before the other?

Answer is surprisingly simple, make sure first module is first in Makefile:

obj-y += mod1.o
obj-y += mod2.o

is the first module's init function guaranteed to finish before the second one is invoked?

Yes, initcalls (module_init hook) in your case are called one-by-one. See init/main.c, do_one_initcall() callers.

adobriyan
  • 2,594
  • 16
  • 8
  • 1
    This solution won't work for modules defined in different Makefiles. Any solution for the case that the two modules are compiled in different Makefiles? – talel Jan 23 '13 at 14:22
  • @talel Generally there is a top Makefile to define the sequence to descend into directories and an arch Makefile to define arch-specific sections. You can look for those init-y, core-y, drivers-y etc. in those Makefiles. Also this article may be helpful to you. http://www.kernel.org/doc/Documentation/kbuild/makefiles.txt – ywu Apr 05 '16 at 21:23
0

I'm assuming you want to fix the static initialization fiasco

Have a look at

Static Initialization Order Fiasco

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • He's talking about Linux kernel modules; your solutions all have to do with C++ userspace. – Eric Seppanen Apr 15 '11 at 14:47
  • @Eric: oops, thanks for noticing - I missed that completely... I assumed the strange names (module_init) where on some kind of exotic UNIX flavour :) – sehe Apr 15 '11 at 18:07