schedule.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Let's block the entire kernel! Yay!
  3. kthreads only allow interrupting if you call schedule.
  4. If you don't, they just run forever, and you have to kill the VM.
  5. Sleep functions like usleep_range also end up calling schedule.
  6. Test with:
  7. dmesg -n 1
  8. insmod /schedule.ko yn=[01]
  9. dmesg | tail
  10. Then:
  11. - yn=0:
  12. - `qemu -smp 1`: everything blocks!
  13. - `qemu -smp 2`: you can still use the board, but is it noticeably slow
  14. - yn=1: all good
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/kthread.h>
  18. #include <linux/module.h>
  19. #include <uapi/linux/stat.h> /* S_IRUSR | S_IWUSR */
  20. static int yn = 1;
  21. module_param(yn, int, S_IRUSR | S_IWUSR);
  22. MODULE_PARM_DESC(yn, "A short integer");
  23. static struct task_struct *kthread;
  24. static int work_func(void *data)
  25. {
  26. unsigned int i = 0;
  27. while (!kthread_should_stop()) {
  28. pr_info("%u\n", i);
  29. i++;
  30. if (yn)
  31. schedule();
  32. }
  33. return 0;
  34. }
  35. static int myinit(void)
  36. {
  37. kthread = kthread_create(work_func, NULL, "mykthread");
  38. wake_up_process(kthread);
  39. return 0;
  40. }
  41. static void myexit(void)
  42. {
  43. kthread_stop(kthread);
  44. }
  45. module_init(myinit)
  46. module_exit(myexit)
  47. MODULE_LICENSE("GPL");