1
votes

Here my basic kernel module code.

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

MODULE_LICENSE("GPL");
static int test_bug_init(void)
{
    printk(KERN_INFO"%s: In init\n", __func__);
    BUG();
    return 0;
}

static void test_bug_exit(void)
{
    printk(KERN_INFO"%s: In exit\n", __func__);
}

module_init(test_bug_init);
module_exit(test_bug_exit);

When I load this module it was successfully loaded but, while unloading time gets the message like "Module in use".

So, why we can't unload the module after BUG() call? Is there another way to unload the module?

1

1 Answers

3
votes

In kernel sources you can see that BUG() code eventually invokes unreachable() macro:

# define unreachable() do { } while (1)

Hence your init function test_bug_init() is in use because of infinite loop in it - it cannot return. Verify this by adding something like

//...
BUG();
printk(KERN_INFO "%s: After BUG()\n", __func__);

So you won't see this print in log.

Read also: BUG() FAQ


Is there another way to unload the module?

You can't unload it, because it is 'in use' and you can't make it not in use somehow (can't stop using it). Just reboot.