THIS_MODULE macro definitions in Linux

Source: Internet
Author: User

have been brooding over what this this_module is all about, the ubiquitous stuff in the Linux kernel. Today on the Internet search, basically understand. Online cattle have been written more detailed, but also currently no time for a deeper analysis, so directly affixed to come ...

Reprint website:

Http://blog.csdn.net/a954423389/archive/2010/12/27/6101369.aspx

SOURCE Location:

@ kernel/module.c

@ include/linux/module.h

The struct struct module represents a kernel module in the kernel, and the module is associated with a struct module struct and becomes part of the kernel through insmod (actually executing the init_module system call) inserting its own kernel module into the kernel. The following is a complete definition of the struct struct module, which is then explained individually:

struct MODULE

{

Enum Module_state State;

struct List_head list;

Char Name[module_name_len];

struct Module_kobject mkobj;

struct Module_param_attrs *param_attrs;

const char *version;

const char *srcversion;

const struct Kernel_symbol *syms;

unsigned int num_syms;

const unsigned long *crcs;

const struct Kernel_symbol *gpl_syms;

unsigned int num_gpl_syms;

const unsigned long *gpl_crcs;

unsigned int num_exentries;

const struct Exception_table_entry *extable;

Int (*init) (void);

void *module_init;

void *module_core;

unsigned long init_size, core_size;

unsigned long init_text_size, core_text_size;

struct mod_arch_specific arch;

int unsafe;

int License_gplok;

#ifdef Config_module_unload

struct Module_ref Ref[nr_cpus];

struct List_head modules_which_use_me;

struct Task_struct *waiter;

void (*exit) (void);

#endif

#ifdef config_kallsyms

Elf_sym *symtab;

unsigned long num_symtab;

Char *strtab;

struct Module_sect_attrs *sect_attrs;

#endif

void *percpu;

Char *args;

};

We insert a kernel module, usually using the tool insmod, the tool actually calls the system call Init_module, in the system call function, the first call Load_module, the user space passed into the entire kernel module file to create a kernel module, Returns a struct module structure body. This structure is represented in the kernel as the kernel module.

