linux – 为什么这个内核模块在2.6.39上被标记为永久

前端之家收集整理的这篇文章主要介绍了linux – 为什么这个内核模块在2.6.39上被标记为永久前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
加载此模块时:
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4.  
  5. MODULE_LICENSE("Dual BSD/GPL");
  6.  
  7. static int hello_init(void) {
  8. printk("<1> Hello world!\n");
  9. return 0;
  10. }
  11.  
  12. static void hello_exit(void) {
  13. printk("<1> Bye,cruel world\n");
  14. }
  15.  
  16.  
  17. module_init(hello_init);
  18. module_exit(hello_exit);

(从http://www.freesoftwaremagazine.com/articles/drivers_linux?page=0,2)

该模块在lsmod中被标记为[永久],不能卸载,在2.6.39-02063904-generic(从Ubuntu PPA).但它在默认的2.6.38内核上工作正常. (在Ubuntu 11.04 x86上).

2.6.39发生了什么变化?我的代码需要改变什么?

当我遇到这个问题时,我试图分离一个更复杂的问题.

编辑:

根据答案的建议,我编辑了添加__init和__exit(hello3.c)的代码

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4.  
  5. MODULE_LICENSE("Dual BSD/GPL");
  6.  
  7. static int __init hello_init(void) {
  8. printk("<1> Hello world!\n");
  9. return 0;
  10. }
  11.  
  12. static void __exit hello_exit(void) {
  13. printk("<1> Bye,cruel world\n");
  14. }
  15.  
  16. module_init(hello_init);
  17. module_exit(hello_exit);

构建输出

  1. make -C /lib/modules/2.6.39-02063904-generic/build M=/home/douglas/kernelmod modules
  2. make[1]: Entering directory `/usr/src/linux-headers-2.6.39-02063904-generic'
  3. Building with KERNELRELEASE = 2.6.39-02063904-generic
  4. CC [M] /home/douglas/kernelmod/hello3.o
  5. Building modules,stage 2.
  6. Building with KERNELRELEASE = 2.6.39-02063904-generic
  7. MODPOST 8 modules
  8. CC /home/douglas/kernelmod/hello3.mod.o
  9. LD [M] /home/douglas/kernelmod/hello3.ko
  10. make[1]: Leaving directory `/usr/src/linux-headers-2.6.39-02063904-generic'

EDIT2:

hello3.mod.c:

  1. #include <linux/module.h>
  2. #include <linux/vermagic.h>
  3. #include <linux/compiler.h>
  4.  
  5. MODULE_INFO(vermagic,VERMAGIC_STRING);
  6.  
  7. struct module __this_module
  8. __attribute__((section(".gnu.linkonce.this_module"))) = {
  9. .name = KBUILD_MODNAME,.init = init_module,#ifdef CONFIG_MODULE_UNLOAD
  10. .exit = cleanup_module,#endif
  11. .arch = MODULE_ARCH_INIT,};
  12.  
  13. static const struct modversion_info ____versions[]
  14. __used
  15. __attribute__((section("__versions"))) = {
  16. { 0xbe4b3e92,"module_layout" },{ 0xb4390f9a,"mcount" },{ 0x5e3b3ab4,"printk" },};
  17.  
  18. static const char __module_depends[]
  19. __used
  20. __attribute__((section(".modinfo"))) =
  21. "depends=";
  22.  
  23.  
  24. MODULE_INFO(srcversion,"D2A869459874C22AB265981");

  1. # grep CONFIG_MODULE_UNLOAD /boot/config-2.6.39-02063904-generic
  2. CONFIG_MODULE_UNLOAD=y

EDIT3:

更有趣的是,我自己编译的香草内核不会发生这种情况
加载和卸载模块正常.

EDIT4:

我在虚拟机上安装了Oneiric beta 2内核,而3.0.0-11内核也没有任何问题.所以它似乎局限于Ubuntu香草PPA内核.这不是很有趣的解决.

解决方法

所以在咨询Canonical之后,我知道问题是什么:

Ubuntu mainline builds是用Hardy工具链构建的,11.04和11.10工具链与构建树外核心模块是不兼容的.

猜你在找的Linux相关文章