waitqueue example works, kthread_uniterruptible completely wrong and renamed to schedule, params added

This commit is contained in:
Ciro Santilli
2017-06-05 14:53:00 +01:00
parent 0d9fd5b2ca
commit 709d01e513
5 changed files with 125 additions and 55 deletions

58
kernel_module/schedule.c Normal file
View File

@@ -0,0 +1,58 @@
/*
Let's block the entire kernel! Yay!
kthreads only allow interrupting if you call schedule.
If you don't, they just run forever, and you have to kill the VM.
Test with:
dmesg -n 1
insmod /schedule.ko yn=[01]
dmesg | tail
Then:
- yn=0:
- `qemu -smp 1`: everything blocks!
- `qemu -smp 2`: you can still use the board, but is it noticeably slow
- yn=1: all good
*/
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <uapi/linux/stat.h> /* S_IRUSR | S_IWUSR */
MODULE_LICENSE("GPL");
static int yn = 1;
module_param(yn, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(yn, "A short integer");
static struct task_struct *kthread;
static int work_func(void *data)
{
unsigned int i = 0;
while (!kthread_should_stop()) {
pr_info("%u\n", i);
i++;
if (yn)
schedule();
}
return 0;
}
int init_module(void)
{
kthread = kthread_create(work_func, NULL, "mykthread");
wake_up_process(kthread);
return 0;
}
void cleanup_module(void)
{
kthread_stop(kthread);
}