参考文献
https://devarea.com/linux-kernel-development-kernel-module-parameters/
https://stackoverflow.com/questions/11031554/kernel-module-parameters-changes-using-sys-module/33655017
https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-module
https://devarea.com/linux-kernel-development-kernel-module-parameters/
/sys/module有系统中所有模块的信息,不论这些模块是以内联(inlined)方式编译到内核映像文件(vmlinuz)中还是编译为外部模块(ko文件),都可能会出现在 /sys/module 中:
编译为外部模块(ko文件)在加载后会出现对应的 /sys/module/<module_name>/, 并且在这个目录下会出现一些属性文件和属性目录来表示此外部模块的一些信息,如版本号、加载状态、所提供的驱动程序等;
编译为内联方式的模块则只在当它有非0属性的模块参数时会出现对应的 /sys/module/<module_name>, 这些模块的可用参数会出现在 /sys/modules//parameters/<param_name> 中,
如 /sys/module/printk/parameters/time 这个可读写参数控制着内联模块 printk 在打印内核消息时是否加上时间前缀;
所有内联模块的参数也可以由 “<module_name>.<param_name>=” 的形式写在内核启动参数上,如启动内核时加上参数 “printk.time=1” 与 向 “/sys/module/printk/parameters/time” 写入1的效果相同;
没有非0属性参数的内联模块不会出现于此。
What: /sys/module
Description:
The /sys/module tree consists of the following structure:
/sys/module/MODULENAME
The name of the module that is in the kernel. This
module name will always show up if the module is loaded as a
dynamic module. If it is built directly into the kernel, it
will only show up if it has a version or at least one
parameter.
Note: The conditions of creation in the built-in case are not
by design and may be removed in the future.
/sys/module/MODULENAME/parameters
This directory contains individual files that are each
individual parameters of the module that are able to be
changed at runtime. See the individual module
documentation as to the contents of these parameters and
what they accomplish.
Note: The individual parameter names and values are not
considered stable, only the fact that they will be
placed in this location within sysfs. See the
individual driver documentation for details as to the
stability of the different parameters.
/sys/module/MODULENAME/refcnt
If the module is able to be unloaded from the kernel, this file
will contain the current reference count of the module.
Note: If the module is built into the kernel, or if the
CONFIG_MODULE_UNLOAD kernel configuration value is not enabled,
this file will not be present.
下面我们找个pciehp的模块看看下面三个模块参数,pciehp_debug,pciehp_poll_mode和pciehp_poll_time
[root@localhost parameters]# pwd
/sys/module/pciehp/parameters
[root@localhost parameters]# ll
total 0
-rw-r–r–. 1 root root 4096 Sep 9 21:31 pciehp_debug
-rw-r–r–. 1 root root 4096 Sep 9 21:31 pciehp_force
-rw-r–r–. 1 root root 4096 Sep 9 21:31 pciehp_poll_mode
-rw-r–r–. 1 root root 4096 Sep 9 21:31 pciehp_poll_time
[root@localhost parameters]# cat pciehp_debug
N
[root@localhost parameters]# cat pciehp_force
N
[root@localhost parameters]# cat pciehp_poll_mode
N
[root@localhost parameters]# cat pciehp_poll_time
0
-------------我们看到三个参数都是0
[root@localhost parameters]# echo 1 > pciehp_debug
[root@localhost parameters]# cat pciehp_debug
Y
-------------往pciehp_debug 写1后,pciehp_debug 变成Y
[root@localhost parameters]# cat /sys/bus/pci/slots/
18/ 19/ 50/ 51/
[root@localhost parameters]# cat /sys/bus/pci/slots/50/module/parameters/pciehp_debug
Y
[root@localhost parameters]# cat /sys/bus/pci/slots/18/module/parameters/pciehp_debug
Y
-------------sys/bus/pci/slots/slot_number//module/parameters/pciehp_debug 和/sys/module/pciehp/parameters/pciehp_debug是一样的
[root@localhost parameters]# echo 0 >/sys/bus/pci/slots/50/module/parameters/pciehp_debug
[root@localhost parameters]# cat /sys/bus/pci/slots/50/module/parameters/pciehp_debug
N
[root@localhost parameters]# cat /sys/bus/pci/slots/18/module/parameters/pciehp_debug
N
[root@localhost parameters]# pwd
/sys/module/pciehp/parameters
[root@localhost parameters]# cat pciehp_debug
N
-------------往/sys/bus/pci/slots/50/module/parameters/pciehp_debug 写0后,可以看到所有的pciehp_debug 都变成了0
我们看一下/sys/bus/pci/slots/slot_number下的module其实是link到/sys/modulles/pciehp的
接下来,我们来做个试验,我们把pciehp_debug改成1,然后查看电源状态
root@localhost parameters]# pwd
/sys/bus/pci/slots/18/module/parameters
[root@localhost parameters]#
[root@localhost parameters]# echo 1 > pciehp_debug
[root@localhost parameters]# cat pciehp_debug
Y
[root@localhost parameters]# cat /sys/module/pciehp/parameters/pciehp_debug
Y
[root@localhost parameters]# cat /sys/bus/pci/slots/18/power
0
我们发现pciehp_debug为1时,pciehp_get_power_status函数可以打印出debug信息
我们把pciehp_debug改成0
[root@localhost parameters]# pwd
/sys/bus/pci/slots/18/module/parameters
[root@localhost parameters]# echo 0 > pciehp_debug
[root@localhost parameters]# cat pciehp_debug
N
[root@localhost parameters]# cat /sys/module/pciehp/parameters/pciehp_debug
N
[root@localhost parameters]# cat /sys/bus/pci/slots/18/power
0
[root@localhost parameters]#
发现,仍然可以获取电源状态,但是没有任何打印。
注意:pciehp_debug功能已经在新的linux版本取消了,改为使用linux统一的debug功能,详细见linux kernel patch
https://lore.kernel.org/lkml/20190509141456.223614-6-helgaas@kernel.org/
https://lore.kernel.org/lkml/20190509141456.223614-7-helgaas@kernel.org/