4

I have a program like below.

test_module.c:

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

int init_module(void)
{
        while(1) {
                pr_info("hello 4 sec\n");
                msleep(4 * 1000);
        }
        return 0;
}

void cleanup_module(void)
{
        pr_info("module removed successful\n");
}

When I load this module, my terminal become freeze/blocked. How to stop this program. I tried sudo rmmod test_module, but no use. So I rebooted my system. How to break the init_module? In future, if something goes wrong and if my init_module is not ending then what to do? And what happens if we don't stop init_module?

gangadhars
  • 2,584
  • 7
  • 41
  • 68

1 Answers1

5

By restarting the machine.

That is the only way. User-land processes are isolated and can be terminated forcibly and all that, but in kernel there are no such protections.

The init_module function is not supposed to do anything for extended period of time. It should only register the module to appropriate data structure.

If you need to do something periodically in kernel, you need to either schedule a tasklet or worqueue or create a kernel thread. See also When to use kernel threads vs workqueues in the linux kernel. But most kernel code should really just be registered somewhere and run when appropriate event (either from hardware or from userland via system call) happens.

Community
  • 1
  • 1
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172