參考文獻
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]#
發現,仍然可以獲取電源狀態,但是沒有任何打印。