LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2018, 02:37 AM   #1
abhinav007
LQ Newbie
 
Registered: Feb 2018
Posts: 10

Rep: Reputation: Disabled
Kernel module unexpected exit when using insmod


Hi,
I have written a simple kernel driver program. It has simple module entry and exit functions.
Code:
static int __init driver_entry(void){
 printk(KERN_INFO "module entry");
 return 0;
}
static void __exit driver_exit(void){
 printk(KERN_ALERT "module exited");
} 
module_init(driver_entry);
module_exit(driver_exit);
I created the standard makefile required for compiling module as follows:
Code:
CONFIG_MODULE_SIG=n
obj-m := testmod.o

KERNEL_DIR = /usr/src/linux-headers-$(shell uname -r)

all:
     $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
     rm -rf *.o *.ko *.mod.* *.symvers *.order *~
The module compiles without error.I previously received a module verification error due to signature while insmod hence added the CONFIG_MODULE_SIG=n.
I am using the virtualbox environment running Ubuntu 16.04. However, when I insert the module using insmod, the "module exited" message appears highlighted with red background in dmesg log even before the "module entry" message. The function driver_exit gets executed automatically with the insmod call. Not able to understand this phenomenon?

Thank you.
 
Old 03-07-2018, 11:39 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,137

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Is that the previous copy of your driver being removed when you insmod a new one? Did you check with lsmod?
 
Old 03-08-2018, 12:06 AM   #3
abhinav007
LQ Newbie
 
Registered: Feb 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
Details

@smallpond, I have checked with lsmod and the module was not loaded before insmod was used. I changed the Makefile to compile with /lib/modules/$(shell uname -r)/build instead of /usr/src/linux-headers-$(shell uname -r).I am posting the full code below:

module1.c
Code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/semaphore.h>
#include <asm/uaccess.h>
#include <linux/init.h>

static int __init driver_entry(void)
{
	printk(KERN_ALERT "module1: module entered");
	return 0;
}
static void __exit driver_exit(void)
{
	printk(KERN_ALERT "module1: module exit");
}
module_init(driver_entry);
module_exit(driver_exit);

MODULE_AUTHOR("Abhi");
MODULE_DESCRIPTION("Simple driver");
MODULE_LICENSE("GPL");
Makefile:
Code:
CONFIG_MODULE_SIG=n
obj-m := module1.o

KERNEL_DIR = /lib/modules/$(shell uname -r)/build

all:
	$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules

