-
Notifications
You must be signed in to change notification settings - Fork 5
/
lock_seqlock.c
167 lines (133 loc) · 4.8 KB
/
lock_seqlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// An example of seqlock
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/seqlock.h>
// Structure having module related data
struct mod_info {
struct task_struct *read_task_1;
struct task_struct *read_task_2;
struct task_struct *write_task;
unsigned int msecs_read;
unsigned int msecs_write;
u64 variable; /* variable to be protected via lock */
seqlock_t lock;
} *g_mod_info;
// Callback to perform work in the context of kthread
static int mod_read_callback(void *data) {
struct mod_info *info = (struct mod_info *)data;
/* kthread has associated struct task_struct and kernel's current is set to it */
pr_info("mod_read_callback (%d -- %s) -- START\n", current->pid, current->comm);
while (kthread_should_stop() == false) {
unsigned int seq_no;
do
{
seq_no = read_seqbegin(&info->lock);
{
pr_info("mod_read_callback (%d -- %s) -- work (%lld)\n", current->pid, current->comm, g_mod_info->variable);
// NOTE: spin lock disable preemption so can't call sleep
// msleep_interruptible(info->msecs_read);
}
} while (read_seqretry(&info->lock, seq_no));
msleep_interruptible(info->msecs_read);
}
pr_info("mod_read_callback (%d -- %s) -- END\n", current->pid, current->comm);
return 0;
}
// Callback to perform work in the context of kthread
static int mod_write_callback(void *data) {
struct mod_info *info = (struct mod_info *)data;
pr_info("mod_write_callback (%d -- %s) -- START\n", current->pid, current->comm);
while (kthread_should_stop() == false) {
write_seqlock(&info->lock);
{
pr_info("mod_write_callback (%d -- %s) -- work (%lld)\n", current->pid, current->comm, g_mod_info->variable);
++g_mod_info->variable;
// NOTE: spin lock disable preemption so can't call sleep
// msleep_interruptible(info->msecs_write);
}
write_sequnlock(&info->lock);
msleep_interruptible(info->msecs_write);
}
pr_info("mod_write_callback (%d -- %s) -- END\n", current->pid, current->comm);
return 0;
}
//
// Module entry point
//
static int __init mod_init(void) {
int ret;
pr_info("mod: init\n");
g_mod_info = kmalloc(sizeof(struct mod_info), GFP_KERNEL);
if (!g_mod_info) {
pr_err("Fail to allocate memory\n");
return -ENOMEM;
}
g_mod_info->variable = 0;
g_mod_info->msecs_read = 1000 * 2;
g_mod_info->msecs_write = 1000 * 3;
pr_info("Setup seqlock...\n");
seqlock_init(&g_mod_info->lock);
pr_info("Setup kthread (reader # 1)...\n");
g_mod_info->read_task_1 = kthread_create(mod_read_callback, g_mod_info, "read_thread_1");
if (IS_ERR(g_mod_info->read_task_1)) {
pr_err("Fail to create kthread\n");
ret = PTR_ERR(g_mod_info->read_task_1);
goto err_free_mem;
}
pr_info("Setup kthread (reader # 2)...\n");
g_mod_info->read_task_2 = kthread_create(mod_read_callback, g_mod_info, "read_thread_2");
if (IS_ERR(g_mod_info->read_task_2)) {
pr_err("Fail to create kthread\n");
ret = PTR_ERR(g_mod_info->read_task_2);
goto err_free_mem;
}
pr_info("Setup kthread (writer)...\n");
g_mod_info->write_task = kthread_create(mod_write_callback, g_mod_info, "write_thread");
if (IS_ERR(g_mod_info->write_task)) {
pr_err("Fail to create kthread\n");
ret = PTR_ERR(g_mod_info->write_task);
goto err_free_mem;
}
// Above created kthread is not running yet
pr_info("Start kthreads...\n");
ret = wake_up_process(g_mod_info->read_task_1);
pr_err(" - Read task # 1: # %d\n", ret); // 1 means kthread is started; 0 means already running
ret = wake_up_process(g_mod_info->read_task_2);
pr_err(" - Read task # 2: # %d\n", ret); // 1 means kthread is started; 0 means already running
ret = wake_up_process(g_mod_info->write_task);
pr_err(" - Write task: # %d\n", ret); // 1 means kthread is started; 0 means already running
return 0;
err_free_mem:
kfree(g_mod_info);
g_mod_info = NULL;
return ret;
}
//
// Module exit point
//
static void __exit mod_exit(void) {
pr_info("mod: exit\n");
if (!g_mod_info)
return;
kthread_stop(g_mod_info->write_task);
kthread_stop(g_mod_info->read_task_1);
kthread_stop(g_mod_info->read_task_2);
kfree(g_mod_info);
g_mod_info = NULL;
}
//
// Setup module entry and exit points
//
module_init(mod_init);
module_exit(mod_exit);
//
// Setup module info
//
MODULE_VERSION("0.0.1");
MODULE_DESCRIPTION("mod: seqlock example");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("aakbar5 <[email protected]>");