0

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
  • 1
    Please, read compiler messages - do NOT define `__KERNEL__` and `MODULE`. – Alexander Dzyoba Feb 19 '14 at 12:07
  • agreed, but then how compiler will identify that this is kernel code and also not permanent part of kernel (i.e. Loadable Kernel Module)? – Prafull Barpute Feb 19 '14 at 13:19
  • Any highlights on my first question (rmmod failed)? – Prafull Barpute Feb 19 '14 at 13:21
  • 1
    That's a job for kbuild to deal with compiler. – Alexander Dzyoba Feb 19 '14 at 13:22
  • The only thing that could cause `rmmod` to fail such trivial code is that flags. – Alexander Dzyoba Feb 19 '14 at 13:23
  • I tried removing __KERNEL__ and MODULE also but no any sucess – Prafull Barpute Feb 19 '14 at 13:24
  • Problem may be related to toolchain. Look here http://stackoverflow.com/questions/7482469/why-is-this-kernel-module-marked-at-permanent-on-2-6-39 And here http://lists.kernelnewbies.org/pipermail/kernelnewbies/2011-April/001498.html – Alexander Dzyoba Feb 19 '14 at 13:29
  • How do you compile this module? – CL. Feb 19 '14 at 15:18
  • @avd, 2nd URL I already tried & it is not working even with setting flag USE_IMMEDIATE. I will check other possibility i.e. tool-chain. & share results. – Prafull Barpute Feb 20 '14 at 05:17
  • This problem is solved. Yes it is problem related to toolchain. I downloaded .deb files to install kernel from url "http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.4-precise/". But I was no sure about the toolchain used to generate these .deb files. So I finally download linux kernel source from "https://www.kernel.org/"->compile & install on my laptop & then compile my module. Problem disappeared. Thanks @avd for providing me valuable clue. – Prafull Barpute Feb 24 '14 at 09:59

2 Answers2

0

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.

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
0

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.

user55111
  • 322
  • 1
  • 3
  • 12