I am trying to write kernel module for ubuntu 12.04 LTS OS.
kernel version is 3.4.0-030400-generic-pae
I am able to compile it & load it to kernel.
For loading I use
sudo insmod nmod_main.ko
Now, If I try to remove it using
sudo rmmod nmod_main.ko
it gives me bellow error.
ERROR: Removing 'nmod_main': Device or resource busy
lsmod gives bellow information:
Module Size Used by
nmod_main 12394 0 [permanent]
Why this module shows permanent?
Bellow is the C-code for this module.
/********** Start of code ************/
#define __KERNEL__
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "init_module() called\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "cleanup_module() called\n");
}
/********** End of code ************/
I am getting some compilation warnings as bellow:
Building with KERNELRELEASE = 3.4.0-030400-generic-pae
CC [M] ../src/nmod_main.o
../src/nmod_main.c:1:0: warning: "KERNEL" redefined [enabled by default]
:0:0: note: this is the location of the previous definition
../src/nmod_main.c:2:0: warning: "MODULE" redefined [enabled by default]
:0:0: note: this is the location of the previous definition
Building modules, stage 2.
Building with KERNELRELEASE = 3.4.0-030400-generic-pae
Please can anybody help me in this regard.
I am using bellow makefile for building this module:
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
.PHONY: build clean
build:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf ../src/*.o ../src/*~ core ../src/.depend ../src/.*.cmd ../src/*.ko ../src/*.mod.c
else
$(info Building with KERNELRELEASE = ${KERNELRELEASE})
obj-m := ../src/nmod_main.o
endi
This problem is solved. Yes, it is problem related to toolchain. I downloaded .deb files to install Linux Kernel from url.
But I was not sure about the toolchain used to generate these .deb files. So I finally downloaded Linux kernel source from url, compiled & installed on my laptop. Then compiled my module. Problem disappeared.
Thanks #avd for providing me valuable clue.
You need to add module entry points which will allow kernel to load or unload module.Without module_init and module_exit kernel does'nt know how to unload the module and module became permanent. BUt dont know to fix i need solution How to remove this permanent module.
Related
I am looking into cross compiling a kernel module for an ARM linux. I have my toolchain installed.
But there's something I am not quite getting from various how-tos.
The module I want to build is gadgetfs.
The kernel version on my host is 3.5.0-34-generic while
on the target it's 3.6.9-0.1
Now what kernel sources or headers do I actually need to download and install, and where?
I downloaded linux-3.6.9.tar.bz2 from kernel.org and extracted it.
In drivers/usb/gadget/ there's a Makefile and according to this site I need to append these lines to it, then run make:
KDIR := /lib/modules/`uname -r`/build
PWD := `pwd`
obj-m := dummy_hcd.o gadgetfs.o
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
But what do i have to replace uname -r with? Cause this would give me my host's kernel version. But my target version is different. Where is the /lib/modules/3.6.9 folder?
CROSS_COMPILE and ARCH is both set.
You need to cross compile (or download pre-compiled) matching version of Linux for your target on your host machine with right configuration since Linux doesn't have a stable binary API. Host's kernel version is not relevant.
After having target build available on your host you can build a module via
make -C kernel_build_dir M=`pwd` ARCH=arm CROSS_COMPILE=<...> modules
under that module's directory.
I have been trying to cross compile a loadable kernel module and I have been getting warning message that I would like to get help.
Below is the warning message
make[1]: Entering directory `/home/userid/rowboat-android/kernel'
Building modules, stage 2.
MODPOST 1 modules
WARNING: "omap_device_build" [/home/userid/myfiles/lcdc_load_device.ko] undefined!
WARNING: "omap_hwmod_lookup" [/home/userid/myfiles/lcdc_load_device.ko] undefined!
make[1]: Leaving directory `/home/userid/rowboat-android/kernel'
Below is the Makefile
obj-m :=lcdc_load_device.o
lcdc_load_device-m := ../rowboat-android/kernel/arch/arm/plat-omap/omap_device.o
lcdc_load_device-m += ../rowboat-android/kernel/arch/arm/mach-omap2/omap_hwmod.o
ccflags-m := -I$(src)/../rowboat-android/kernel/arch/arm/plat-omap/include/plat/
KDIR := /home/userid/rowboat-android/kernel/
PWD := $(shell pwd)
default:
$(MAKE) ARCH=arm CROSS_COMPILE=/home/userid/rowboat-android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
I am suspecting that the makefile is finding the header file for "omap_device_build" and "omap_hwmod_lookup" functions.
Appreciate your help, and thank you advance.
Thank you to mbratch and artless noise for their comments
To address the WARNINGS, the symbols need to be exported. For the above question place
EXPORT_SYMBOL(omap_device_build) in omap_device.c
EXPORT_SYMBOL(omap_hwmod_lookup) in omap_hwmod.c
and compile the kernel. Then compile the loadable kernel module against the compiled kernel. Perform the following to verify if the symbols have been exported
grep omap_device_build /proc/kallsyms
or
grep omap_device_build Module.symvers
For more details reference the following links
Kernel Symbols: What's available to Your Module
Building and Running Modules
Comments are welcome
Thank you
I've just started learning linux kernel modules and trying to write simple Hello world program.
So mymod.c:
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Author");
MODULE_DESCRIPTION("\"Hello, world!\" minimal module");
MODULE_VERSION("printk");
int init_module(void)
{
printk("<1>Hello world 1.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world 1.\n");
}
Makefile:
obj-m += mymod.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make outout:
make -C /lib/modules/3.2.0-23-generic-pae/build M=/root modules
make[1]: Entering directory `/usr/src/linux-3.2.42'
WARNING: Symbol version dump /usr/src/linux-3.2.42/Module.symvers
is missing; modules will have no dependencies and modversions.
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-3.2.42'
So it creates files I needed, but when I try to install this by
insmod mymod.ko
I get next output:
insmod: error inserting 'mymod.ko': -1 Invalid module format
So I'd like to know what's the problem?
PS. OS - Ubuntu Server 12.04. Kernel - linux 3.2.0-23 pae
UPDATE:
I've downloaded from kernel.org kernel 3.2.42 and put it in /usr/src and did 'make defconfig && make prepare', 'make modules_prepare'. Also I've created link in /lib/modules/3.2.0-23-generic-pae/build.
Is this the source tree for the running kernel? If not, it should fail.
Install the kernel-devel (or similarly named) package for your distribution, it adds enough machinery to build modules against it.
You missed the module_init and module_cleanup declaration,
module_init (module_init);
module_exit (cleanup_module);
Otherwise it would have no entry point defined, and it wouldn't load.
Because this task requires so many details, and small files to be coordinated it is best to use UML (user mode linux) so that the kprintf (kernel printf) always outputs to the terminal even in a graphical environment.
I am trying to compile a hello world module given over here
I have followed the following step.
Downloaded Linux kernel 2.6.35-rc5
extracted to directory /general/source/linux
Complied the entire kernel.
created a dir test in the linux folder.
Created and complied a hello world module as mentioned there.
when I run the insmod command, I get this error
insmod: error inserting 'hello.ko': -1 Invalid module format
How do I sort out this error?
Regards,
Ok the mistake that you are making is the kernel version.
First try
uname -r
You would get the kernel version. The downloaded version mostly likely won't be the kernel version of your system.
So change the make file to
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
.PHONY: build clean
build:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c
else
$(info Building with KERNELRELEASE = ${KERNELRELEASE})
obj-m := hello.o
endif
Make sure the tabs are in the order as mentioned in the above script.
Your kernel module must match the running kernel. If you want to install this specific module, for example, you'd need to also install the kernel that you've built.
Normally, you'd not build the kernel on your own and use a pre-built version that matches your distribution's kernel. Look for a kernel-headers package in your distribution's repository.
I made certain modifications in hid module.
I can make it and load (insmod) it on kernel v 2.6.27.14 sucessfully
Now I am willing to load the same kernel module on kernel v 2.6.27.11
As there is no differance in the kerbel source files for both the kernel versions
I can make it sucessfully, but I cannot add / insmod in this .11 kernel version
**
ERROR: Module myhid does not exist in /proc/modules
insmod: error inserting 'myhid.ko': -1 Invalid module format
**
Regards,
You can't load a module compiled for another kernel version. That the source code of the module did not change does not mean that the binary will be the same for another kernel version. Any interface change of kernel internal APIs (even when not directly visible) will break the module...
Therefore, the kernel stays on the safe side by disallowing loading of modules that were built for another kernel version. Alternatively, you can set the MODVERSIONS configuration option when building your kernel. This will embed version information into all symbols used by your module and with luck you can load it on another kernel version.
If any interface used by your module changed, the result will be the same though.
see what "modinfo" tells you about your module:
Check that it's compiled properly, linked to the right kernel.
$ modinfo hid
filename: /lib/modules/2.6.27.7/kernel/drivers/hid/hid.ko
license: GPL
depends:
vermagic: 2.6.27.7 mod_unload 486
parm: pb_fnmode:Mode of fn key on Apple keyboards (0 = disabled, 1 = fkeyslast, 2 = fkeysfirst) (int)
When you compile/install modules, don't forget that you have to run "depmod" (as root) to rebuild the modules dependancies before running insmod/modprobe.
Thanks ! Here is the make file . I dowload all the dependent source files for HID.O and rename them
MAKEFILE
obj-m := myhid.o
myhid-objs := my-hiddraw.o my-hid-core.o my-hid-input.o my-hid-input-quirk.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules