Linux – Where does modprobe load a driver that udev requests

driverskernel-moduleslinux-kernelmodprobeudev

Let's take a scenario where a Linux system has booted and is running correctly. A user comes along and hotplugs a USB memory device.

The sequence of events that happen are illustrated on the diagram below:

enter image description here

Where does modprobe load its driver into ?
Is the driver for the requested device found in /sys/bus/drivers after modprobe loads it or before ?

What I'm trying to establish here is the relationship between entries in /sys/ and the events that happen on the graph above.

Best Answer

The uevent message contains information about the device (example). This information contains registered vendor and model identification for devices connected to buses such as PCI and USB. Udev parses these events and constructs a fixed-form module name which it passes to modprobe. modprobe looks under /lib/modules/VERSION for a file called depmod.alias which is generated when the kernel is installed and that maps the fixed-form module names to actual driver module file names. See Are driver modules loaded and unloaded automatically? for more details about the process — that answer describes the earlier days when the kernel called modprobe directly, but the way modprobe and module aliases work hasn't changed.

See also Michael Opdenacker's presentation “Hotplugging with udev” which has more examples and describes other aspects of device management with udev, and the Linux from scratch guide which has a section on how the fixed-form module names are defined.

modprobe loads a module by calling the init_module system call. It doesn't interact with sysfs in any way. When the module is loaded, the kernel creates an entry for it in /sys/module. Any entry that appears elsewhere in sysfs is up to the code in the module (e.g. a module with a driver for a type of USB devices will call some generic USB support code that adds an entry under /sys/bus/usb/drivers).

Related Question