1. Introduction

As Linux developers and system administrators, for effectively working with the Linux kernel, it’s crucial for us to understand the difference between kernel drivers and kernel modules.

In this tutorial, we’ll delve into the details of kernel drivers and kernel modules, exploring their roles, advantages, and differences. Let’s dive right in!

2. Kernel Drivers

Kernel drivers are an integral part of the Linux kernel and play a vital role in interacting with hardware devices. As kernel drivers are software components, we can consider them translators between the operating system (OS) and the physical devices connected to our computers.

By enabling communication between the OS and various devices like graphics cards, network adapters, keyboards, and mice, kernel drivers ensure the smooth functionality of our Linux systems.

Typically, the kernel implements kernel drivers, which reside in the kernel’s source code and load into the memory during system boot. Operating at the kernel level, kernel drivers have direct access to the hardware, facilitating efficient and secure communication with devices.

Some common examples of kernel drivers include network drivers for managing network interfaces, sound drivers for audio devices, and display drivers for graphics cards. These drivers provide essential functionalities to control and utilize the hardware effectively.

3. Kernel Modules

While kernel drivers are an integral part of the kernel, kernel modules offer a more flexible approach. Kernel modules are separate pieces of code that can dynamically load and unload from the kernel during runtime. They extend the kernel’s functionality by adding or modifying device drivers or other kernel components without requiring a complete kernel recompilation.

Furthermore, kernel modules offer several advantages. They allow for easy installation and removal of device drivers without rebooting the system. This dynamic nature makes them particularly useful when working with hardware that may be frequently added or removed. Additionally, kernel modules enable us to add new features or fix bugs in existing drivers without impacting the entire kernel.

The kernel modules used on our system vary depending on the specific system and its configuration. However, let’s familiarize ourselves with some commonly used kernel modules:

  • nvidia – provides support for NVIDIA graphics cards
  • snd_hda_intel – manages Intel High Definition Audio devices
  • iwlwifi – handles Intel Wireless LAN devices
  • btusb – controls Bluetooth devices using USB interfaces
  • usb_storage – supports USB storage devices like external hard drives or USB flash drives

These examples represent only a small subset of the numerous kernel modules available, each serving a specific purpose to enable hardware support or provide additional functionality in the Linux kernel.

3.1. Loading a Kernel Module

To load a kernel module, we can execute the modprobe command with administrative privileges (sudo) followed by the module’s filename, typically without the .ko extension.

When we use modprobe, it not only loads the specified module but also resolves and loads any dependencies required by that module. This command loads the specified kernel module into the kernel’s address space and ensures that the module can function properly within the kernel environment:

$ sudo modprobe my_module

Here, we attempt to insert the my_module kernel module into the kernel. If the module loads successfully, there will be no output. Otherwise, it displays any error messages or warnings related to the module loading process.

3.2. Unloading a Kernel Module

Similar to our previous interaction, if we want to unload a loaded kernel module from the running kernel, we use modprobe with the -r option. We simply provide the module’s name with sudo:

$ sudo modprobe -r my_module

Here, we unload the my_module kernel module from the kernel.

4. The lspci Command

Before we delve deeper into the distinction between kernel drivers and kernel modules, let’s take a moment to examine the output of the lspci command with the -k flag:

$ lspci -k
01:00.0 VGA compatible controller: NVIDIA Corporation G86 [Quadro NVS 290] (rev a1)
    Subsystem: NVIDIA Corporation Device 0492
    Kernel driver in use: nvidia
    Kernel modules: nvidia_current, nouveau, nvidiafb

The lspci command is a Linux utility that provides information about the Peripheral Component Interconnect (PCI) devices connected to our system. It provides detailed information about the hardware devices and their corresponding drivers. When used with the -k flag, it not only displays details about the PCI devices but also shows the associated kernel driver and kernel modules for those devices.

5. Kernel Drivers vs. Kernel Modules

In our previous interaction, the Kernel driver in use field indicates that the active kernel driver for the graphics card is nvidia. This means that the OS currently utilizes the nvidia kernel driver to communicate with and manage the graphics card.

On the other hand, the Kernel modules field lists the kernel modules associated with the device. In our example, they are nvidia_current, nouveau, and nvidiafb. These modules are additional pieces of code that load dynamically to provide extended functionalities for the graphics card.

Furthermore, with tools like insmod or modprobe, we can dynamically insert or remove a kernel module (a piece of code) from the kernel. In contrast, a kernel driver is a specific and fixed program that interacts directly with hardware devices.

It’s important to note that not all kernel modules are drivers. For instance, a kernel module can provide services or extend the kernel’s functionality in various ways beyond interacting with hardware. For example, a module may be responsible for loading a different process scheduler or implementing specific elements of a complex hardware stack, such as the USB HID driver.

The primary distinction lies in the kernel driver’s focus on facilitating communication with hardware devices, while kernel modules serve different purposes within the kernel. Now, let’s see the differences between kernel drivers and kernel modules.

5.1. Direct Interaction With Hardware

A kernel driver directly interacts with hardware devices. It bridges the OS kernel and the specific hardware component, enabling seamless communication and control.

In our previous example, the nvidia driver manages the NVIDIA Corporation G86 graphics card. This driver loads into the kernel and provides the necessary functionalities to utilize the graphics card effectively. This makes kernel drivers essential for the stable operation of critical hardware components that require direct access to the kernel.

Typically, they integrate into the kernel file on disk and load during system boot. Being part of the kernel gives these drivers direct access to hardware, resulting in slightly better performance than kernel modules.

5.2. Flexibility and Dynamic Loading

On the other hand, kernel modules offer flexibility and modularity through dynamic loading or unloading from the kernel at runtime, allowing for more flexibility in managing the kernel’s functionality. The nvidia_current, nouveau, and nvidiafb modules mentioned earlier are examples of kernel modules associated with the NVIDIA graphics card. These modules provide flexible, enhanced options for driving the hardware based on our needs or the system’s performance.

The flexibility of kernel modules is particularly useful when we work with hardware that may be hot-plugged or when we modify specific kernel components without affecting the entire system or rebooting it.

Moreover, kernel modules allow for targeted modifications or additions to specific parts of the kernel, reducing complexity and potential risks associated with modifying the entire kernel. As developers, we can add new features, fix bugs, or experiment with different modules without recompiling the kernel.

5.3. System Stability

Since kernel drivers load into memory during the system boot process as an integral part of the kernel, they provide a higher level of stability. The close integration with the kernel ensures reliable and secure communication between the OS and the hardware devices.

On the other hand, kernel modules, while providing flexibility and modularity, introduce the possibility of dynamically loading and unloading code into the kernel. This introduces a certain level of risk, as improper handling or faulty modules may cause instability or system crashes.

6. Conclusion

In this article, we explored the differences between kernel drivers and kernel modules. We learned that kernel drivers are an integral part of the Linux kernel, providing direct interaction with hardware devices, while kernel modules offer flexibility and modularity through dynamic loading.

By leveraging the strengths of kernel drivers and kernel modules, we can effectively utilize and extend the capabilities of the Linux kernel while ensuring system stability and flexibility.

Finally, with our better understanding of kernel drivers and kernel modules, we can confidently navigate the Linux ecosystem and make informed choices in our development and system administration tasks.

Comments are closed on this article!