Linux kernel modules

Source: Internet
Author: User

1 , what is a kernel module?

The kernel module is a Linux-provided mechanism that allows dynamic loading into the kernel while the kernel is running, with two features:

1) kernel module itself does not compile into kernel image, effectively control reduced kernel image size

2) Once the kernel module is loaded, he is exactly the same as the rest of the kernel.

2 , why do I need kernel modules?

If all functions are compiled at the kernel compile time, the kernel will be large and the kernel must be recompiled to add or remove features to the kernel.

For example, in Ubuntu on a general-purpose PC platform, it is impossible to know what device is needed beforehand, and we do not know what driver to pre-compile.

3 , kernel modules, and application differences

Working mode

Nature of work




Race State

Operating mode


USR Mode

Strategic nature

User Layer





Kernel modules

SVC Mode

Functional performance

Kernel layer




Be blocked

4 , the basic components of kernel modules

|        --two functions (typically required)| |        --module initialization (load) function: Do some preparatory work when kernel modules are loaded into the kernel | | --Module unload function: Recycle, clean up Resources | | --Authorization (license statement) (required): The Linux kernel is subject to the GPL (general public License) Authorization Constraint | --module parameters (optional): The value that the module can be passed to when it is loaded, which itself corresponds to the global variables within the module | --Module export symbol (optional)|--module Information description (optional)

5. Module load (initialize) function

Generally with __init logo declaration

function naming rules xxx_init XXX device name init function name (Initialize)

function form:

static ini __init xxx_init (void /* Initialization code * Return value: Success: 0 Failure: Negative number, absolute value is error code * The return value obtained by the application layer is-1, the error code is saved to errno (one per process); The standardized errno.h has been clearly defined linux/errno.h  */}

Registration method: module_init (x); x is the first address of the module initialization function

6. module Unload function

Generally with __exit logo declaration

function naming rules xxx_exit XXX device name exit feature name (uninstall)

static ini __init xxx_init (void/**/}

Registration method: module_exit (x); x is the first address of the module unload function

7 , module license Statement

Module_license (_license)//_license is the string of the authorization name//"GPL" [GNU Public License v2 or later]//"GPL v2" [GNU public License v2]//"GPL and additional rights" [GNU Public License v2 rights and more]//"Dual BSD/GPL" [GNU Public License v2 or BSD License Choice]//"Dual MIT/GPL" [GNU Public License v2 or MIT License Choice]//"Dual MPL/GPL" [GNU Public License v2 or Mozilla License Choice]

8. Module declaration and Description

In the Linux kernel module, we can use Module_author, module_description, Module_version, module_device_table, Module_alias respectively to declare the author of the module, description , versions, device tables, and aliases, such as:

Module_author (AUTHOR); Module_description (DESCRIPTION); Module_version (version_string); Module_device_table (Table_info); Module_alias (Alternate_name);

For device drivers such as USB and PCI, a module_device_table is typically created to indicate which devices are supported by the driver module, such as:

/* List of devices that correspond to this drive */

Static struct usb_device_id skel_table [] =/* / }}; Module_device_table (USB, skel_table);

9 , module parameters : When loading the module, the module can be passed the parameter

Header file linux/moduleparam.h

A , passing ordinary variables

Module_param (name, type, perm); Declare kernel module parameters /* name-the variable name of the receive parameter type-variant types Are:byte, short, ushort, int., uint, long, ulong charp:a character Point  ER bool:a bool, values 0/1, y/n, y/n. Invbool:the above, only sense-reversed (N = True) Perm-Permission header file Linux/stat.h #define S_IRWXUGO (s_irwxu| S_irwxg| S_IRWXO) #define S_IALLUGO (s_isuid| s_isgid| S_isvtx| S_irwxugo) #define S_IRUGO (s_irusr| s_irgrp| S_iroth) #define S_IWUGO (s_iwusr| s_iwgrp| S_iwoth) #define S_IXUGO (s_ixusr| s_ixgrp| S_ixoth)* /


int 0  int0644);


# insmod Xxx.ko i=

B , passing array parameters

Module_param_array (name, type, Nump, perm) /* declaring kernel module array parameters name-array name Type-array member type nump– a pointer to an integer variable that holds the length of the array perm-permissions */


int arr[] = {1,2,3,4,5,6}; int len=0int0644);


# insmod Xxx.ko arr=1,2,3,4,5

C , passing string parameters

string , Len, Perm) /* declare kernel module string parameter name-the external name of the string cache (incoming variable name) string-The internal name of the string cache Nump-the number of arrays perm-permissions */


Char " Hello World "  0644);


# insmod Xxx.ko extstr="hello"

compiling kernel modules

If a kernel module is to be loaded to run in a kernel, the module must be compiled with the source code that compiles the kernel image, or the runtime will get an error

A , header Files (grammatical problems)

B , compiling results (most important impact)

    • Compile-time symbol table (only used at compile time)
    • Run-time kernel symbol table
    • # cat/proc/kallsyms runtime kernel symbol table

C , compiling the system

Example makefile:

12, kernel makefile find kernel module makeifle kernel module makeifle definition to compile object Ifneq ($ (kernelrelease),) # To compile the object to represent the demo.c compiled into Demo.ko obj-m = demo.oelse# Kernel source directory Kerneldir: =/lib/modules/$ (Shell uname-r)/buildpwd: = $ (shell pwd) 
    Modules:  $ (make)-C $ (Kerneldir) m=$ (PWD) modulesendifclean:  rm-rf. Tmp_versions Mod Ule.symvers modules.order tmp_versions. *.cmd *.o *.ko *.mod.c

Kernelrelease is a variable defined in the top-level makefile of the kernel source, and kernelrelease is not defined when the first read executes this makefile, so make reads the content after the else is executed.

When the target of make is all, the-C $ (kerneldir) indicates that jumping to the kernel source directory reads the makefile;m=$ (PWD) there and then returns to the current directory to continue to read in and execute the current makefile.

When returned from the kernel source directory, Kernelrelease has been defined and Kbuild is also initiated to parse the Kbuild syntax, and make will continue to read the content before else. Else before is a Kbuild syntax statement that indicates the dependencies of the files in the module's source code and the name of the target module to be generated.

The name of each kernel contains its version number, which is also the value displayed by the Uname-r command.

One , operating the kernel module

A , loading modules

    1. # Insmode Module file name
    2. # modprobe Module Name # depmod
    3. # modinfo can see the module information

B , view modules

# Lsmod

Module supermodel block size using count users (by no content is the user module, there is useless kernel module)

Module size used by

Demo 2333 0 (used is the current number in use, for 0 o'clock only allowed to be uninstalled)

Mptscsih 39530 1 Mptspi

C , uninstalling the module

# RMMOD Module name (used is 0 o'clock only allowed to be uninstalled)

D , view kernel output information

# DMESG | Tail–n 10/* View the last ten lines in the post */

F , export symbol table

#define EXPORT_SYMBOL (export symbol table), the symbol can be a global variable, or it can be a function

Linux kernel modules

Related Article

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: 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.