Skip to content

Commit f59ca5a

Browse files
committed
timer: move doc to README
1 parent 7d16a5c commit f59ca5a

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

README.adoc

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,11 +3958,7 @@ Bibliography:
39583958
* https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module
39593959
* https://en.wikipedia.org/wiki/Netlink
39603960

3961-
=== Linux kernel asynchronous APIs
3962-
3963-
In this section we will document asynchronous APIs of Linux kernel, especially kthread-related scheduled events.
3964-
3965-
==== kthread
3961+
=== kthread
39663962

39673963
Kernel threads are managed exactly like userland threads; they also have a backing `task_struct`, and are scheduled with the same mechanism:
39683964

@@ -4000,7 +3996,7 @@ Bibliography:
40003996
* http://stackoverflow.com/questions/10177641/proper-way-of-handling-threads-in-kernel
40013997
* http://stackoverflow.com/questions/4084708/how-to-wait-for-a-linux-kernel-thread-kthreadto-exit
40023998

4003-
===== kthreads
3999+
==== kthreads
40044000

40054001
Let's launch two threads and see if they actually run in parallel:
40064002

@@ -4034,7 +4030,7 @@ Possible very likely outcome:
40344030

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

4037-
===== sleep
4033+
==== sleep
40384034

40394035
Count to dmesg every one second from `0` up to `n - 1`:
40404036

@@ -4044,14 +4040,14 @@ insmod /sleep.ko n=5
40444040

40454041
Source: link:kernel_module/sleep.c[]
40464042

4047-
The sleep is done with a call to `usleep_range` directly inside `module_init` for simplicity.
4043+
The sleep is done with a call to link:https://github.com/torvalds/linux/blob/v4.17/kernel/time/timer.c#L1984[`usleep_range`] directly inside `module_init` for simplicity.
40484044

40494045
Bibliography:
40504046

40514047
* https://stackoverflow.com/questions/15994603/how-to-sleep-in-the-linux-kernel/44153288#44153288
40524048
* https://github.com/torvalds/linux/blob/v4.17/Documentation/timers/timers-howto.txt
40534049

4054-
===== Workqueue
4050+
==== Workqueues
40554051

40564052
A more convenient front-end for <<kthread>>:
40574053

@@ -4089,11 +4085,11 @@ Stop:
40894085
rmmod work_from_work
40904086
....
40914087

4092-
The sleep is done indirectly through: `queue_delayed_work`, which waits the specified time before scheduling the work.
4088+
The sleep is done indirectly through: link:https://github.com/torvalds/linux/blob/v4.17/include/linux/workqueue.h#L522[`queue_delayed_work`], which waits the specified time before scheduling the work.
40934089

40944090
Source: link:kernel_module/work_from_work.c[]
40954091

4096-
===== schedule
4092+
==== schedule
40974093

40984094
Let's block the entire kernel! Yay:
40994095

@@ -4128,6 +4124,31 @@ The system also responds if we <<number-of-cores,add another core>>:
41284124
./run -c 2 -F 'dmesg -n 1;insmod /schedule.ko schedule=0'
41294125
....
41304126

4127+
=== Timers
4128+
4129+
Count from `0` to `9` infinitely many times in 1 second intervals using timers:
4130+
4131+
....
4132+
insmod /timer.ko
4133+
....
4134+
4135+
Stop counting:
4136+
4137+
....
4138+
rmmod timer
4139+
....
4140+
4141+
Source: link:kernel_module/timer.c[]
4142+
4143+
Timers are callbacks that run when an interrupt happens, from the interrupt context itself.
4144+
4145+
Therefore they produce more accurate timing than thread scheduling, which is more complex, but you can't do too much work inside of them.
4146+
4147+
Bibliography:
4148+
4149+
* http://stackoverflow.com/questions/10812858/timers-in-linux-device-drivers
4150+
* https://gist.github.com/yagihiro/310149
4151+
41314152
=== IRQ
41324153

41334154
==== irq.ko

kernel_module/README.adoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
https://github.com/cirosantilli/linux-kernel-module-cheat#directory-structure
2-
3-
. Asynchronous
4-
.. link:timer.c[]

kernel_module/timer.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
/*
2-
Print the jiffies every second.
3-
4-
Timers are callbacks that run when an interrupt happens, from the interrupt context itself.
5-
6-
Therefore they produce more accurate timing than thread scheduling, which is more complex,
7-
but you can't do too much work inside of them.
8-
9-
See also:
10-
11-
- http://stackoverflow.com/questions/10812858/timers-in-linux-device-drivers
12-
- https://gist.github.com/yagihiro/310149
13-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#timers */
142

153
#include <linux/jiffies.h>
164
#include <linux/kernel.h>
175
#include <linux/module.h>
186
#include <linux/timer.h>
197

8+
static int i;
209
/* We would normally mark this as static and give it a more generic name.
2110
* But let's do it like this this time for the sake of our GDB kernel module step debugging example. */
2211
void lkmc_timer_callback(struct timer_list *data);
@@ -26,7 +15,10 @@ DEFINE_TIMER(mytimer, lkmc_timer_callback);
2615

2716
void lkmc_timer_callback(struct timer_list *data)
2817
{
29-
pr_info("%u\n", (unsigned)jiffies);
18+
pr_info("%d\n", i);
19+
i++;
20+
if (i == 10)
21+
i = 0;
3022
mod_timer(&mytimer, jiffies + onesec);
3123
}
3224

kernel_module/workqueue_cheat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* https://github.com/cirosantilli/linux-kernel-module-cheat#workqueue */
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#workqueues */
22

33
#include <linux/delay.h> /* usleep_range */
44
#include <linux/kernel.h>

0 commit comments

Comments
 (0)