clean:
	$(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
The module throws compile error if I try to compile with "sudo make", but compiles successfully without the use of "sudo". I am attaching the log below:
Code:
abhinav@abhinav-VirtualBox:~/tuts/ch1$ ls
Makefile  module1.c

abhinav@abhinav-VirtualBox:~/tuts/ch1$ dmesg

abhinav@abhinav-VirtualBox:~/tuts/ch1$ make 
make -C /lib/modules/4.13.0-36-generic/build M=/home/abhinav/tuts/ch1 modules
make[1]: Entering directory '/usr/src/linux-headers-4.13.0-36-generic'
  CC [M]  /home/abhinav/tuts/ch1/module1.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/abhinav/tuts/ch1/module1.mod.o
  LD [M]  /home/abhinav/tuts/ch1/module1.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.13.0-36-generic'

abhinav@abhinav-VirtualBox:~/tuts/ch1$ sudo insmod module1.ko

abhinav@abhinav-VirtualBox:~/tuts/ch1$ dmesg
[ 1420.222942] module1: module exit

abhinav@abhinav-VirtualBox:~/tuts/ch1$ lsmod
Module                  Size  Used by
module1                16384  0
snd_intel8x0           40960  2
snd_ac97_codec        131072  1 snd_intel8x0
intel_powerclamp       16384  0
ac97_bus               16384  1 snd_ac97_codec
snd_pcm                98304  2 snd_ac97_codec,snd_intel8x0
crct10dif_pclmul       16384  0
crc32_pclmul           16384  0
ghash_clmulni_intel    16384  0
cryptd                 24576  1 ghash_clmulni_intel
joydev                 20480  0
intel_rapl_perf        16384  0
snd_seq_midi           16384  0
input_leds             16384  0
snd_seq_midi_event     16384  1 snd_seq_midi
snd_rawmidi            32768  1 snd_seq_midi
snd_seq                65536  2 snd_seq_midi_event,snd_seq_midi
serio_raw              16384  0
snd_seq_device         16384  3 snd_seq,snd_rawmidi,snd_seq_midi
vboxvideo              53248  2
ttm                    94208  1 vboxvideo
drm_kms_helper        167936  1 vboxvideo
snd_timer              32768  2 snd_seq,snd_pcm
drm                   360448  5 vboxvideo,ttm,drm_kms_helper
fb_sys_fops            16384  1 drm_kms_helper
syscopyarea            16384  2 vboxvideo,drm_kms_helper
snd                    81920  11 snd_seq,snd_ac97_codec,snd_timer,snd_rawmidi,snd_intel8x0,snd_seq_device,snd_pcm
vboxguest             294912  1 vboxvideo
mac_hid                16384  0
sysfillrect            16384  2 vboxvideo,drm_kms_helper
i2c_piix4              24576  0
sysimgblt              16384  2 vboxvideo,drm_kms_helper
soundcore              16384  1 snd
parport_pc             32768  0
ppdev                  20480  0
lp                     20480  0
parport                49152  3 lp,parport_pc,ppdev
autofs4                40960  2
hid_generic            16384  0
usbhid                 49152  0
hid                   118784  2 hid_generic,usbhid
ahci                   36864  2
e1000                 143360  0
psmouse               147456  0
libahci                32768  1 ahci
pata_acpi              16384  0
video                  40960  0

abhinav@abhinav-VirtualBox:~/tuts/ch1$ sudo rmmod module1.ko

abhinav@abhinav-VirtualBox:~/tuts/ch1$ dmesg
[ 1420.222942] module1: module exit
[ 2283.046381] module1: module entered

abhinav@abhinav-VirtualBox:~/tuts/ch1$ lsmod
Module                  Size  Used by
snd_intel8x0           40960  2
snd_ac97_codec        131072  1 snd_intel8x0
intel_powerclamp       16384  0
ac97_bus               16384  1 snd_ac97_codec
snd_pcm                98304  2 snd_ac97_codec,snd_intel8x0
crct10dif_pclmul       16384  0
crc32_pclmul           16384  0
ghash_clmulni_intel    16384  0
cryptd                 24576  1 ghash_clmulni_intel
joydev                 20480  0
intel_rapl_perf        16384  0
snd_seq_midi           16384  0
input_leds             16384  0
snd_seq_midi_event     16384  1 snd_seq_midi
snd_rawmidi            32768  1 snd_seq_midi
snd_seq                65536  2 snd_seq_midi_event,snd_seq_midi
serio_raw              16384  0
snd_seq_device         16384  3 snd_seq,snd_rawmidi,snd_seq_midi
vboxvideo              53248  2
ttm                    94208  1 vboxvideo
drm_kms_helper        167936  1 vboxvideo
snd_timer              32768  2 snd_seq,snd_pcm
drm                   360448  5 vboxvideo,ttm,drm_kms_helper
fb_sys_fops            16384  1 drm_kms_helper
syscopyarea            16384  2 vboxvideo,drm_kms_helper
snd                    81920  11 snd_seq,snd_ac97_codec,snd_timer,snd_rawmidi,snd_intel8x0,snd_seq_device,snd_pcm
vboxguest             294912  1 vboxvideo
mac_hid                16384  0
sysfillrect            16384  2 vboxvideo,drm_kms_helper
i2c_piix4              24576  0
sysimgblt              16384  2 vboxvideo,drm_kms_helper
soundcore              16384  1 snd
parport_pc             32768  0
ppdev                  20480  0
lp                     20480  0
parport                49152  3 lp,parport_pc,ppdev
autofs4                40960  2
hid_generic            16384  0
usbhid                 49152  0
hid                   118784  2 hid_generic,usbhid
ahci                   36864  2
e1000                 143360  0
psmouse               147456  0
libahci                32768  1 ahci
pata_acpi              16384  0
video                  40960  0
Can clearly verify that module is loaded and unloaded by using lsmod, but not sure what is actually going behind the execution. It seems driver_exit function is triggered as soon as the module is instatiated by insmod. Could it be still the module verification error due to missing signature?
 
Old 03-08-2018, 11:13 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Is there some compelling reason you are using insmod instead of modprobe?
 
Old 03-09-2018, 12:46 AM   #5
abhinav007
LQ Newbie
 
Registered: Feb 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
Is there some compelling reason you are using insmod instead of modprobe?
Seems there a lot of work needs to be done with modprobe. It demands the module to be present in /lib/usr/$(shell uname -r) directory. Also I tried modules_install but it throws error that
Code:
cp: cannot stat './modules.order':No such file or directory
Also I found many methods to compile and load LKM, it has become slightly confusing
 
Old 03-09-2018, 01:27 AM   #6
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Modprobe is more trouble, but it also makes more checks, and it doesn't mysteriously fail like insmod.
 
Old 03-09-2018, 03:45 AM   #7
abhinav007
LQ Newbie
 
Registered: Feb 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
Modprobe is more trouble, but it also makes more checks, and it doesn't mysteriously fail like insmod.
I used modprobe this time by making a symbolic link and depmod. No change in the output though.
It seems that the driver_entry and driver_exit are behaving exactly opposite than intended. driver_exit is executed when the module is instantiated and driver_entry is executed when rmmod is used. The message indicating module is out-of-tree build and module verification failure appears initially every time I restart the system and load the module for the first time (even though CONFIG_MODULE_SIG=n in makefile). I also checked that CONFIG_MODULE_SIG_FORCE is not enabled.

Could it be that dmesg log is lagging behind or something similar?

Last edited by abhinav007; 03-11-2018 at 10:45 PM.
 
Old 03-12-2018, 08:46 AM   #8
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Try tailing /var/log/messages and watch it as you try to load the module. It should give some clue. You could also enable kernel debugging.
 
Old 03-21-2018, 11:38 AM   #9
abhinav007
LQ Newbie
 
Registered: Feb 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
Solution

Found the solution. It was the most stupid thing I could imagine, just added "\n" at the end of printk message. Guess the module was working properly but dmesg didn't show correct log until I added a newline at the end of the KERN_INFO & KERN_ALERT message.
But still wonder how this issue prompted "module exited" before "module entry" ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
kernel module failure for insmod gnaray2 Linux - Software 1 06-09-2016 12:29 AM
Invalid module format when insmod the kernel module georgewhr Linux - Software 1 10-20-2013 11:41 AM
[SOLVED] kernel module insmod problem HGaurav Programming 1 08-03-2009 08:13 AM
kernel module ( hello word ) insmod fail ptlchetan Programming 4 10-13-2006 04:32 AM
How/Where To Make *.ko file so I can insmod module into Kernel? pat_and_cami Programming 1 04-09-2006 03:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration