title: Synchronization at kernel level during insmod
body: <p>I am a kernel newbie, I have been working out with the semaphores lately, which help in synchronizing among the kernel threads, I know its not a good way of synchronization but please bear with me If I am missing something here.
  I have two modules, In which I try to access a common function by exporting the symbol to the kernel symbol table from the first module and this will be accessed by the second module during the <code>init_module()</code> calls.</p>
  
  <p>The first module as give below has the exported symbol "caller",</p>
  
  <pre><code>#define DEBUG                                        
  #include&lt;linux/init.h&gt;                               
  #include&lt;linux/module.h&gt;                             
  
  struct semaphore sem =__SEMAPHORE_INITIALIZER(sem,1);               
  EXPORT_SYMBOL(sem);
  
  
  void static caller(int a, int b,int count){
          complete(&amp;sig);
          while(1){
  
                  int temp=20000,i;
                  printk(KERN_INFO"called from module %s\n",THIS_MODULE-&gt;name);
  //              down_interruptible(&amp;sem);                                    
                  while(--count&gt;0){
                          for(i=0;i&lt;temp;i++);
                          printk(KERN_INFO"This is a Exported Symbol into the table %d %d",a+b,count);   
                  }
  //              up(&amp;sem);                                                              
                  break;
          }
  }
  static int __init hello_kernel(void){
          int count=1000000;                                                                      
          init_completion(&amp;sig);
          printk(KERN_ALERT"Entered In the First Module");
  //      printk(KERN_INFO" Parameter passed are %d %s",arg,chrarg);
          caller(1,1,count);                                                                      
          return 0;                                                                               
  
  }
  
  static void __exit  hello_exit(void){
          dump_stack();
          printk(KERN_EMERG"GOHEAVEN");
  
  }
  
  module_init(hello_kernel);
  module_exit(hello_exit);
  MODULE_LICENSE("GPL");                   
  EXPORT_SYMBOL(caller);
  module_init(hello_kernel);
  module_exit(hello_exit);
  </code></pre>
  
  <p>The second module calls the exported symbol "caller"</p>
  
  <pre><code>#define DEBUG                                        
  #include&lt;linux/init.h&gt;                               
  #include&lt;linux/module.h&gt;                             
  void caller(int , int, int);                         
  
  
  static int __init hello_kernel(void){
          int count=50;                                                           
          printk(KERN_INFO"Entered In Second Module");            
          caller(2,2,count);
          return 0;
  
  }
  
  
  static void __exit  hello_exit(void){
          dump_stack();
          printk(KERN_EMERG"GOHEAVEN");
  
  }
  
  module_init(hello_kernel);
  module_exit(hello_exit);
  MODULE_LICENSE("GPL");                   
  EXPORT_SYMBOL(caller);
  module_init(hello_kernel);
  module_exit(hello_exit);
  </code></pre>
  
  <p>First module log comes first and then the Second modules init is called.</p>
  
  <pre><code>.........
  .........
  [36056.684970] This is a Exported Symbol into the table 2 36
  [36056.685136] This is a Exported Symbol into the table 2 35
  [36056.685301] This is a Exported Symbol into the table 2 34
  [36056.685467] This is a Exported Symbol into the table 2 33
  [36056.685632] This is a Exported Symbol into the table 2 32
  [36056.685798] This is a Exported Symbol into the table 2 31
  [36056.685963] This is a Exported Symbol into the table 2 30
  [36056.686129] This is a Exported Symbol into the table 2 29
  [36056.686314] This is a Exported Symbol into the table 2 28
  [36056.686488] This is a Exported Symbol into the table 2 27
  [36056.686664] This is a Exported Symbol into the table 2 26
  [36056.686851] This is a Exported Symbol into the table 2 25
  [36056.687032] This is a Exported Symbol into the table 2 24
  [36056.687228] This is a Exported Symbol into the table 2 23
  [36056.687393] This is a Exported Symbol into the table 2 22
  [36056.687559] This is a Exported Symbol into the table 2 21
  [36056.687724] This is a Exported Symbol into the table 2 20
  [36056.687890] This is a Exported Symbol into the table 2 19
  [36056.688055] This is a Exported Symbol into the table 2 18
  [36056.688221] This is a Exported Symbol into the table 2 17
  [36056.688387] This is a Exported Symbol into the table 2 16
  [36056.688552] This is a Exported Symbol into the table 2 15
  [36056.688718] This is a Exported Symbol into the table 2 14
  [36056.688883] This is a Exported Symbol into the table 2 13
  [36056.689049] This is a Exported Symbol into the table 2 12
  [36056.689215] This is a Exported Symbol into the table 2 11
  [36056.689380] This is a Exported Symbol into the table 2 10
  [36056.689546] This is a Exported Symbol into the table 2 9
  [36056.689711] This is a Exported Symbol into the table 2 8
  [36056.689877] This is a Exported Symbol into the table 2 7
  [36056.690044] This is a Exported Symbol into the table 2 6
  [36056.690238] This is a Exported Symbol into the table 2 5
  [36056.690432] This is a Exported Symbol into the table 2 4
  [36056.690604] This is a Exported Symbol into the table 2 3
  [36056.690786] This is a Exported Symbol into the table 2 2
  [36056.690964] This is a Exported Symbol into the table 2 1
  
  ...........
  ...........
  [36058.216861] This is a Exported Symbol into the table 4 19
  [36058.216931] This is a Exported Symbol into the table 4 18
  [36058.217001] This is a Exported Symbol into the table 4 17
  [36058.217070] This is a Exported Symbol into the table 4 16
  [36058.217140] This is a Exported Symbol into the table 4 15
  [36058.217210] This is a Exported Symbol into the table 4 14
  [36058.217279] This is a Exported Symbol into the table 4 13
  [36058.217349] This is a Exported Symbol into the table 4 12
  [36058.217419] This is a Exported Symbol into the table 4 11
  [36058.217488] This is a Exported Symbol into the table 4 10
  [36058.217558] This is a Exported Symbol into the table 4 9
  [36058.217628] This is a Exported Symbol into the table 4 8
  [36058.217698] This is a Exported Symbol into the table 4 7
  [36058.217767] This is a Exported Symbol into the table 4 6
  [36058.217837] This is a Exported Symbol into the table 4 5
  [36058.217907] This is a Exported Symbol into the table 4 4
  [36058.217976] This is a Exported Symbol into the table 4 3
  [36058.218046] This is a Exported Symbol into the table 4 2
  </code></pre>
  
  <p>Without the use of the semaphore I was expecting that the dmesg kernel logs will be mixed from both the modules as they access the code inconsistently, but looks like the insmod process 
  loads the modules only one after the another.</p>
  
  <p>What am I missing here??</p>
  
  <p>Thanks.</p>