zoukankan      html  css  js  c++  java
  • Linux驱动手动绑定和解绑定

    Linux内核从2.6.13-rc3开始,提供了在用户空间,可动态的绑定和解绑定设备和设备驱动之间关系的功能。在这之前,只能通过insmod(modprobe)和rmmod来绑定和解绑,而且这种绑定和解绑都是针对驱动和所有设备的。而新的功能可以设置驱动和单个设备之间的联系。
      这里,我们以pci总线的nvme ssd为例,首先执行lspci显示所有的nvme ssd。

    # 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)
    

      有如上这么多nvme ssd,那么我们就可以在/dev下看到多个nvme设备(OS启动后默认加载nvme驱动)。

    # ls /dev/nvme*
    /dev/nvme0 /dev/nvme0n1p2 /dev/nvme0n1 /dev/nvme0n1p3 /dev/nvme0n1p1 /dev/nvme1 /dev/nvme1n1  
    

      同时,对于所有的nvme设备(这里我们以pci总线bdf号为09:00.0的ssd为例),都可以在nvme驱动下看到。其中,bind和unbind文件就是涉及到绑定和解绑的关键文件。

    /sys/bus/pci/drivers/nvme# ll
    total 0
    drwxr-xr-x  2 root root    0 Jan  4 17:10 ./
    drwxr-xr-x 30 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
    

      解绑一个nvme设备,只需将设备的pci总线bdf号写入/sys/bus/pci/drivers/nvme(不同的设备驱动不同)/unbind即可:

    /sys/bus/pci/drivers/nvme# echo -n "0000:09:00.0" > unbind
    解除绑定成功,再查看目录下文件,该驱动下不再有对应的设备。同时,/dev下也没有对应的nvme设备了。
    /sys/bus/pci/drivers/nvme# ll
    total 0
    drwxr-xr-x  2 root root    0 Jan  4 20:51 ./
    drwxr-xr-x 30 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
    

      绑定一个nvme设备,和解绑类似,将设备的pci总线bdf号写入/sys/bus/pci/drivers/nvme(不同的设备驱动不同)/bind:

    /sys/bus/pci/drivers/nvme# echo -n "0000:09:00.0" > bind
    绑定成功,再次展示该目录下所有文件,可以发现对应设备再次出现。
    /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 30 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
    
  • 相关阅读:
    《AI for Game Developers》第七章 A*路径寻找算法 (二)(skiplow翻译)
    芯片科普学习笔记
    sprboot 配置logback 日志输出
    springboot+mybatis 配置双数据源(mysql,oracle,sqlserver,db2)
    vue 封装axios请求
    *arg参数
    pytest mac安装了pytest,但是输入pytest却提示命令不存在
    构建Java Web开发环境
    在CentOS上编译安装PostgreSQL
    在Ubuntu 14.04上使用Eclipse开发和调试PosgreSQL9.3.4
  • 原文地址:https://www.cnblogs.com/styshoo/p/6281789.html
Copyright © 2011-2022 走看看