forked from aakbar5/handy-kernel_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_mutex.c
143 lines (115 loc) · 3.8 KB
/
lock_mutex.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
// An example of mutex
#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/sched.h>
#include <linux/mutex.h>
// Structure having module related data
struct lockmod_info {
struct task_struct *read_task;
struct task_struct *write_task;
unsigned int msecs_read;
unsigned int msecs_write;
u64 variable; /* variable to be protected via lock */
struct mutex lock;
} *g_lockmod_info;
// Callback to perform work in the context of kthread
static int lockmod_read_callback(void *data) {
struct lockmod_info *info = (struct lockmod_info *)data;
pr_info("lockmod_read_callback -- START\n");
while (kthread_should_stop() == false) {
mutex_lock(&info->lock);
{
pr_info("lockmod_read_callback -- Doing working (%lld)\n", g_lockmod_info->variable);
msleep_interruptible(info->msecs_read);
}
mutex_unlock(&info->lock);
}
pr_info("lockmod_read_callback -- END\n");
return 0;
}
// Callback to perform work in the context of kthread
static int lockmod_write_callback(void *data) {
struct lockmod_info *info = (struct lockmod_info *)data;
pr_info("lockmod_write_callback -- START\n");
while (kthread_should_stop() == false) {
mutex_lock(&info->lock);
{
++g_lockmod_info->variable;
// pr_info("lockmod_write_callback -- Doing working (%lld)\n", g_lockmod_info->variable);
msleep_interruptible(info->msecs_write);
}
mutex_unlock(&info->lock);
}
pr_info("lockmod_write_callback -- END\n");
return 0;
}
//
// Module entry point
//
static int __init lockmod_init(void) {
int ret;
pr_info("lockmod: init\n");
g_lockmod_info = kmalloc(sizeof(struct lockmod_info), GFP_KERNEL);
if (!g_lockmod_info) {
pr_err("Fail to allocate memory\n");
return -ENOMEM;
}
g_lockmod_info->variable = 0;
g_lockmod_info->msecs_read = 1000 * 2;
g_lockmod_info->msecs_write = 1000 * 1;
pr_info("Setup mutex...\n");
mutex_init(&g_lockmod_info->lock);
pr_info("Setup kthread...\n");
g_lockmod_info->read_task = kthread_create(lockmod_read_callback, g_lockmod_info, "read_thread");
if (IS_ERR(g_lockmod_info->read_task)) {
pr_err("Fail to create kthread\n");
ret = PTR_ERR(g_lockmod_info->read_task);
goto err_free_mem;
}
g_lockmod_info->write_task = kthread_create(lockmod_write_callback, g_lockmod_info, "write_thread");
if (IS_ERR(g_lockmod_info->write_task)) {
pr_err("Fail to create kthread\n");
ret = PTR_ERR(g_lockmod_info->write_task);
goto err_free_mem;
}
// Above created kthread is not running yet
pr_info("Start kthreads...\n");
ret = wake_up_process(g_lockmod_info->read_task);
pr_err(" - Read task: # %d\n", ret); // 1 means kthread is started; 0 means already running
ret = wake_up_process(g_lockmod_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_lockmod_info);
g_lockmod_info = NULL;
return ret;
}
//
// Module exit point
//
static void __exit lockmod_exit(void) {
pr_info("lockmod: exit\n");
if (!g_lockmod_info)
return;
kthread_stop(g_lockmod_info->write_task);
kthread_stop(g_lockmod_info->read_task);
mutex_destroy(&g_lockmod_info->lock);
kfree(g_lockmod_info);
g_lockmod_info = NULL;
}
//
// Setup module entry and exit points
//
module_init(lockmod_init);
module_exit(lockmod_exit);
//
// Setup module info
//
MODULE_VERSION("0.0.1");
MODULE_DESCRIPTION("lockmod: mutex example");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("aakbar5 <[email protected]>");