1. Computing

Discuss in my forum

The Linux Loadable Kernel Module How-To

By Bryan Henderson

12.1. Linux 2.4 - Linux 2.6

12.1.1. Linking Done In Kernel

The biggest change to LKMs between Linux 2.4 and Linux 2.6 is an internal one: LKMs get loaded much differently. Most people won't see any difference except that the suffix on a file containing an LKM has changed, because they use high level tools to manage the LKMs and the interface to those tools hasn't changed.

Before Linux 2.6, a user space program would interpret the ELF object (.o) file and do all the work of linking it to the running kernel, generating a finished binary image. The program would pass that image to the kernel and the kernel would do little more than stick it in memory. In Linux 2.6, the kernel does the linking. A user space program passes the contents of the ELF object file directly to the kernel. For this to work, the ELF object image must contain additional information. To identify this particular kind of ELF object file, we name the file with suffix ".ko" ("kernel object") instead of ".o" For example, the serial device driver that in Linux 2.4 lived in the file serial.o in Linux 2.6 lives in the file serial.ko .

So there is a whole new modutils package for use with Linux 2.6. In it, insmod is a trivial program, as compared to the full blown linker of the Linux 2.4 version.

Also, the procedure to build an LKM is somewhat harder. To make a .ko file, you start with a regular .o file. You run the program modpost (which comes with the Linux source code) on it to create a C source file that describes the additional sections the .ko file needs. We'll call this the .mod file because you conventionally include ".mod" in the file name.

You compile the .mod file and link the result with the original .o file to make a .ko file.

The .mod object file contains the name that the LKM instance will have when you load the LKM. You set that name with a -D compile option (when you compile the .mod file) that sets the KBUILD_MODNAME macro.

This change means some things are decidedly harder -- choosing the name for the LKM instance, for example. In Linux 2.4, the name was one of the inputs to the kernel. insmod decided on the name and passed it to the kernel. insmod 's -o option told it explicitly what to use for the LKM instance name. But in 2.6, there is no such parameter on the system call and hence no -o option on insmod . The name is part of the ELF object (.o file) that you pass to the kernel. The default name is built into the ELF object, but if you want to load it with some other name, you must edit the ELF image before passing it to insmod .

12.1.2. No Module Busy Function

In Linux 2.6 can_unload (see Section 11.4 ) is gone.


You can configure the kernel build to build a kernel that does not allow unloading of modules at all, thus sidestepping any problems with modules that get unloaded while still in use. See Section 11.4 .

12.1.4. Reference Counting

The interface that the code of an LKM uses to manipulate its reference count has been replaced.

   Prev    Home    Next
   Writing Your Own Loadable Kernel Module         Copyright Considerations With LKMs

* License

* Loadable Kernel Module How-To Index

  1. About.com
  2. Computing
  3. Linux

©2014 About.com. All rights reserved.