Skip to content

Commit 0ccbc04

Browse files
committed
move schedule() doc to README
1 parent 9c62b82 commit 0ccbc04

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

README.adoc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,6 +4032,41 @@ Possible very likely outcome:
40324032

40334033
The threads almost always interleaved nicely, thus confirming that they are actually running in parallel.
40344034

4035+
===== schedule
4036+
4037+
Let's block the entire kernel! Yay:
4038+
4039+
.....
4040+
./run -F 'dmesg -n 1;insmod /schedule.ko schedule=0'
4041+
.....
4042+
4043+
Outcome: the system hangs, the only way out is to kill the VM.
4044+
4045+
Source: link:kernel_module/schedule.c[]
4046+
4047+
kthreads only allow interrupting if you call `schedule()`, and the `schedule=0` <<kernel-module-parameters,kernel module parameter>> turns it off.
4048+
4049+
Sleep functions like `usleep_range` also end up calling schedule.
4050+
4051+
If we allow `schedule()` to be called, then the system becomes responsive:
4052+
4053+
.....
4054+
./run -F 'dmesg -n 1;insmod /schedule.ko schedule=1'
4055+
.....
4056+
4057+
4058+
and we can observe the counting with:
4059+
4060+
....
4061+
dmesg -w
4062+
....
4063+
4064+
The system also responds if we <<number-of-cores,add another core>>:
4065+
4066+
....
4067+
./run -c 2 -F 'dmesg -n 1;insmod /schedule.ko schedule=0'
4068+
....
4069+
40354070
=== IRQ
40364071

40374072
==== irq.ko

kernel_module/README.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
https://github.com/cirosantilli/linux-kernel-module-cheat#directory-structure
22

33
. Asynchronous
4-
.. link:irq.c[]
5-
.. link:schedule.c[]
64
.. link:sleep.c[]
75
.. link:timer.c[]
86
.. link:work_from_work.c[]

kernel_module/schedule.c

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
1-
/*
2-
Let's block the entire kernel! Yay!
3-
4-
kthreads only allow interrupting if you call schedule.
5-
6-
If you don't, they just run forever, and you have to kill the VM.
7-
8-
Sleep functions like usleep_range also end up calling schedule.
9-
10-
Test with:
11-
12-
dmesg -n 1
13-
insmod /schedule.ko yn=[01]
14-
dmesg | tail
15-
16-
Then:
17-
18-
- yn=0:
19-
- `qemu -smp 1`: everything blocks!
20-
- `qemu -smp 2`: you can still use the board, but is it noticeably slow
21-
- yn=1: all good
22-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#schedule */
232

243
#include <linux/kernel.h>
254
#include <linux/kthread.h>
265
#include <linux/module.h>
276
#include <uapi/linux/stat.h> /* S_IRUSR | S_IWUSR */
287

29-
static int yn = 1;
30-
module_param(yn, int, S_IRUSR | S_IWUSR);
31-
MODULE_PARM_DESC(yn, "A short integer");
8+
static int do_schedule = 1;
9+
module_param_named(schedule, do_schedule, int, S_IRUSR | S_IWUSR);
3210

3311
static struct task_struct *kthread;
3412

@@ -38,7 +16,7 @@ static int work_func(void *data)
3816
while (!kthread_should_stop()) {
3917
pr_info("%u\n", i);
4018
i++;
41-
if (yn)
19+
if (do_schedule)
4220
schedule();
4321
}
4422
return 0;

0 commit comments

Comments
 (0)