Why is the probe method needed in Linux device drivers in addition to init?

后端 未结 7 1238
有刺的猬
有刺的猬 2020-12-04 09:07

In the linux kernel, what does the probe() method, that the driver provides, do? How different is it from the driver\'s init function, i.e. why can

7条回答
  •  执念已碎
    2020-12-04 09:33

    Multiple devices and hotplug

    1. You are running a large server with many PICe connected GPUs accelerators. At some point you decide to buy more GPUs for the free slots.

      If we used init, then we'd have to rmmod and insmod the module. But that would require stopping all connected GPUs, which causes downtime.

      With probe, we just plug in the new GPUs do a rescan.

    2. PCIe hotplug wouldn't be possible otherwise: https://electronics.stackexchange.com/questions/208767/does-pcie-hotplug-actually-work-in-practice

    QEMU edu PCI device example

    QEMU has an educational PCI device called edu, which allows us to easily test when probe is called.

    First, we need a minimal Linux kernel PCI driver for it, which I've written here.

    We can start with the device attached with:

    -device edu
    

    but even more interestingly, we can attach and remove the device from the QEMU monitor as well, Ctrl + Alt + 2 on SDL GUI or -monitor telnet::45454,server,nowait on CLI, with the commands:

    device_add edu
    device_del edu
    

    If the device is attached at boot:

    • probe is called as soon as we insmod the module

    • dmesg contains a line of type: pci 0000:00:04: [1234:11e8] ... that shows that our device was probed to BDF 0000:00:04.

      We know that this is our device, because the vendor is 0x1234 and device id 11e8 in the QEMU source.

      So we conclude that PCI devices are probed at boot, and stored in a list somewhere.

    If we attach the device after boot from the monitor:

    • nothing happens until we do:

      echo 1 > /sys/bus/pci/rescan
      

      See also: How can the linux kernel be forced to enumerate the PCI-e bus?

    • after rescan:

      • if we had already insmodded, probe is called immediately.

        So in this case, probe happened separately from insmod, showing how they differ.

      • if we haven't: probe is called immediately after insmod

提交回复
热议问题