aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/ftdev.c
blob: 7126c61e8b4c60f067dc17cb93a123f5753d96eb (plain) (blame)
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
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/vmalloc.h>

#include <litmus/litmus.h>
#include <litmus/feather_trace.h>
#include <litmus/ftdev.h>

struct ft_buffer* alloc_ft_buffer(unsigned int count, size_t size)
{
	struct ft_buffer* buf;
	size_t total = (size + 1) * count;
	char* mem;
	int order = 0, pages = 1;

	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
	if (!buf)
		return NULL;


	mem = vmalloc(total);

	if (!mem) {
		kfree(buf);
		return NULL;
	}

	if (!init_ft_buffer(buf, count, size,
			    mem + (count * size),  /* markers at the end */
			    mem)) {                /* buffer objects     */
		vfree(mem);
		kfree(buf);
		return NULL;
	}
	return buf;
}

void free_ft_buffer(struct ft_buffer* buf)
{
	int order = 0, pages = 1;
	size_t total;

	if (buf) {
		vfree(buf->buffer_mem);
		kfree(buf);
	}
}

struct ftdev_event {
	int id;
	struct ftdev_event* next;
};

static int activate(struct ftdev_event** chain, int id)
{
	struct ftdev_event* ev = kmalloc(sizeof(*ev), GFP_KERNEL);
	if (ev) {
		printk(KERN_INFO
		       "Enabling feather-trace event %d.\n", (int) id);
		ft_enable_event(id);
		ev->id = id;
		ev->next = *chain;
		*chain    = ev;
	}
	return ev ? 0 : -ENOMEM;
}

static void deactivate(struct ftdev_event** chain, int id)
{
	struct ftdev_event **cur = chain;
	struct ftdev_event *nxt;
	while (*cur) {
		if ((*cur)->id == id) {
			nxt   = (*cur)->next;
			kfree(*cur);
			*cur  = nxt;
			printk(KERN_INFO
			       "Disabling feather-trace event %d.\n", (int) id);
			ft_disable_event(id);
			break;
		}
		cur = &(*cur)->next;
	}
}

static int ftdev_open(struct inode *in, struct file *filp)
{
	struct ftdev* ftdev;
	struct ftdev_minor* ftdm;
	unsigned int buf_idx = iminor(in);
	int err = 0;

	ftdev = container_of(in->i_cdev, struct ftdev, cdev);

	if (buf_idx >= ftdev->minor_cnt) {
		err = -ENODEV;
		goto out;
	}
	if (ftdev->can_open && (err = ftdev->can_open(ftdev, buf_idx)))
		goto out;

	ftdm = ftdev->minor + buf_idx;
	ftdm->ftdev = ftdev;
	filp->private_data = ftdm;

	if (mutex_lock_interruptible(&ftdm->lock)) {
		err = -ERESTARTSYS;
		goto out;
	}

	if (!ftdm->readers && ftdev->alloc)
		err = ftdev->alloc(ftdev, buf_idx);
	if (0 == err)
		ftdm->readers++;

	mutex_unlock(&ftdm->lock);
out:
	return err;
}

static int ftdev_release(struct inode *in, struct file *filp)
{
	struct ftdev* ftdev;
	struct ftdev_minor* ftdm;
	unsigned int buf_idx = iminor(in);
	int err = 0;

	ftdev = container_of(in->i_cdev, struct ftdev, cdev);

	if (buf_idx >= ftdev->minor_cnt) {
		err = -ENODEV;
		goto out;
	}
	ftdm = ftdev->minor + buf_idx;

	if (mutex_lock_interruptible(&ftdm->lock)) {
		err = -ERESTARTSYS;
		goto out;
	}

	if (ftdm->readers == 1) {
		while (ftdm->events)
			deactivate(&ftdm->events, ftdm->events->id);

		/* wait for any pending events to complete */
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(HZ);

		printk(KERN_ALERT "Failed trace writes: %u\n",
		       ftdm->buf->failed_writes);

		if (ftdev->free)
			ftdev->free(ftdev, buf_idx);
	}

	ftdm->readers--;
	mutex_unlock(&ftdm->lock);
out:
	return err;
}

/* based on ft_buffer_read
 * @returns < 0 : page fault
 *          = 0 : no data available
 *          = 1 : one slot copied
 */
