Modules
Intro to Kernel Programming
Linux modules are lumps of code that can be
dynamically linked into the kernel at any point
after the system has booted. They can be
unlinked from the kernel and removed when they
are no longer needed. Mostly Linux kernel
modules are device drivers, pseudo-device
drivers such as network drivers, or file-systems.
You can either load and unload Linux kernel
modules explicitly using
the insmod and rmmod commands or the kernel
itself can demand that the kernel daemon
(kerneld) loads and unloads the modules as they
are needed.
Once a Linux module has been loaded it is as
much a part of the kernel as any normal kernel
code. It has the same rights and responsibilities
as any kernel code; in other words, Linux kernel
modules can crash the kernel just like all kernel
code or device drivers can.
As each module is loaded, the kernel modifies
the kernel symbol table, adding to it all of the
resources or symbols exported by the newly
loaded module. This means that, when the next
module is loaded, it has access to the services of
the already loaded modules.
When the module is unloaded, the kernel
removes any symbols that that module exported
into the kernel symbol table.
Demand loaded kernel modules are normally
kept in /lib/modules/kernel-version. The kernel
modules are linked object files just like other
programs in the system except that they are
linked as a relocatable images. That is, images
that are not linked to run from a particular
address. They can be either a.out or elf format
object files. insmodmakes a privileged system
call to find the kernel's exported symbols.
insmod physically writes the address of the
symbol into the appropriate place in the module.
When insmod has fixed up the module's
references to exported kernel symbols, it asks the
kernel for enough space to hold the new kernel,
again using a privileged system call. The kernel
allocates a newmodule data structure and enough
kernel memory to hold the new module and puts
it at the end of the kernel modules list. The new
module is marked as UNINITIALIZED.
• Rmmod Delete
• Calls clean_up to remove datastructures
• Running the module Make
• Incorporating into kernel
• Inserts the module parameters into kernel list Insmod
• Memory references associated
Module LifeCycle
The kernel calls the modules initialization
routine and, if it is successful it carries on
installing the module. The module's cleanup
routine address is stored in it's module data
structure and it will be called by the kernel when
that module is unloaded. Finally, the module's
state is set to RUNNING.
This is reflected mostly in the functions
described in between the init_module and
cleanup_module
This field is slightly overloaded as it also holds
the AUTOCLEAN and VISITED flags. Both of
these flags are used for demand loaded modules.
These modules are marked as AUTOCLEAN so
that the system can recognize which ones it may
automatically unload. The VISITED flag marks
the module as in use by one or more other system
components; it is set whenever another
component makes use of the module.
Each time the system is asked by kerneld to
remove unused demand loaded modules it looks
through all of the modules in the system for
likely candidates. It only looks at modules
marked as AUTOCLEAN and in the
stateRUNNING. If the candidate has
its VISITED flag cleared then it will remove the
module, otherwise it will clear the VISITED flag
and go on to look at the next module in the
system.
Assuming that a module can be unloaded, its
cleanup routine is called to allow it to free up the
kernel resources that it has allocated.
The module data structure is marked
as DELETED and it is unlinked from the list of
kernel modules. Any other modules that it is
dependent on have their reference lists modified
so that they no longer have it as a dependent. All
of the kernel memory that the module needed is
deallocated.