Score:0

Kernel Module exit message get printed next time when inserting the module

co flag

I have written simple kernel module to print Hello kernel msg.

my_module.c

#include <linux/module.h>
#include <linux/init.h>

/* Meta Information */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("BHAGWAT");
MODULE_DESCRIPTION("A HELLO WORLD MODULE");

/*this function is called when module is loaded into the kernel*/

static int __init ModuleInit(void)
{
    printk("Hello, kernel!\n");
    return 0;
}

/*this function is called when module is removed from kernel*/
static void __exit ModuleExit(void)
{
    printk("Goodbye, kernel");
}

module_init(ModuleInit);
module_exit(ModuleExit);

Makefile

obj-m += my_module.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

when I insert the module I get msg printed in kernel logs, but when I remove the module msg does not get printed. When I again insert the module, two msg get printed one for last module exit and other for module load. Why module exit msg get printed later when i insert the module but not when removed the module.

bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ sudo insmod my_module.ko
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ dmesg | tail
[   24.595403] rfkill: input handler enabled
[   30.244336] rfkill: input handler disabled
[  145.426767] my_module: module verification failed: signature and/or required key missing - tainting kernel
[  145.427022] Hello, kernel!
[  217.026559] Goodbye, kernel
[  498.489388] Hello, kernel!
[  524.139613] Goodbye, kernel
[  528.270128] Hello, kernel!
[  577.360611] Goodbye, kernel
[  587.700237] Hello, kernel!
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ sudo rmmod my_module 
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ dmesg | tail
[   24.595403] rfkill: input handler enabled
[   30.244336] rfkill: input handler disabled
[  145.426767] my_module: module verification failed: signature and/or required key missing - tainting kernel
[  145.427022] Hello, kernel!
[  217.026559] Goodbye, kernel
[  498.489388] Hello, kernel!
[  524.139613] Goodbye, kernel
[  528.270128] Hello, kernel!
[  577.360611] Goodbye, kernel
[  587.700237] Hello, kernel!
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ sudo insmod my_module.ko
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ dmesg | tail
[  145.426767] my_module: module verification failed: signature and/or required key missing - tainting kernel
[  145.427022] Hello, kernel!
[  217.026559] Goodbye, kernel
[  498.489388] Hello, kernel!
[  524.139613] Goodbye, kernel
[  528.270128] Hello, kernel!
[  577.360611] Goodbye, kernel
[  587.700237] Hello, kernel!
[  667.900373] Goodbye, kernel
[  676.245356] Hello, kernel!
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ sudo rmmod my_module 
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ dmesg | tail
[  145.426767] my_module: module verification failed: signature and/or required key missing - tainting kernel
[  145.427022] Hello, kernel!
[  217.026559] Goodbye, kernel
[  498.489388] Hello, kernel!
[  524.139613] Goodbye, kernel
[  528.270128] Hello, kernel!
[  577.360611] Goodbye, kernel
[  587.700237] Hello, kernel!
[  667.900373] Goodbye, kernel
[  676.245356] Hello, kernel!
hr flag
Perhaps because there is no newline at the end of your `"Goodbye, kernel"` message? See for example [printk is line buffered?](https://stackoverflow.com/questions/13538618/printk-is-line-buffered)
Jeff avatar
mx flag
What kernel version are you using? Presumably it's 5.4, right?
Score:0
in flag

use '\n' in your printk statement of exit module printk("Goodbye, kernel\n");

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.