static int ft_buffer_copy_to_user(struct ft_buffer* buf, char __user *dest)
{
	unsigned int idx;
	int err = 0;
	if (buf->free_count != buf->slot_count) {
		/* data available */
		idx = buf->read_idx % buf->slot_count;
		if (buf->slots[idx] == SLOT_READY) {
			err = copy_to_user(dest, ((char*) buf->buffer_mem) +
					   idx * buf->slot_size,
					   buf->slot_size);
			if (err == 0) {
				/* copy ok */
				buf->slots[idx] = SLOT_FREE;
				buf->read_idx++;
				fetch_and_inc(&buf->free_count);
				err = 1;
			}
		}
	}
	return err;
}

static ssize_t ftdev_read(struct file *filp,
			  char __user *to, size_t len, loff_t *f_pos)
{
	/* 	we ignore f_pos, this is strictly sequential */

	ssize_t err = 0;
	size_t chunk;
	int copied;
	struct ftdev_minor* ftdm = filp->private_data;

	if (mutex_lock_interruptible(&ftdm->lock)) {
		err = -ERESTARTSYS;
		goto out;
	}


	chunk = ftdm->buf->slot_size;
	while (len >= chunk) {
		copied = ft_buffer_copy_to_user(ftdm->buf, to);
		if (copied == 1) {
			len    -= chunk;
			to     += chunk;
			err    += chunk;
	        } else if (err == 0 && copied == 0 && ftdm->events) {
			/* Only wait if there are any events enabled and only
			 * if we haven't copied some data yet. We cannot wait
			 * here with copied data because that data would get
			 * lost if the task is interrupted (e.g., killed).
			 */
			mutex_unlock(&ftdm->lock);
			set_current_state(TASK_INTERRUPTIBLE);

			schedule_timeout(50);

			if (signal_pending(current)) {
				if (err == 0)
					/* nothing read yet, signal problem */
					err = -ERESTARTSYS;
				goto out;
			}
			if (mutex_lock_interruptible(&ftdm->lock)) {
				err = -ERESTARTSYS;
				goto out;
			}
		} else if (copied < 0) {
			/* page fault */
			err = copied;
			break;
		} else
			/* nothing left to get, return to user space */
			break;
	}
	mutex_unlock(&ftdm->lock);
out:
	return err;
}

static long ftdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	long err = -ENOIOCTLCMD;
	struct ftdev_minor* ftdm = filp->private_data;

	if (mutex_lock_interruptible(&ftdm->lock)) {
		err = -ERESTARTSYS;
		goto out;
	}

	/* FIXME: check id against list of acceptable events */

	switch (cmd) {
	case  FTDEV_ENABLE_CMD:
		if (activate(&ftdm->events, arg))
			err = -ENOMEM;
		else
			err = 0;
		break;

	case FTDEV_DISABLE_CMD:
		deactivate(&ftdm->events, arg);
		err = 0;
		break;

	default:
		printk(KERN_DEBUG "ftdev: strange ioctl (%u, %lu)\n", cmd, arg);
	};

	mutex_unlock(&ftdm->lock);
out:
	return err;
}

static ssize_t ftdev_write(struct file *filp, const char __user *from,
			   size_t len, loff_t *f_pos)
{
	struct ftdev_minor* ftdm = filp->private_data;
	ssize_t err = -EINVAL;
	struct ftdev* ftdev = ftdm->ftdev;

	/* dispatch write to buffer-specific code, if available */
	if (ftdev->write)
		err = ftdev->write(ftdm->buf, len, from);

	return err;
}

struct file_operations ftdev_fops = {
	.owner   = THIS_MODULE,
	.open    = ftdev_open,
	.release = ftdev_release,
	.write   = ftdev_write,
	.read    = ftdev_read,
	.unlocked_ioctl = ftdev_ioctl,
};