State is the current status of the module. It is an enumerated variable and the desirable value is: module_state_live,module_state_coming,module_state_going. Indicates that the module is currently in normal use (alive), the module is currently being loaded, and the module is currently being uninstalled. After completing the partial creation of the module in the Load_module function, the state is set to complete the initialization of the module in the Module_state_coming,sys_init_module function (including adding the module to the Global module list, Call the initialization function of the module itself, set the module state to Module_state_live, and finally, when the module is unloaded using the Rmmod tool, the system call Delete_module is called and the module's status is set to Module_state_going. This is a state of internal maintenance for the module.

List is a member of a list, all kernel modules are maintained in a global linked list, and the linked list header is a global variable of the struct module *modules. Any newly created module will be added to the head of the list, which can be referenced by Modules->next.

Name is the name of the module, which usually takes the file name of the module as the module name. It is an identification of this module.

Also, to introduce the macro this_module, it is defined as follows # define THIS_MODULE (&__this_module), __this_module is a struct module variable, representing the current module, A bit like current. You can use the This_module macro to refer to the struct module structure of the modules, and try the following modules:

#include <linux/module.h>

Module_license ("Dual BSD/GPL");

static int hello_init (void)

{

unsigned int cpu = GET_CPU ();

struct module *mod;

PRINTK (Kern_alert "This module:%p==%p/n", &__this_module, This_module);

PRINTK (kern_alert "module state:%d/n", this_module->state);

PRINTK (kern_alert "module name:%s/n", this_module->name);

List_for_each_entry (mod, * (&this_module->list.prev), list)

PRINTK (kern_alert "module name:%s/n", mod->name);

return 0;

}

static void Hello_exit (void)

{

PRINTK (kern_alert "module state:%d/n", this_module->state);

}

Module_init (Hello_init);

Module_exit (Hello_exit);

Owner is a struct module * type of struct pointer, now tell you that each struct module structure in the kernel represents a kernel module, just like 17 Dali each representative represents a group of people, as to what the person, choose their people just know, likewise, What module is represented by each struct module struct, and the module to which it is initialized is known. Of course, the initialization of this structure is not what the driver should do, but what is done in the tortuous process of the insmod or modprobe to the Xxx_init function that you have driven just now. After the Insmod command executes, a system call in kernel/module.c is called Init_module, which invokes the Load_module function, creates a kernel module of the entire kernel module file passed into the user space, and returns a struct The module structure, from which the kernel is represented by this structure.

and see what This_module macro means, it's defined in include/linux/module.h

This_module #define (&__this_module)

is a struct module variable that represents the current module, which is somewhat similar to that of the famous present, and can be referenced by the This_module macro to reference the module's struct-module structure, such as using this_module-> State can get the status of the current module. Now you should understand why in that time you need not hesitate to set the struct Usb_driver structure owner to This_module, the owner pointer to your module itself. So now the owner says no, it's gone? This is a long talk, so let's cut it short. Do not know that time you have not forgotten to initialize owner, anyway is a lot of people will forget, we all focus on probe, disconnect and so on need to move the role of the brain, this does not need to move the brain, only a few seconds to specify the owner is often overlooked, This is easy to get often do not cherish, not easy to get to the often day thinking to fight. So in 2006 on the eve of the Chinese New Year, in our work is not intentional learning waiting for the Spring Festival, Greg Stick to the first line, remove the owner, so thousands of people write USB drive no longer have to remember to initialize the owner of the time. We do not have to set the owner, but the core can not be set, struct usb_driver structure is not the owner of it, but it is embedded in the struct DEVICE_DRIVER structure there is ah, set it can be. So Greg adds Usb_register_driver (), and Usb_register () can call it by specifying the parameter as This_module, and everything is moved into it. Anyway Usb_register () is also inline, and does not increase the cost of the call.

The rest of the relevant web sites, for reference:

Http://hi.baidu.com/_huangshuijing/blog/item/253f9a9516183f0c7bf480c5.html

Http://www.embedu.org/Column/Column92.htm

The following are for the kernel 2.6.18

The definition of this_module in Module.h is as follows:

extern struct module __this_module; #define This_module (&__this_module)

That is, the address of the __this_module object is saved, where is this __this_module defined? This will be compiled from the module, if compiled module will be found, will generate a file such as *.MOD.C, open this file, you will find, similar to the following definition:

struct Module __this_module__attribute__ ((Section (". Gnu.linkonce.this_module"))) = {. Name = Kbuild_modname,. init = Init_module, #ifdef config_module_unload. Exit = Cleanup_module, #endif};

This file was generated by calling Modpost, and in Modpost's main there was a code like this:

    for (mod = modules; mod; mod = mod->next) {        if (mod->skip)            continue;        Buf.pos = 0;        Add_header (&buf, mod);        Add_versions (&buf, mod);        Add_depends (&BUF, mod, modules);        Add_moddevtable (&buf, mod);        Add_srcversion (&buf, mod);        sprintf (fname, "%S.MOD.C", mod->name);        Write_if_changed (&buf, fname);    }

One of the Add_header has secretly added the definition of __this_module.

static void Add_header (struct buffer *b, struct module *mod) {    buf_printf (b, "#include <linux/module.h>\n"); c1/>buf_printf (b, "#include <linux/vermagic.h>\n");    buf_printf (b, "#include <linux/compiler.h>\n");    buf_printf (b, "\ n");    buf_printf (b, "Module_info (Vermagic, vermagic_string); \ n");    buf_printf (b, "\ n");    buf_printf (b, "struct module __this_module\n");    buf_printf (b, "__attribute__ (section (\". gnu.linkonce.this_module\ ")) = {\ n");    buf_printf (b, ". Name = kbuild_modname,\n");    if (mod->has_init)        buf_printf (b, ". Init = init_module,\n");    if (mod->has_cleanup)        buf_printf (b, "#ifdef config_module_unload\n"                  ". Exit = cleanup_module,\n"                  " #endif \ n ");    buf_printf (b, "};\n");}

THIS_MODULE macro definitions in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.