-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork.c
More file actions
290 lines (236 loc) · 6.7 KB
/
Copy pathwork.c
File metadata and controls
290 lines (236 loc) · 6.7 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Alpabet Order */
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kref.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/workqueue.h>
#define DRIVER_DESC "Work Queue Test Kernel Module"
struct test_work {
atomic_t index;
int type;
struct kref kref;
struct work_struct w;
struct workqueue_struct *wq;
};
struct test_work *tw;
enum {
TYPE_NONE,
TYPE_WQ, /* create_workqueue() */
TYPE_SINGLE_WQ, /* create_singlethread_workqueue() */
TYPE_ALLOC_WQ, /* alloc_workqueue() */
TYPE_ALLOC_WQ_MAX, /* alloc_workqueue() with max_active */
TYPE_ALLOC_WQ_UNBOUND, /* alloc_workqueue() unbound */
TYPE_ALLOC_WQ_UNBOUND_MAX, /* alloc_workqueue() unbound & max_active */
TYPE_MAX,
};
static int wq_type = 0;
module_param(wq_type, int, 0644);
MODULE_PARM_DESC(wq_type, "create work queue type for test purpose");
/********** debugfs **********/
static struct dentry *dent;
static int type_show(struct seq_file *s, void *unused)
{
struct test_work *tw = s->private;
switch (tw->type) {
case TYPE_WQ:
seq_puts(s, "type: create_workqueue()\n");
break;
case TYPE_SINGLE_WQ:
seq_puts(s, "type: create_singlethread_workqueue()\n");
break;
case TYPE_ALLOC_WQ:
seq_puts(s, "type: alloc_workqueue() with single\n");
break;
case TYPE_ALLOC_WQ_MAX:
seq_puts(s, "type: alloc_workqueue() with max_active(256)\n");
break;
case TYPE_ALLOC_WQ_UNBOUND:
seq_puts(s, "type: alloc_workqueue() with unbound\n");
break;
case TYPE_ALLOC_WQ_UNBOUND_MAX:
seq_puts(s, "type: alloc_workqueue() with unbound & max_active\n");
break;
default:
seq_puts(s, "No value");
break;
}
return 0;
}
static int type_open(struct inode *inode, struct file *file)
{
return single_open(file, type_show, inode->i_private);
}
static ssize_t type_write(struct file *file, const char __user *ubuf,
size_t count, loff_t *ppos)
{
pr_info("%s: called\n", __func__);
/* Not yet implement */
return count;
}
static const struct file_operations type_fops = {
.open = type_open,
.read = seq_read,
.write = type_write,
.llseek = seq_lseek,
.release = single_release,
};
static int start_show(struct seq_file *s, void *unused)
{
struct test_work *tw = s->private;
int i;
bool result;
pr_info("%s: called\n", __func__);
/* Let's put the queue_work here for test */
for (i = 0; i < 10; i++) {
pr_err("%s: (%d)th: Add test_work to the tw's workqueue\n",
__func__, i+1);
result = queue_work(tw->wq, &tw->w);
if (result == false)
pr_err("%s: (%d)th: failed to queue_work\n",
__func__, i+1);
msleep(20);
}
pr_info("%s: done ---->\n", __func__);
return 0;
}
static int start_open(struct inode *inode, struct file *file)
{
return single_open(file, start_show, inode->i_private);
}
static const struct file_operations start_fops = {
.open = start_open,
.release = single_release,
.read = seq_read,
.llseek = seq_lseek,
};
static void work_debugfs_init(struct test_work *tw)
{
struct dentry *file;
dent = debugfs_create_dir("test_work", 0);
if (IS_ERR_OR_NULL(dent)) {
pr_err("%s: failed to debugfs dir create\n", __func__);
return;
}
file = debugfs_create_file("type", 0666, dent, tw, &type_fops);
if (IS_ERR_OR_NULL(file)) {
pr_err("%s: failed to debugfs file(type) create\n", __func__);
debugfs_remove_recursive(dent);
}
file = debugfs_create_file("start", 0644, dent, tw, &start_fops);
if (IS_ERR_OR_NULL(file)) {
pr_err("%s: failed to debugfs file(start) create\n", __func__);
debugfs_remove_recursive(dent);
}
return;
}
static void work_debugfs_remove(void)
{
debugfs_remove_recursive(dent);
}
/********** debugfs **********/
static void work_func(struct work_struct *work)
{
struct test_work *tw;
tw = container_of(work, struct test_work, w);
if (!tw) {
pr_err("%s: Null pointer!!\n", __func__);
return;
}
atomic_inc(&tw->index);
pr_info("%s: (%d)th: start---> ", __func__, atomic_read(&tw->index));
pr_info("%s: (%d)th: step1. 500ms sleep.\n", __func__, atomic_read(&tw->index));
msleep(500);
pr_info("%s: (%d)th: step2. 50ms delay\n", __func__, atomic_read(&tw->index));
mdelay(50);
pr_info("%s: (%d)th: step3. 500ms sleep\n", __func__, atomic_read(&tw->index));
msleep(500);
pr_info("%s: (%d)th: done----> ", __func__, atomic_read(&tw->index));
}
static void work_func_with_busy(struct work_struct *work)
{
struct test_work *tw;
tw = container_of(work, struct test_work, w);
if (!tw) {
pr_err("%s: Null pointer!!\n", __func__);
return;
}
atomic_inc(&tw->index);
pr_info("%s: (%d)th: start---> ", __func__, atomic_read(&tw->index));
pr_info("%s: (%d)th: step2. 50ms delay\n", __func__, atomic_read(&tw->index));
mdelay(50);
pr_info("%s: (%d)th: done----> ", __func__, atomic_read(&tw->index));
}
static void __init_wq(void)
{
switch (wq_type) {
case TYPE_WQ:
pr_info("%s: create_singlethread_workqueue()\n", __func__);
tw->wq = create_singlethread_workqueue("test_wq");
break;
case TYPE_SINGLE_WQ:
pr_info("%s: create_workqueue()\n", __func__);
tw->wq = create_workqueue("test_wq");
break;
case TYPE_ALLOC_WQ:
pr_info("%s: alloc_workqueue() with sigle\n", __func__);
tw->wq = alloc_workqueue("test_wq", WQ_HIGHPRI | WQ_CPU_INTENSIVE, 10);
break;
case TYPE_ALLOC_WQ_MAX:
pr_info("%s: alloc_workqueue() with max_active(256)\n", __func__);
/* max_active is 0, which means set as 256 for max_active. */
tw->wq = alloc_workqueue("test_wq", WQ_HIGHPRI | WQ_CPU_INTENSIVE, 0);
break;
case TYPE_ALLOC_WQ_UNBOUND:
pr_info("%s: alloc_workqueue() with unbound\n", __func__);
tw->wq = alloc_workqueue("test_wq", WQ_HIGHPRI | WQ_UNBOUND, 1);
break;
case TYPE_ALLOC_WQ_UNBOUND_MAX:
pr_info("%s: alloc_workqueue() with unbound & max_active(256)\n", __func__);
tw->wq = alloc_workqueue("test_wq", WQ_HIGHPRI | WQ_UNBOUND, 0);
break;
default:
pr_err("%s:Not yet value\n", __func__);
return;
}
//INIT_WORK(&tw->w, work_func);
INIT_WORK(&tw->w, work_func_with_busy);
}
static void cleanup_wq(struct kref *refcount)
{
struct test_work *tw;
tw = container_of(refcount, struct test_work, kref);
if (tw->wq) {
flush_workqueue(tw->wq);
destroy_workqueue(tw->wq);
}
work_debugfs_remove();
kfree(tw);
}
static int __init init_wq(void)
{
tw = kzalloc(sizeof(struct test_work), GFP_KERNEL);
if (!tw)
return -ENOMEM;
work_debugfs_init(tw);
__init_wq();
atomic_set(&tw->index, 0);
kref_init(&tw->kref);
pr_info("%s: test workqueu module init\n", __func__);
return 0;
}
static void __exit exit_wq(void)
{
pr_info("%s: called\n", __func__);
kref_put(&tw->kref, cleanup_wq);
}
module_init(init_wq);
module_exit(exit_wq);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);