int ftdev_init(	struct ftdev* ftdev, struct module* owner,
		const int minor_cnt, const char* name)
{
	int i, err;

	BUG_ON(minor_cnt < 1);

	cdev_init(&ftdev->cdev, &ftdev_fops);
	ftdev->name = name;
	ftdev->minor_cnt = minor_cnt;
	ftdev->cdev.owner = owner;
	ftdev->cdev.ops = &ftdev_fops;
	ftdev->alloc    = NULL;
	ftdev->free     = NULL;
	ftdev->can_open = NULL;
	ftdev->write	= NULL;

	ftdev->minor = kcalloc(ftdev->minor_cnt, sizeof(*ftdev->minor),
			GFP_KERNEL);
	if (!ftdev->minor) {
		printk(KERN_WARNING "ftdev(%s): Could not allocate memory\n",
			ftdev->name);
		err = -ENOMEM;
		goto err_out;
	}

	for (i = 0; i < ftdev->minor_cnt; i++) {
		mutex_init(&ftdev->minor[i].lock);
		ftdev->minor[i].readers = 0;
		ftdev->minor[i].buf     = NULL;
		ftdev->minor[i].events  = NULL;
	}

	ftdev->class = class_create(owner, ftdev->name);
	if (IS_ERR(ftdev->class)) {
		err = PTR_ERR(ftdev->class);
		printk(KERN_WARNING "ftdev(%s): "
			"Could not create device class.\n", ftdev->name);
		goto err_dealloc;
	}

	return 0;

err_dealloc:
	kfree(ftdev->minor);
err_out:
	return err;
}

/*
 * Destroy minor devices up to, but not including, up_to.
 */
static void ftdev_device_destroy(struct ftdev* ftdev, unsigned int up_to)
{
	dev_t minor_cntr;

	if (up_to < 1)
		up_to = (ftdev->minor_cnt < 1) ? 0 : ftdev->minor_cnt;

	for (minor_cntr = 0; minor_cntr < up_to; ++minor_cntr)
		device_destroy(ftdev->class, MKDEV(ftdev->major, minor_cntr));
}

void ftdev_exit(struct ftdev* ftdev)
{
	printk("ftdev(%s): Exiting\n", ftdev->name);
	ftdev_device_destroy(ftdev, -1);
	cdev_del(&ftdev->cdev);
	unregister_chrdev_region(MKDEV(ftdev->major, 0), ftdev->minor_cnt);
	class_destroy(ftdev->class);
	kfree(ftdev->minor);
}

int register_ftdev(struct ftdev* ftdev)
{
	struct device **device;
	dev_t trace_dev_tmp, minor_cntr;
	int err;

	err = alloc_chrdev_region(&trace_dev_tmp, 0, ftdev->minor_cnt,
			ftdev->name);
	if (err) {
		printk(KERN_WARNING "ftdev(%s): "
		       "Could not allocate char. device region (%d minors)\n",
		       ftdev->name, ftdev->minor_cnt);
		goto err_out;
	}

	ftdev->major = MAJOR(trace_dev_tmp);

	err = cdev_add(&ftdev->cdev, trace_dev_tmp, ftdev->minor_cnt);
	if (err) {
		printk(KERN_WARNING "ftdev(%s): "
		       "Could not add cdev for major %u with %u minor(s).\n",
		       ftdev->name, ftdev->major, ftdev->minor_cnt);
		goto err_unregister;
	}

	/* create the minor device(s) */
	for (minor_cntr = 0; minor_cntr < ftdev->minor_cnt; ++minor_cntr)
	{
		trace_dev_tmp = MKDEV(ftdev->major, minor_cntr);
		device = &ftdev->minor[minor_cntr].device;

		*device = device_create(ftdev->class, NULL, trace_dev_tmp, NULL,
				"litmus/%s%d", ftdev->name, minor_cntr);
		if (IS_ERR(*device)) {
			err = PTR_ERR(*device);
			printk(KERN_WARNING "ftdev(%s): "
				"Could not create device major/minor number "
				"%u/%u\n", ftdev->name, ftdev->major,
				minor_cntr);
			printk(KERN_WARNING "ftdev(%s): "
				"will attempt deletion of allocated devices.\n",
				ftdev->name);
			goto err_minors;
		}
	}

	return 0;

err_minors:
	ftdev_device_destroy(ftdev, minor_cntr);
	cdev_del(&ftdev->cdev);
err_unregister:
	unregister_chrdev_region(MKDEV(ftdev->major, 0), ftdev->minor_cnt);
err_out:
	return err;
}