Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the memory allocation of "__this_module" variable?

From <linux/module.h>:

#ifdef MODULE
#define MODULE_GENERIC_TABLE(gtype,name)            \
extern const struct gtype##_id __mod_##gtype##_table        \
  __attribute__ ((unused, alias(__stringify(name))))

extern struct module __this_module;
#define THIS_MODULE (&__this_module)
#else  /* !MODULE */
#define MODULE_GENERIC_TABLE(gtype,name)
#define THIS_MODULE ((struct module *)0)
#endif

I can see "extern struct module __this_module;" is just a declaration of __this_module, but not definition of __this_module. So where is the memory allocation of __this_module? I can't find it in kernel code.

like image 303
Nan Xiao Avatar asked Nov 06 '15 02:11

Nan Xiao


1 Answers

According to an obscure and dark spot in the LKML...

Does this mean that the module structure (struct module) and it's various ubstructures are filled in by insmod?

Regards, Naren

On Sun, 5 Nov 2000, Tigran Aivazian wrote:

On Sun, 5 Nov 2000, Naren Devaiah wrote: > >

I've looked in the 2.4.0-pre10 source tree and found it defined as extern struct module __this_module; in module.h (among other files), but where is it actually defined?

it isn't -- it's magic, of course :). The way it works is for insmod to arrange things in such a manner that &__this_module resolves to point to the beginning of module's address space, which happens to contain 'struct module' at the beginning.

Regards, Tigran

Follow-up...

On Sun, 5 Nov 2000, Naren Devaiah wrote:

Does this mean that the module structure (struct module) and it's various substructures are filled in by insmod?

Regards, Naren

Yes, partially, i.e. have a look at sys_create_module() and sys_init_module() system calls, they are in kernel/module.c

sys_create_module() just allocates the space and links the module into the list but sys_init_module() is passed a 'struct module' from userspace whose content is harshly validated (trust no one!) and then installed into a real kernel 'struct module' and module's init_module() routine is invoked.

Regards, Tigran

like image 60
3442 Avatar answered Oct 01 '22 17:10

3442