summaryrefslogtreecommitdiffstats
path: root/drivers/misc/eventlib/eventlib.c
blob: aed557fd745b5e7f6832087f19ebf6e997fcdcb6 (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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * eventlib.c
 *
 * Copyright (c) 2017-2018, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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.
 *
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/crc32.h>

#include <linux/keventlib.h>

#include "eventlib.h"

#define KEVENTLIB_VERSION		"0.2"

#define EVENTLIB_SYSFS_DIR_NAME		"eventlib"
#define EVENTLIB_SYSFS_TEST_FILE_NAME	"test"
#define EVENTLIB_SYSFS_EVENTS_FILE_NAME	"events"
#define EVENTLIB_SYSFS_SCHEMA_FILE_NAME	"schema"

#define EVENTLIB_TEST_SHM_SIZE		(PAGE_SIZE)

#define EVENTLIB_MAX_PROVIDERS		256
#define EVENTLIB_TEST_DATA_SIZE		0x10

struct eventlib_provider_info {
	struct kobject *kobj;

	struct bin_attribute attr;
	struct bin_attribute attr_schema;

	void *data;
	size_t data_size;

	struct eventlib_ctx el_ctx;

	void *w2r;
	size_t w2r_size;

	int id;
	struct list_head list;

	char *schema;
	size_t schema_size;
};

static struct eventlib_module {
	struct kobject *kobj_root;

	struct list_head providers;
	atomic_t nr_providers;

	spinlock_t lock;

	int test_id;
} ctx;

struct eventlib_work_data {
	struct work_struct work;
	struct eventlib_provider_info *provider;
};

#define EVENTLIB_TEST_SAMPLE_MAGIC	0x11223344
struct eventlib_test_sample {
	uint32_t magic;
	uint32_t size;
	uint32_t crc;
} __attribute__((__packed__));

static int is_initialized;

static int keventlib_init(struct eventlib_provider_info *info)
{
	int ret;
	struct eventlib_ctx *el_ctx = &info->el_ctx;

	info->w2r = info->data;
	info->w2r_size = info->data_size;

	pr_debug("w2r: %p, size: %#zx\n", info->w2r, info->w2r_size);

	memset(el_ctx, 0, sizeof(*el_ctx));

	el_ctx->direction = EVENTLIB_DIRECTION_WRITER;
	el_ctx->w2r_shm = info->w2r;
	el_ctx->w2r_shm_size = (uint32_t)info->w2r_size;
	el_ctx->r2w_shm = NULL;
	el_ctx->r2w_shm_size = 0;
	el_ctx->flags = 0;

	ret = eventlib_init(el_ctx);
	if (ret)
		return ret;

	return 0;
}

static int
sysfs_mmap(struct file *filp, struct kobject *kobj,
	   struct bin_attribute *attr, struct vm_area_struct *vma)
{
	unsigned long vm_size, pfn;

	struct eventlib_provider_info *info =
		container_of(attr, struct eventlib_provider_info, attr);

	vm_size = vma->vm_end - vma->vm_start;
	vma->vm_private_data = filp->private_data;

	pr_debug("%s: vma: %#lx - %#lx (%#lx)\n",
		 __func__, vma->vm_start, vma->vm_end, vm_size);

	if (vm_size != attr->size)
		return -EINVAL;

	if (!info->data)
		return -ENOMEM;

	pfn = virt_to_phys(info->data) >> PAGE_SHIFT;

	if (remap_pfn_range(vma, vma->vm_start, pfn,
			    vma->vm_end - vma->vm_start,
			    vma->vm_page_prot))
		return -EAGAIN;

	return 0;
}

static ssize_t
sysfs_schema_read(struct file *filp, struct kobject *kobj,
		  struct bin_attribute *attr,
		  char *buf, loff_t off, size_t len)
{
	struct eventlib_provider_info *info =
		container_of(attr, struct eventlib_provider_info, attr_schema);

	if (info->schema == NULL)
		return -ENOENT;

	if (len > info->schema_size - off)
		len = info->schema_size - off;

	memcpy(buf, info->schema + off, len);

	return len;
}

static int
create_sysfs_entry(struct eventlib_provider_info *info,
		   const char *name)
{
	int ret;
	struct bin_attribute *attr = &info->attr;

	info->kobj = kobject_create_and_add(name, ctx.kobj_root);
	if (info->kobj == NULL) {
		pr_err("Unable to create sysfs directory: %s\n", name);
		return -ENOMEM;
	}

	sysfs_bin_attr_init(attr);

	attr->attr.name = EVENTLIB_SYSFS_EVENTS_FILE_NAME;
	attr->attr.mode = 0444;
	attr->mmap = sysfs_mmap;
	attr->read = NULL;
	attr->size = info->data_size;

	ret = sysfs_create_bin_file(info->kobj, attr);
	if (ret) {
		pr_err("Unable to create sysfs file: %s\n",
		       attr->attr.name);
		kobject_put(info->kobj);
		return ret;
	}

	if (info->schema) {
		struct bin_attribute *attr_schema = &info->attr_schema;

		sysfs_bin_attr_init(attr_schema);

		attr_schema->attr.name = EVENTLIB_SYSFS_SCHEMA_FILE_NAME;
		attr_schema->attr.mode = 0444;
		attr_schema->mmap = NULL;
		attr_schema->read = sysfs_schema_read;
		attr_schema->write = NULL;
		attr_schema->size = info->schema_size;

		ret = sysfs_create_bin_file(info->kobj, attr_schema);
		if (ret) {
			pr_err("Unable to create sysfs file: %s\n",
			       attr_schema->attr.name);
			kobject_put(info->kobj);
			return ret;
		}
	}

	return 0;
}

static void remove_sysfs_entry(struct eventlib_provider_info *info)
{
	sysfs_remove_bin_file(info->kobj, &info->attr);
	if (info->schema)
		sysfs_remove_bin_file(info->kobj, &info->attr_schema);

	kobject_put(info->kobj);

}

static int is_id_free(int id)
{
	struct eventlib_provider_info *info;

	list_for_each_entry(info, &ctx.providers, list) {
		if (id == info->id)
			return 0;
	}

	return 1;
}

static int get_free_id(void)
{
	int id;

	for (id = 0; id < EVENTLIB_MAX_PROVIDERS; id++) {
		if (is_id_free(id))
			return id;
	}

	return -EMFILE;
}

static int
provider_init(struct eventlib_provider_info *info,
	      size_t size, const char *name,
	      const char *schema, size_t schema_size)
{
	int ret = 0, id;

	info->data = NULL;
	info->data_size = 0;

	info->w2r = NULL;
	info->w2r_size = 0;

	if (size == 0 || !is_power_of_2(size))
		return -EINVAL;

	info->data = (void *)__get_free_pages(GFP_KERNEL, get_order(size));
	if (!info->data)
		return -ENOMEM;

	memset(info->data, 0, size);
	info->data_size = size;

	if (schema && schema_size > 0) {
		info->schema_size = schema_size;

		info->schema = kmalloc(info->schema_size, GFP_KERNEL);
		if (!info->schema)
			return -ENOMEM;

		memcpy(info->schema, schema, schema_size);
	} else {
		info->schema = NULL;
		info->schema_size = 0;
	}

	ret = create_sysfs_entry(info, name);
	if (ret < 0)
		goto err_free;

	ret = keventlib_init(info);
	if (ret < 0)
		goto err_sysfs;

	INIT_LIST_HEAD(&info->list);

	spin_lock(&ctx.lock);

	id = get_free_id();
	if (id < 0) {
		pr_err("Too many providers: > %d\n", EVENTLIB_MAX_PROVIDERS);
		ret = -EMFILE;
		goto err_get_id;
	}

	info->id = id;

	list_add_tail(&info->list, &ctx.providers);
	atomic_inc(&ctx.nr_providers);

	spin_unlock(&ctx.lock);

	return 0;

err_get_id:
	spin_unlock(&ctx.lock);
	eventlib_close(&info->el_ctx);

err_sysfs:
	remove_sysfs_entry(info);

err_free:
	if (info->schema) {
		kfree(info->schema);
		info->schema = NULL;
	}

	free_pages((unsigned long)info->data, get_order(size));

	return ret;
}

static struct eventlib_provider_info *
find_provider_info(int id)
{
	struct eventlib_provider_info *info;

	list_for_each_entry(info, &ctx.providers, list) {
		if (id == info->id)
			return info;
	}

	return NULL;
}

static void
__free_provider(struct work_struct *work)
{
	struct eventlib_work_data *wd =
		container_of(work, struct eventlib_work_data, work);

	struct eventlib_provider_info *info = wd->provider;

	remove_sysfs_entry(info);

	if (info->schema)
		kfree(info->schema);

	kfree(info);
	kfree(wd);

	if (atomic_dec_and_test(&ctx.nr_providers))
		kobject_put(ctx.kobj_root);
}

static void free_provider(struct eventlib_provider_info *info)
{
	struct eventlib_work_data *wd;

	eventlib_close(&info->el_ctx);

	free_pages((unsigned long)info->data,
		   get_order(info->data_size));

	list_del(&info->list);

	wd = kmalloc(sizeof(*wd), GFP_ATOMIC);
	if (!wd)
		return;

	wd->provider = info;
	INIT_WORK(&wd->work, __free_provider);
	schedule_work(&wd->work);
}

static void unregister_all_providers(void)
{
	struct eventlib_provider_info *info, *next;

	spin_lock(&ctx.lock);
	list_for_each_entry_safe(info, next, &ctx.providers, list)
		free_provider(info);
	spin_unlock(&ctx.lock);
}

int keventlib_write(int id, void *data, size_t size, uint32_t type, uint64_t ts)
{
	int err = 0;
	struct eventlib_provider_info *info;

	pr_debug("%s: size: %#zx\n", __func__, size);

	spin_lock(&ctx.lock);

	info = find_provider_info(id);
	if (!info) {
		err = -ENOENT;
		goto err_out;
	}

	if (!info->data) {
		err = -ENOMEM;
		goto err_out;
	}

	eventlib_write(&info->el_ctx, 0, type, ts, data, size);

err_out:
	spin_unlock(&ctx.lock);
	return err;
}
EXPORT_SYMBOL(keventlib_write);

int keventlib_register(size_t size, const char *name,
		       const char *schema, size_t schema_size)
{
	int ret;
	struct eventlib_provider_info *info;

	if (!is_initialized) {
		pr_warn("keventlib is not initialized\n");
		return -EACCES;
	}

	info = kmalloc(sizeof(*info), GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	ret = provider_init(info, size, name, schema, schema_size);
	if (ret < 0) {
		kfree(info);
		return ret;
	}

	return info->id;
}
EXPORT_SYMBOL(keventlib_register);

void keventlib_unregister(int id)
{
	struct eventlib_provider_info *info;

	if (!is_initialized) {
		pr_warn("keventlib is not initialized\n");
		return;
	}

	spin_lock(&ctx.lock);

	info = find_provider_info(id);
	if (!info) {
		pr_err("Unregistered provider: %d\n", id);
		spin_unlock(&ctx.lock);
		return;
	}

	free_provider(info);

	spin_unlock(&ctx.lock);
}
EXPORT_SYMBOL(keventlib_unregister);

static void put_test_data(int id)
{
	int i;
	uint8_t value = 0;
	uint64_t ts = 0;
	uint8_t *data;
	struct eventlib_test_sample *s;
	uint8_t buffer[sizeof(struct eventlib_test_sample) +
		       EVENTLIB_TEST_DATA_SIZE];

	for (i = 0; i < 32; i++) {
		s = (struct eventlib_test_sample *)buffer;
		data = (uint8_t *)(s + 1);

		s->magic = EVENTLIB_TEST_SAMPLE_MAGIC;
		s->size = EVENTLIB_TEST_DATA_SIZE;
		memset(data, value++, s->size);
		s->crc = crc32(~0U, data, s->size) ^ 0xffffffff;

		keventlib_write(ctx.test_id, buffer,
				sizeof(buffer), 10 + id, ts++);
	}
}

static int __init
eventlib_module_init(void)
{
	int ret;

	if (is_initialized)
		return -ENOMEM;

	atomic_set(&ctx.nr_providers, 0);

	INIT_LIST_HEAD(&ctx.providers);
	spin_lock_init(&ctx.lock);

	ctx.kobj_root = kobject_create_and_add(EVENTLIB_SYSFS_DIR_NAME,
					       kernel_kobj);
	if (ctx.kobj_root == NULL) {
		pr_err("Unable to create sysfs directory: %s\n",
		       EVENTLIB_SYSFS_DIR_NAME);
		return -ENOMEM;
	}

	is_initialized = 1;

	ret = keventlib_register(EVENTLIB_TEST_SHM_SIZE,
				 EVENTLIB_SYSFS_TEST_FILE_NAME,
				 NULL, 0);
	if (ret < 0) {
		kobject_put(ctx.kobj_root);
		is_initialized = 0;
		return ret;
	}

	ctx.test_id = ret;
	put_test_data(ctx.test_id);

	pr_info("keventlib is initialized, test id: %d\n", ctx.test_id);

	return 0;
}

static void __exit
eventlib_module_exit(void)
{
	unregister_all_providers();
	pr_info("keventlib is uninitialized\n");
}

subsys_initcall(eventlib_module_init);
module_exit(eventlib_module_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Nvidia Ltd");
MODULE_DESCRIPTION("Kernel Eventlib");
MODULE_VERSION(KEVENTLIB_VERSION);