94

When I do a lspci -k on my Kubuntu with a 3.2.0-29-generic kernel I can see something like this:

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

There is a kernel driver nvidia and kernel modules nvidia_current, nouveau, nvidiafb.

Now I wondered what might be the difference between Kernel drivers and Kernel modules?

6 Answers 6

107

A kernel module is a bit of compiled code that can be inserted into the kernel at run-time, such as with insmod or modprobe.

A driver is a bit of code that runs in the kernel to talk to some hardware device. It "drives" the hardware. Most every bit of hardware in your computer has an associated driver.¹ A large part of a running kernel is driver code.²

A driver may be built statically into the kernel file on disk.³ A driver may also be built as a kernel module so that it can be dynamically loaded later. (And then maybe unloaded.)

Standard practice is to build drivers as kernel modules where possible, rather than link them statically to the kernel, since that gives more flexibility. There are good reasons not to, however:

  • Sometimes a given driver is absolutely necessary to help the system boot up. That doesn't happen as often as you might imagine, due to the initrd feature.

  • Statically built drivers may be exactly what you want in a system that is statically scoped, such as an embedded system. That is to say, if you know in advance exactly which drivers will always be needed and that this will never change, you have a good reason not to bother with dynamic kernel modules.

  • If you build your kernel statically and disable Linux's dynamic module loading feature, you prevent run-time modification of the kernel code. This provides additional security and stability at the expense of flexibility.

Not all kernel modules are drivers. For example, a relatively recent feature in the Linux kernel is that you can load a different process scheduler. Another example is that the more complex types of hardware often have multiple generic layers that sit between the low-level hardware driver and userland, such as the USB HID driver, which implements a particular element of the USB stack, independent of the underlying hardware.


Asides:

  1. One exception to this broad statement is the CPU chip, which has no "driver" per se. Your computer may also contain hardware for which you have no driver.

  2. The rest of the code in an OS kernel provides generic services like memory management, IPC, scheduling, etc. These services may primarily serve userland applications, as with the examples linked previously, or they may be internal services used by drivers or other intra-kernel infrastructure.

  3. The one in /boot, loaded into RAM at boot time by the boot loader early in the boot process.

3
  • 1
    Modules can be filesystems, network protocols, firewall functionalities, and a lot more. Some hardware (e.g. WiFi cards) require a stack of modules, some offering general infrastructure while others handle the hardware itself.
    – vonbrand
    Commented Dec 30, 2015 at 14:48
  • 3
    This is a good general outline, but I had exactly the same question as the OP, then came across this answer and still did not know why the "driver in use" is different from the "modules". In contrast, @Jim Paris's answer is correct. From man lspci: " -k Show kernel drivers handling each device and also kernel modules capable of handling it." You could read that as: "Show the driver currently / actually handling the device and also all modules which could / are meant to handle it".
    – Binarus
    Commented Dec 27, 2017 at 16:38
  • If you know windows: A module is very similar to a DLL. On unix, a module is similar to a shared object, but a module is just for the kernel. A dynamically linked module can contain drivers. A kernel can contain statically linked drivers. A module is different from a DLL (or .so) because the kernel has specific requirements for how things get dynamically loaded.
    – robocat
    Commented May 14, 2019 at 22:41
29

To answer your specific question about the lspci output, the "kernel driver" line refers to which driver is currently bound to the card, in this case the proprietary nvidia driver. The "kernel modules" line lists all of the drivers known to be capable of binding to this card. Here, the proprietary driver shows up it a different name, probably due to how lspci found the driver and its filename versus the name coded into the driver itself.

1
  • Thanks - that helped. If I only had issued man lspci - it says exactly what you wrote.
    – Binarus
    Commented Dec 27, 2017 at 16:40
11

A kernel module may not be a device driver at all

"Kernel driver" is not a well defined term, but let's give it a shot.

This is a kernel module that does not drive any hardware, and thus could not be reasonably considered a "device driver":

#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");

static int myinit(void)
{
    printk(KERN_INFO "hello init\n");
    return 0;
}

static void myexit(void)
{
    printk(KERN_INFO "hello exit\n");
}

module_init(myinit)
module_exit(myexit)

After build, you can use it with:

insmod hello.ko

and it prints hello init to dmesg.

There are, however, kernel modules that are not device drivers, but are actually useful, e.g., modules that expose kernel debugging / performance information.

Device drivers are usually also kernel modules.

An example of something that is a "device driver" is a bit harder to generate, since it requires a hardware to drive, and hardware descriptions tend to be complicated.

Using QEMU or other emulators however, we can construct software models of real or simplified hardware, which is a great way to learn how to talk to hardware.  Here is a simple example of a minimal PCI device driver: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/pci.c

We then see that in x86, talking to hardware comes down to:

Those operations cannot in general be done from userland, as explained at: What is difference between User space and Kernel space? There are however some exceptions: https://stackoverflow.com/questions/7986260/linux-interrupt-handling-in-user-space.

The kernel then offers higher level APIs to make such hardware interaction easier and more portable:

  • request_irq to handle interrupts
  • ioreadX and IO memory mapping
  • even higher level interfaces for popular protocols like PCI and USB
5

According to this nice tutorial :

...one type of module is the device driver, which allows the kernel to access hardware connected to the system.

So, if we try to draw a tree, we will have "Device driver" which inherits from (extends) Module, and which has more specific characteristics, between which we find "accessing to hardware"...

1
  • This is only partially correct. The driver is an object of a class in the hierarchy (yes, Linux' internal design, as most current operating systems, is object oriented). But said driver might be a module (loadable at runtime) or compiled into the kernel. There is no (or very little) difference between the alternatives, code wise.
    – vonbrand
    Commented Dec 30, 2015 at 14:52
0

My answer will go with Jim. A kernel driver is a program (kernel module) that is designed to drive a piece of hardware. The lspci output says nvidia is the kernel driver as it is the loaded module for the device. Along with it comes other available kernel modules available.

I'll add that the commands in linux to list and remove drivers are lsmod and rmmod respectively. Which says list module and remove module.

0

All Drivers are modules. Not all modules are drivers.

Modules can be inserted at runtime. Modules/Drivers can be statically compiled along with the kernel also.

Typical module init has

module_init(init_fn);
init_fn()
{
   /* some code */
}

The same module can be made a driver

module_init(init_fn);
init_fn()
{
   device_register(&device);
   /* some code */
}
5
  • 9
    Drivers aren't always modules, they can be included in the main kernel image. Commented Sep 5, 2012 at 22:06
  • 3
    @Prabagaran: "All Drivers are modules. All modules are not drivers." This is contradictory. In mathematical terms, what you are saying is D -> M and M -> !D. This allows for D and !D.
    – user22304
    Commented Sep 13, 2012 at 10:50
  • 3
    I think he means "All drivers are modules. Not all modules are drivers".
    – Renan
    Commented Sep 14, 2012 at 1:29
  • 4
    @Renan: That would be correct, but if you look at the edit history for this answer somebody already tried to fix the mistake and the author reverted it. Normally I would just edit to fix the error and move on, but in this case I've -1'ed because it's just wrong and confusing the issue.
    – Caleb
    Commented Sep 25, 2012 at 11:39
  • As I remember (haven't fooled around in a while) there are some drivers that can't be built as loadable modules. I seem to remember others which can only be handled as modules.
    – vonbrand
    Commented Dec 30, 2015 at 14:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .