-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_queue_character_driver.c
270 lines (190 loc) · 7.41 KB
/
message_queue_character_driver.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
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
#define pr_fmt(fmt) "%s:%s(): " fmt, KBUILD_MODNAME, __func__
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kthread.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
MODULE_AUTHOR("Georgiy Lebedev");
MODULE_DESCRIPTION("Miscellaneous character driver implementing a message queue");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
#define KTHREAD_NAME "message_queue_character_device_kthread"
static int free_ptr(void *ptr)
{
kfree(ptr);
do_exit(0);
}
struct {
struct device *dev;
struct mutex dev_lock;
struct message_queue {
const char **messages;
size_t head;
size_t tail;
size_t capacity;
} mq;
} static *drv_ctx;
#define MESSAGE_QUEUE_INIT_CAPACITY 2lu
#define MESSAGE_QUEUE_GROWTH_COEFF 2lu
static int message_queue_enqueue(struct message_queue *mq, const char *msg, const size_t msglen)
{
const char **new_messages = NULL;
const size_t new_capacity = mq->capacity * MESSAGE_QUEUE_GROWTH_COEFF;
size_t mq_sz = 0;
const size_t head_sz = mq->head + 1;
const size_t tail_sz = mq->capacity - mq->tail - 1;
size_t last_msg_idx = 0;
dev_info(drv_ctx->dev, "enqueing message");
if (mq->capacity == 0) {
dev_info(drv_ctx->dev, "constructing new message queue with initial capacity '%zu' and grow coefficient '%zu'", MESSAGE_QUEUE_INIT_CAPACITY, MESSAGE_QUEUE_GROWTH_COEFF);
mq->messages = kzalloc(MESSAGE_QUEUE_INIT_CAPACITY * sizeof(*mq->messages), GFP_KERNEL);
if (unlikely(mq->messages == NULL)) return -ENOMEM;
mq->capacity = MESSAGE_QUEUE_INIT_CAPACITY;
mq->head = mq->tail = MESSAGE_QUEUE_INIT_CAPACITY - 1;
} else if ((mq->tail - 1 + mq->capacity) % mq->capacity == mq->head) {
dev_info(drv_ctx->dev, "reallocating message queue from capacity '%zu' to capacity '%zu'", mq->capacity, new_capacity);
new_messages = kzalloc(new_capacity * sizeof(*new_messages), GFP_KERNEL);
if (unlikely(new_messages == NULL)) return -ENOMEM;
last_msg_idx = (mq->tail + 1) % mq->capacity;
if (last_msg_idx <= mq->head) {
mq_sz = mq->capacity - 1 - (mq->tail + 1) % mq->capacity;
memcpy(new_messages + new_capacity - mq_sz, mq->messages + last_msg_idx, mq_sz * sizeof(*mq->messages));
mq->tail = new_capacity - mq_sz - 1;
} else {
memcpy(new_messages + new_capacity - head_sz, mq->messages, (head_sz) * sizeof(*mq->messages));
memcpy(new_messages + new_capacity - head_sz - tail_sz, mq->messages + mq->tail + 1, tail_sz * sizeof(*mq->messages));
mq->tail = new_capacity - head_sz - tail_sz - 1;
}
kthread_run(free_ptr, mq->messages, KTHREAD_NAME);
mq->messages = new_messages;
mq->head = new_capacity - 1;
mq->capacity = new_capacity;
}
mq->messages[mq->tail] = msg;
mq->tail = (mq->tail - 1) % mq->capacity;
return 0;
}
#undef MESSAGE_QUEUE_INIT_CAPACITY
#undef MESSAGE_QUEUE_GROWTH_COEFF
static const char *message_queue_dequeue(struct message_queue *mq)
{
const char *msg = mq->messages[mq->head];
dev_info(drv_ctx->dev, "dequeing message");
if (mq->tail == mq->head) return NULL;
mq->head = (mq->head - 1) % mq->capacity;
return msg;
}
static int dev_open(struct inode *inode, struct file *filp)
{
if (mutex_is_locked(&drv_ctx->dev_lock)) {
dev_info(drv_ctx->dev, "device already in use");
return -EBUSY;
}
dev_info(drv_ctx->dev, "opening file");
mutex_lock(&drv_ctx->dev_lock);
return nonseekable_open(inode, filp);
}
static ssize_t dev_read(struct file *filp, char __user *user_buf, const size_t sz, loff_t *off)
{
char *msg = NULL;
size_t msglen = 0;
dev_info(drv_ctx->dev, "reading message: sz=%zu, off=%llu", sz, *off);
if (*off > 0) return 0;
msg = (char *) message_queue_dequeue(&drv_ctx->mq);
if (msg == NULL) return 0;
msglen = strlen(msg);
if (copy_to_user(user_buf, msg, msglen) != 0) {
dev_warn(drv_ctx->dev, "copy_to_user failed");
kthread_run(free_ptr, msg, KTHREAD_NAME);
return -EFAULT;
}
dev_info(drv_ctx->dev, "reading message: msg=%s", msg);
kthread_run(free_ptr, msg, KTHREAD_NAME);
*off += msglen;
return msglen;
}
static ssize_t dev_write(struct file *filp, const char __user *user_buf, const size_t sz, loff_t *off)
{
char *msg = NULL;
int status = 0;
dev_info(drv_ctx->dev, "writing message: sz=%zu, off=%llu", sz, *off);
msg = kzalloc((sz + 1) * sizeof(*msg), GFP_KERNEL);
if (unlikely(msg == NULL)) return -ENOMEM;
if (copy_from_user(msg, user_buf, sz) != 0) {
dev_warn(drv_ctx->dev, "copy_from_user failed");
kthread_run(free_ptr, msg, KTHREAD_NAME);
return -EFAULT;
}
dev_info(drv_ctx->dev, "writing message: msg=%s", msg);
status = message_queue_enqueue(&drv_ctx->mq, msg, sz);
if (status != 0) {
kthread_run(free_ptr, msg, KTHREAD_NAME);
return status;
}
return sz;
}
static int dev_close(struct inode *_, struct file *__)
{
dev_info(drv_ctx->dev, "closing file");
mutex_unlock(&drv_ctx->dev_lock);
return 0;
}
static const struct file_operations fops = {
.open = dev_open,
.read = dev_read,
.write = dev_write,
.llseek = no_llseek,
.release = dev_close,
};
static struct miscdevice miscdev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "mq",
.mode = 0666,
.fops = &fops,
};
static int __init drv_init(void)
{
int status = 0;
pr_info("initializing module");
status = misc_register(&miscdev);
if (status != 0) {
pr_notice("intializing module: misc device registration failed, aborting");
return status;
}
dev_info(miscdev.this_device, "initializing module: misc driver (major#=10) registered: minor#=%d", miscdev.minor);
drv_ctx = devm_kzalloc(miscdev.this_device, sizeof(*drv_ctx), GFP_KERNEL);
if (unlikely(drv_ctx == NULL)) return -ENOMEM;
drv_ctx->dev = miscdev.this_device;
mutex_init(&drv_ctx->dev_lock);
dev_info(drv_ctx->dev, "initializing module: driver initialized");
return 0;
}
static void __exit drv_exit(void)
{
size_t idx = drv_ctx->mq.head;
size_t last_msg_idx = 0;
size_t i = 0;
pr_info("exiting module");
dev_info(drv_ctx->dev, "exiting module: head=%zu tail=%zu capacity=%zu", drv_ctx->mq.head, drv_ctx->mq.tail, drv_ctx->mq.capacity);
for (; i < drv_ctx->mq.capacity; ++i) dev_info(drv_ctx->dev, "exiting module: messages[%zu]=%s", i, drv_ctx->mq.messages[i]);
if (drv_ctx->mq.head == drv_ctx->mq.tail) goto deregister;
last_msg_idx = (drv_ctx->mq.tail + 1) % drv_ctx->mq.capacity;
if (last_msg_idx <= drv_ctx->mq.head) {
for (; last_msg_idx <= idx; --idx) kthread_run(free_ptr, (char *) drv_ctx->mq.messages[idx], KTHREAD_NAME);
} else {
for (; 0 <= idx; --idx) kthread_run(free_ptr, (char *) drv_ctx->mq.messages[idx], KTHREAD_NAME);
idx = drv_ctx->mq.capacity - 1;
for (; drv_ctx->mq.tail < idx; --idx) kthread_run(free_ptr, (char *) drv_ctx->mq.messages[idx], KTHREAD_NAME);
}
kthread_run(free_ptr, drv_ctx->mq.messages, KTHREAD_NAME);
deregister:
misc_deregister(&miscdev);
pr_info("exiting moudle: driver deregistered");
}
#undef pr_fmt
module_init(drv_init);
module_exit(drv_exit);