Starting with 2.6.13-RC3, the Linux kernel provides the ability to dynamically bind and Unbind devices and device drivers in user space. Prior to this, the insmod can only be bound and reconciled through the modprobe and rmmod, and this binding and bundling is for the driver and all devices. The new feature can set the connection between the driver and the individual device.
Here, we take the NVMe SSD for PCI bus as an example, first execute LSPCI display all NVMe SSDs.
# Lspci | grep memory
01:00.0 non-volatile Memory Controller:samsung Electronics Co LTD Device a802 (rev 01)
09:00.0 non-volatile Memory Controller:samsung Electronics Co LTD Device a802 (rev 01)
Like so many NVMe SSDs, we can see multiple nvme devices in/dev (the default loading of NVME drivers after OS boot).
# ls/dev/nvme*
/dev/nvme0/dev/nvme0n1p2/dev/nvme0n1/dev/nvme0n1p3/dev/nvme0n1p1/dev/nvme1/dev/nvme1n1
At the same time, for all nvme devices (here we take the PCI bus BDF number 09:00.0 SSD as an example), can be seen under the NVMe drive. Of these, bind and unbind files are key files that involve binding and unbinding.
/sys/bus/pci/drivers/nvme# LL
Total 0
Drwxr-xr-x 2 root root 0 Jan 4 17:10.
Drwxr-xr-x root root 0 Jan 4 17:10.. /
lrwxrwxrwx 1 root root 0 Jan 4 20:49 0000:01:00.0. /.. /.. /.. /devices/pci0000:00/0000:00:01.0/0000:01:00.0/
lrwxrwxrwx 1 root root 0 Jan 4 20:49 0000:09:00.0. /.. /.. /.. /devices/pci0000:00/0000:00:03.0/0000:05:00.0/0000:06:0a.0/0000:07:00.0/0000:08:01.0/0000:09:00.0/
--w-------1 root root 4096 Jan 4 20:46 Bind
lrwxrwxrwx 1 root root 0 Jan 4 20:49 module. /.. /.. /.. /module/nvme/
--w-------1 root root 4096 Jan 4 20:49 new_id
--w-------1 root root 4096 Jan 4 20:49 remove_id
--w-------1 root root 4096 Jan 4 17:10 uevent
--w-------1 root root 4096 Jan 4 17:10 Unbind
To unbind a nvme device, simply write the PCI bus BDF number of the device to/SYS/BUS/PCI/DRIVERS/NVME (different device drivers)/unbind:
/sys/bus/pci/drivers/nvme# echo-n "0000:09:00.0" > Unbind
Unbind successfully, and then view the file under the directory, the driver no longer has the corresponding device. At the same time, there is no corresponding nvme device in/dev.
/sys/bus/pci/drivers/nvme# LL
Total 0
Drwxr-xr-x 2 root root 0 Jan 4 20:51.
Drwxr-xr-x root root 0 Jan 4 20:49.. /
lrwxrwxrwx 1 root root 0 Jan 4 20:49 0000:01:00.0. /.. /.. /.. /devices/pci0000:00/0000:00:01.0/0000:01:00.0/
--w-------1 root root 4096 Jan 4 20:46 Bind
lrwxrwxrwx 1 root root 0 Jan 4 20:49 module. /.. /.. /.. /module/nvme/
--w-------1 root root 4096 Jan 4 20:49 new_id
--w-------1 root root 4096 Jan 4 20:49 remove_id
--w-------1 root root 4096 Jan 4 20:49 uevent
--w-------1 root root 4096 Jan 4 20:51 Unbind
Bind a NVMe device, and reconcile similar, to write the PCI bus BDF number of the device to/SYS/BUS/PCI/DRIVERS/NVME (different device drivers differ)/bind:
/sys/bus/pci/drivers/nvme# echo-n "0000:09:00.0" > Bind
The binding succeeds, showing all the files in that directory again, and you can see that the corresponding device appears again.
/sys/bus/pci/drivers/nvme# LL
Total 0
/sys/bus/pci/drivers/nvme# LL
Total 0
Drwxr-xr-x 2 root root 0 Jan 5 09:13.
Drwxr-xr-x root root 0 Jan 4 20:49.. /
lrwxrwxrwx 1 root root 0 Jan 4 20:49 0000:01:00.0. /.. /.. /.. /devices/pci0000:00/0000:00:01.0/0000:01:00.0/
lrwxrwxrwx 1 root root 0 Jan 5 09:13 0000:09:00.0. /.. /.. /.. /devices/pci0000:00/0000:00:03.0/0000:05:00.0/0000:06:0a.0/0000:07:00.0/0000:08:01.0/0000:09:00.0/
--w-------1 root root 4096 Jan 5 09:13 Bind
lrwxrwxrwx 1 root root 0 Jan 4 20:49 module. /.. /.. /.. /module/nvme/
--w-------1 root root 4096 Jan 4 20:49 new_id
--w-------1 root root 4096 Jan 4 20:49 remove_id
--w-------1 root root 4096 Jan 4 20:49 uevent
--w-------1 root root 4096 Jan 4 20:51 Unbind
Linux driver Manual binding and Unbind method