-
Notifications
You must be signed in to change notification settings - Fork 5
/
lkm_mev.c
528 lines (421 loc) · 14.1 KB
/
lkm_mev.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* LKM Sandbox::Driver for memory-based character devices
* <https://github.com/tpiekarski/lkm-sandbox>
* ---
* Copyright 2020 Thomas Piekarski <[email protected]>
*
* This file is part of LKM Sandbox.
*
* LKM Sandbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* LKM Sandbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LKM Sandbox. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/semaphore.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Thomas Piekarski");
MODULE_DESCRIPTION("Driver for memory-based character devices");
MODULE_VERSION("0.1");
#define LKM_MEV_DEVICE_COUNT 1
#define LKM_MEV_DEVICE_MINOR 0
#define LKM_MEV_DEVICE_NAME "mev"
#define LKM_MEV_QSET_SIZE 1000
#define LKM_MEV_QUANTUM_SIZE 4000
// Structures
struct mev_qset {
void **data;
struct mev_qset *next;
};
struct mev_container {
struct mev_qset *data;
int quantum;
int qset;
unsigned long size;
struct semaphore sem;
struct cdev cdev;
};
// Prototypes
static long mev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static loff_t mev_llseek(struct file *file, loff_t offset, int whence);
struct mev_qset *mev_follow(struct mev_container *container, int n);
static bool mev_io_is_wronly(unsigned int f_flags);
static int mev_open(struct inode *inode, struct file *file);
static int mev_release(struct inode *inode, struct file *file);
static ssize_t mev_read(struct file *file, char __user *buf, size_t count,
loff_t *f_pos);
void mev_trim(struct mev_container *container);
static ssize_t mev_write(struct file *file, const char __user *buf,
size_t count, loff_t *f_pos);
// Global Declaration
static dev_t mev_device;
static struct mev_container *dev_container;
static struct file_operations mev_fops = {
.owner = THIS_MODULE,
.llseek = mev_llseek,
.read = mev_read,
.write = mev_write,
// Using new ioctl to avoid BKL (Big Kernel Lock)
// (for details refer to: https://lwn.net/Articles/119652/)
.unlocked_ioctl = mev_ioctl,
.open = mev_open,
.release = mev_release
};
static int lkm_mev_setup_cdev(struct mev_container *dev, int index)
{
int dev_no = MKDEV(MAJOR(mev_device), MINOR(mev_device) + index);
printk(KERN_INFO "%s: Trying to initialize and add cdev to kernel\n",
THIS_MODULE->name);
cdev_init(&dev->cdev, &mev_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &mev_fops;
int rc = cdev_add(&dev->cdev, dev_no, 1);
if (rc < 0) {
printk(KERN_ERR "%s: Failed adding cdev, %d, %d\n",
THIS_MODULE->name, MAJOR(dev_no), MINOR(dev_no));
return rc;
}
printk(KERN_INFO "%s: Initializing semaphore\n", THIS_MODULE->name);
sema_init(&dev->sem, 1);
return 0;
}
static int __init lkm_mev_init(void)
{
printk(KERN_INFO "%s: Initializing module\n", THIS_MODULE->name);
printk(KERN_INFO
"%s: Trying to allocate major and minor for device '%s'\n",
THIS_MODULE->name, LKM_MEV_DEVICE_NAME);
int rc = alloc_chrdev_region(&mev_device, LKM_MEV_DEVICE_MINOR,
LKM_MEV_DEVICE_COUNT, LKM_MEV_DEVICE_NAME);
if (rc < 0) {
printk(KERN_ERR
"%s: Failed allocating major/minor for device '%s'\n",
THIS_MODULE->name, LKM_MEV_DEVICE_NAME);
return rc;
}
printk(KERN_INFO
"%s: Allocated major %d and minor %d for device '%s'\n",
THIS_MODULE->name, MAJOR(mev_device), MINOR(mev_device),
LKM_MEV_DEVICE_NAME);
printk(KERN_INFO "%s: Allocating memory for device container\n",
THIS_MODULE->name);
size_t size = LKM_MEV_DEVICE_COUNT * sizeof(struct mev_container);
dev_container = kmalloc(size, GFP_KERNEL);
if (dev_container == NULL) {
printk(KERN_ERR
"%s: Failed allocating memory for device container\n",
THIS_MODULE->name);
return -ENOMEM;
}
rc = lkm_mev_setup_cdev(dev_container, 0);
if (rc < 0) {
printk(KERN_ERR "%s: Failed setting up cdev\n",
THIS_MODULE->name);
return rc;
}
printk(KERN_INFO "%s: Setup cdev and added cdev to kernel\n",
THIS_MODULE->name);
return 0;
}
module_init(lkm_mev_init);
static void __exit lkm_mev_exit(void)
{
printk(KERN_INFO "%s: Exiting module\n", THIS_MODULE->name);
if (dev_container != NULL) {
printk(KERN_INFO
"%s: Trimming data, deleting cdev and deallocating memory of device container\n",
THIS_MODULE->name);
mev_trim(dev_container);
cdev_del(&dev_container->cdev);
kfree(dev_container);
}
if (mev_device != 0) {
printk(KERN_INFO "%s: Unregistering major/minor of '%s'\n",
THIS_MODULE->name, LKM_MEV_DEVICE_NAME);
unregister_chrdev_region(mev_device, LKM_MEV_DEVICE_COUNT);
}
}
module_exit(lkm_mev_exit);
static long mev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
// todo: implement callback mev_ioctl
return 0l;
}
static loff_t mev_llseek(struct file *file, loff_t offset, int whence)
{
// todo: implement callback mev_llseek
return 0;
}
// todo: review and understand how following exactly works, why is there memory allocated
// Source of function: https://github.com/jesstess/ldd3-examples/blob/master/examples/scull/main.c#L262
struct mev_qset *mev_follow(struct mev_container *container, int n)
{
struct mev_qset *qset = container->data;
// If it's needed allocate first qset
// todo: check under what exactly circumstances qset could be emtpy
// todo: consider to extract this into an own, well named function
if (!qset) {
qset = container->data =
kmalloc(sizeof(struct mev_qset), GFP_KERNEL);
if (qset == NULL) {
return NULL;
}
// todo: check memset - what does it do exactly?
memset(qset, 0, sizeof(struct mev_qset));
}
// Follow the list
// todo: get to know why qsets are read from back to front
while (n--) {
if (!qset->next) {
// todo: figure out why allocating memory in advance (for writing?)
qset->next =
kmalloc(sizeof(struct mev_qset), GFP_KERNEL);
if (qset->next == NULL) {
return NULL;
}
// todo: check memset - what does it do exactly?
memset(qset->next, 0, sizeof(struct mev_qset));
}
qset = qset->next;
continue; // todo: figure out why such explicit continue
}
return qset;
}
static bool mev_io_is_wronly(unsigned int f_flags)
{
if ((f_flags & O_ACCMODE) == O_WRONLY) {
printk(KERN_DEBUG
"%s: - ((f_flags & O_ACCMODE) == O_WRONLY) = true\n",
THIS_MODULE->name);
return true;
}
return false;
}
static int mev_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "%s: Opening device\n", THIS_MODULE->name);
printk(KERN_INFO "%s: Looking for container with cdev at inode\n",
THIS_MODULE->name);
struct mev_container *container =
container_of(inode->i_cdev, struct mev_container, cdev);
if (container == NULL) {
printk(KERN_WARNING
"%s: Failed getting container by cdev at inode\n",
THIS_MODULE->name);
return -EFAULT;
}
printk(KERN_INFO "%s: Found container for device (%d, %d)\n",
THIS_MODULE->name, MAJOR(container->cdev.dev),
MINOR(container->cdev.dev));
// todo: check if the pointer to container won't point to NULL as soon as function is left
file->private_data = container;
if (mev_io_is_wronly(file->f_flags)) {
mev_trim(container);
}
return 0;
}
static int mev_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO "%s: Releasing device\n", THIS_MODULE->name);
// nothing to do for a memory-based device
return 0;
}
// todo: review and understand how reading exactly works
static ssize_t mev_read(struct file *file, char __user *buf, size_t count,
loff_t *f_pos)
{
printk(KERN_INFO "%s: Trying to read some data\n", THIS_MODULE->name);
struct mev_container *container = file->private_data;
struct mev_qset *dptr = NULL;
int quantum = container->quantum;
int qset = container->qset;
int itemsize = quantum * qset;
int item = 0;
int s_pos = 0;
int q_pos = 0;
int rest = 0;
ssize_t retval = 0;
if (down_interruptible(&container->sem)) {
printk(KERN_ERR
"%s: Failed getting hold of the semaphore by calling interruptible down\n",
THIS_MODULE->name);
return -ERESTARTSYS;
}
if (*f_pos >= container->size) {
printk(KERN_DEBUG "%s: - (*f_pos >= container->size) = true\n",
THIS_MODULE->name);
goto out;
}
if (*f_pos + count > container->size) {
printk(KERN_DEBUG
"%s: - (*f_pos + count > container->size) = true",
THIS_MODULE->name);
count = container->size - *f_pos;
}
// Find listitem, qset index and offset in quantum (same as in write)
// todo: output all values with printk
// todo: figure out why one time is a division where another is modulo?
item = (long)*f_pos / itemsize;
rest = (long)*f_pos % itemsize;
// s_pos corresponds to the position within scull
s_pos = rest / quantum;
// q_pos corresponds to the position within quantum
q_pos = rest % quantum;
// todo: check how to follow the list up to the right position
// The book using a function named scull_follow without mentioning it further
// (https://github.com/jesstess/ldd3-examples/blob/master/examples/scull/main.c#L262)
dptr = mev_follow(container, item); // -> corresponds to scull_follow()
// todo: extract boolean expression into well-named function
// (something like is_data_holey or is_data_empty (so java-like ;))
if (dptr == NULL || !dptr->data || !dptr->data[s_pos]) {
printk(KERN_DEBUG
"%s: - (dptr == NULL || !dptr->data || !dptr->data[s_pos]) = true\n",
THIS_MODULE->name);
goto out;
}
if (count > quantum - q_pos) {
printk(KERN_DEBUG "%s: - (count > quantum - q_pos) = true\n",
THIS_MODULE->name);
count = quantum - q_pos; // read up to the end of this quantum
}
if (copy_to_user(buf, dptr->data[s_pos] + q_pos, count)) {
printk(KERN_DEBUG
"%s: - (copy_to_user(buf, dptr->data[s_pos] + q_pos, count)) = true\n",
THIS_MODULE->name);
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
out:
up(&container->sem);
return retval;
}
// todo: review and understand how trimming exactly works
// todo: printk values in container before and after trimming
void mev_trim(struct mev_container *container)
{
printk(KERN_INFO "%s: Trimming device to '0'\n", THIS_MODULE->name);
struct mev_qset *next = NULL;
struct mev_qset *dptr = NULL;
int qset = container->qset;
int i = 0;
for (dptr = container->data; dptr; dptr = next) {
printk(KERN_DEBUG "%s: Trimming, entering outer loop\n",
THIS_MODULE->name);
if (dptr->data) {
for (i = 0; i < qset; i++) {
printk(KERN_DEBUG
"%s: Trimming, entering inner loop\n",
THIS_MODULE->name);
kfree(dptr->data[i]);
}
kfree(dptr->data);
dptr->data = NULL;
}
next = dptr->next;
kfree(dptr);
}
container->size = 0;
container->quantum = LKM_MEV_QUANTUM_SIZE;
container->qset = LKM_MEV_QSET_SIZE;
container->data = NULL;
}
// todo: review and understand how writing exactly works
static ssize_t mev_write(struct file *file, const char __user *buf,
size_t count, loff_t *f_pos)
{
printk(KERN_DEBUG "%s: Trying to write some data\n", THIS_MODULE->name);
struct mev_container *container = file->private_data;
struct mev_qset *dptr;
int quantum = container->quantum;
int qset = container->qset;
int itemsize = quantum * qset;
int item = 0;
int s_pos = 0;
int q_pos = 0;
int rest = 0;
ssize_t retval = -ENOMEM;
if (down_interruptible(&container->sem)) {
printk(KERN_ERR
"%s: Failed getting hold of the semaphore by calling interruptible down\n",
THIS_MODULE->name);
return -ERESTARTSYS;
}
// Find listitem, qset index and offset in quantum (same as in read)
// todo: output all values with printk
// todo: figure out why one time is a division where another is modulo?
item = (long)*f_pos / itemsize;
rest = (long)*f_pos % itemsize;
// s_pos corresponds to the position within scull
s_pos = rest / quantum;
// q_pos corresponds to the position within quantum
q_pos = rest % quantum;
// todo: check how to follow the list up to the right position
// The book using a function named scull_follow without mentioning it further
// (https://github.com/jesstess/ldd3-examples/blob/master/examples/scull/main.c#L262)
dptr = mev_follow(container, item); // -> corresponds to scull_follow()
if (dptr == NULL) {
printk(KERN_DEBUG "%s: - (dptr == NULL) = true\n",
THIS_MODULE->name);
goto out;
}
// todo: get more comfortable with memory allocation in ANSI C and in the kernel
// (For example write a little lkm_kmalloc module and let your VM run out of memory :))
if (!dptr->data) {
printk(KERN_DEBUG "%s: - (!dptr->data) = true\n",
THIS_MODULE->name);
dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
if (!dptr->data) {
goto out;
}
// todo: check what and why memset is used
memset(dptr->data, 0, qset * sizeof(char *));
}
if (!dptr->data[s_pos]) {
printk(KERN_DEBUG "%s: - (!dptr->data[s_pos]) = true\n",
THIS_MODULE->name);
dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
if (!dptr->data[s_pos]) {
goto out;
}
// todo: check why is memset not used here like in the parent data
}
if (count > quantum - q_pos) {
printk(KERN_DEBUG "%s: - (count > quantum - q_pos) = true\n",
THIS_MODULE->name);
count = quantum - q_pos; // write up to the end of this quantum
}
if (copy_from_user(dptr->data[s_pos] + q_pos, buf, count)) {
printk(KERN_DEBUG
"%s: - (copy_from_user(dptr->data[s_pos] + q_pos, buf, count)) = true\n",
THIS_MODULE->name);
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
if (container->size < *f_pos) {
printk(KERN_DEBUG "%s: - (container->size < *f_pos) = true\n",
THIS_MODULE->name);
container->size = *f_pos;
}
out:
up(&container->sem);
return retval;
}