Module init call vs symbols exporting race?

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Rusty Russell <rusty@...>, Linux Kernel Mailing List <linux-kernel@...>
Date: Tuesday, November 6, 2007 - 5:20 am

Hi.

I looked at the sys_init_module() and found that the ->init callback
for the module is called without the module_mutex held and *after*
the module's symbols are exported. Doesn't this create the race when
loading two modules in parallel? Like this.

Consider the first module to be (without any internal locking for
simplicity)

=== foo.c ===
static struct list_head foo_list;

void foo_register(struct list_head *elem)
{
	list_add(elem, &foo_list);
}
EXPORT_SYMBOL(foo_register);

static int foo_init(void)
{
	INIT_LIST_HEAD(&foo_list);
}
module_init(foo_init);
===

and the second one

=== bar.c ===
struct bar_struct {
	struct list_head list;
	...
} bar;

static int bar_init(void)
{
	foo_register(&bar.list);
}
module_init(bar_init);
===

If I understand the sys_init_module() right, the following code
flow is possible:

1CPU                                        2CPU
sys_init_module(/* foo module */)           sys_init_module(/* bar module */)
  mutex_lock(&module_mutex);
  load_module();
      /* export the foo_init here */
  mutex_unlock(&module_mutex);
                                            mutex_lock(&module_mutex);
                                            load_module();
                                                /* resolve the foo_init here */
                                            mutex_unlock(&mudule_mutex);
                                            mod->init(); /* bar_init */
           /*
            * OOPS! The foo_list is not ready yet, because the foo_init
            * is not called yet.
            */
  mod->init(); /* foo_init... too late */

Is this analysis correct?

Thanks,
Pavel
-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Module init call vs symbols exporting race?, Pavel Emelyanov, (Tue Nov 6, 5:20 am)
Re: Module init call vs symbols exporting race?, Rusty Russell, (Tue Nov 6, 8:41 am)
Re: Module init call vs symbols exporting race?, Jan Glauber, (Wed Nov 7, 6:01 am)
Re: Module init call vs symbols exporting race?, Andi Kleen, (Sat Nov 10, 4:23 pm)
Re: Module init call vs symbols exporting race?, Rusty Russell, (Wed Nov 7, 10:10 pm)
Re: Module init call vs symbols exporting race?, Jan Glauber, (Fri Nov 9, 12:06 pm)
Re: Module init call vs symbols exporting race?, Jon Masters, (Fri Nov 9, 7:44 am)
Re: Module init call vs symbols exporting race?, Jan Glauber, (Fri Nov 9, 8:16 am)
Re: Module init call vs symbols exporting race?, Rusty Russell, (Sat Nov 10, 3:27 am)
Re: Module init call vs symbols exporting race?, Jan Glauber, (Mon Nov 12, 10:03 am)