summaryrefstree
path: root/kernel/fs/proc/dlog.c
blob: ab8db2472dbfaf6145edf1a9821488f45ec2e779 (plain)
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
/*
 *  linux/fs/proc/dlog.c
 *
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/security.h>
#include <linux/syscalls.h>
#include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <linux/fs.h>
#include <linux/seq_file.h>

#define DLOG_ACTION_CLOSE          	0
#define DLOG_ACTION_OPEN           	1
#define DLOG_ACTION_READ		2
#define DLOG_ACTION_READ_ALL		3
#define DLOG_ACTION_WRITE    		4
#define DLOG_ACTION_SIZE_BUFFER   	5

#define DLOG_BUF_SHIFT 			18 /*256KB*/

#define S_PREFIX_MAX			32
#define DLOG_BUF_LINE_MAX		(1024 - S_PREFIX_MAX)

/* record buffer */
#define DLOG_BUF_ALIGN			__alignof__(struct dlog_metadata)
#define __DLOG_BUF_LEN			(1 << DLOG_BUF_SHIFT)

struct dlog_sequence {
	u64 dlog_seq;
	u32 dlog_idx;
	u64 dlog_first_seq;
	u32 dlog_first_idx;
	u64 dlog_next_seq;
	u32 dlog_next_idx;
	u64 dlog_clear_seq;
	u32 dlog_clear_idx;
	u64 dlog_end_seq;
};

struct dlog_metadata {
	u64 ts_nsec;
	u16 len;
	u16 text_len;
	pid_t pid;
	pid_t tgid;
	char comm[TASK_COMM_LEN];
	char tgid_comm[TASK_COMM_LEN];
};

struct dlog_data {
	struct dlog_sequence dlog_seq;
	struct dlog_metadata dlog_meta;
	spinlock_t dlog_spinlock;
	wait_queue_head_t dlog_wait_queue;
	__aligned(DLOG_BUF_ALIGN) char dlog_cbuf[__DLOG_BUF_LEN];
};

static struct proc_dir_entry *dlog_dir;

static int do_dlog(int type, struct dlog_data *dl_data, char __user *buf, int count);
static int vdlog(struct dlog_data *dl_data, const char *fmt, va_list args);

#define DEFINE_DLOG_VARIABLE(_name) \
static struct dlog_data dlog_data_##_name = { \
	.dlog_seq = { \
		.dlog_seq = 0, \
		.dlog_idx = 0, \
		.dlog_first_seq = 0, \
		.dlog_first_idx = 0, \
		.dlog_next_seq = 0, \
		.dlog_next_idx = 0, \
		.dlog_clear_seq = 0, \
		.dlog_clear_idx = 0, \
		.dlog_end_seq = -1 \
	}, \
	.dlog_meta = { \
		.ts_nsec = 0, \
		.len = 0, \
		.text_len = 0, \
		.pid = 0, \
		.tgid = 0, \
		.comm = {0, }, \
		.tgid_comm = {0, } \
	}, \
	.dlog_spinlock = __SPIN_LOCK_INITIALIZER(dlog_data_##_name.dlog_spinlock), \
	.dlog_wait_queue = __WAIT_QUEUE_HEAD_INITIALIZER(dlog_data_##_name.dlog_wait_queue), \
	.dlog_cbuf = {0, }, \
}

static int dlog_release(struct inode *inode, struct file *file)
{
	struct dlog_data *dl_data = (struct dlog_data *)(file->private_data);
	return do_dlog(DLOG_ACTION_CLOSE, dl_data, NULL, 0);
}

static ssize_t dlog_read(struct file *file, char __user *buf,
		                         size_t count, loff_t *ppos)
{
	struct dlog_data *dl_data = (struct dlog_data *)(file->private_data);
	return do_dlog(DLOG_ACTION_READ_ALL, dl_data, buf, count);
}

loff_t dlog_llseek(struct file *file, loff_t offset, int whence)
{
	struct dlog_data *dl_data = (struct dlog_data *)(file->private_data);
	return (loff_t)do_dlog(DLOG_ACTION_SIZE_BUFFER, dl_data, 0, 0);
}

#define DEFINE_DLOG_OPERATION(_name) \
static int dlog_open_##_name(struct inode *inode, struct file *file) \
{ \
	file->f_flags |= O_NONBLOCK; \
	file->private_data = &dlog_data_##_name; \
	return do_dlog(DLOG_ACTION_OPEN, &dlog_data_##_name, NULL, 0); \
} \
\
static const struct file_operations dlog_##_name##_operations = { \
	.read           = dlog_read, \
	.open           = dlog_open_##_name, \
	.release        = dlog_release, \
	.llseek         = dlog_llseek, \
};

DEFINE_DLOG_VARIABLE(mm);
DEFINE_DLOG_VARIABLE(efs);
DEFINE_DLOG_VARIABLE(etc);

DEFINE_DLOG_OPERATION(mm);
DEFINE_DLOG_OPERATION(efs);
DEFINE_DLOG_OPERATION(etc);

static const char DLOG_VERSION_STR[] = "1.0.0";

static int dlog_version_show(struct seq_file *m, void *v)
{
	seq_printf(m, "%s\n", DLOG_VERSION_STR);
	return 0;
}

static int dlog_version_open(struct inode *inode, struct file *file)
{
	return single_open(file, dlog_version_show, NULL);
}

static const struct file_operations dlog_ver_operations = {
	.open           = dlog_version_open,
	.read           = seq_read,
	.llseek         = seq_lseek,
	.release        = single_release,
};

#define DEFINE_DLOG_CREATE(_name,log_name) \
static void dlog_create_##_name(struct proc_dir_entry *dlog_dir) \
{ \
	proc_create(#log_name, S_IRUSR|S_IRGRP, \
			dlog_dir, &dlog_##_name##_operations); \
}

DEFINE_DLOG_CREATE(mm,dlog_mm);
DEFINE_DLOG_CREATE(efs,dlog_efs);
DEFINE_DLOG_CREATE(etc,dlog_etc);

static int __init dlog_init(void)
{
	dlog_dir = proc_mkdir_mode("dlog", S_IRUSR|S_IRGRP, NULL);

	proc_create("dlog_version", S_IRUSR|S_IRGRP,
				dlog_dir, &dlog_ver_operations);

	dlog_create_mm(dlog_dir);
	dlog_create_efs(dlog_dir);
	dlog_create_etc(dlog_dir);

	return 0;
}
module_init(dlog_init);

/* human readable text of the record */
static char *dlog_text(const struct dlog_metadata *msg)
{
	return (char *)msg + sizeof(struct dlog_metadata);
}

static struct dlog_metadata *dlog_buf_from_idx(char *dlog_cbuf, u32 idx)
{
	struct dlog_metadata *msg = (struct dlog_metadata *)(dlog_cbuf + idx);

	if (!msg->len)
		return (struct dlog_metadata *)dlog_cbuf;
	return msg;
}

static u32 dlog_next(char *dlog_cbuf, u32 idx)
{
	struct dlog_metadata *msg = (struct dlog_metadata *)(dlog_cbuf + idx);

	if (!msg->len) {
		msg = (struct dlog_metadata *)dlog_cbuf;
		return msg->len;
	}
	return idx + msg->len;
}

static void dlog_buf_store(const char *text, u16 text_len,
					struct dlog_data *dl_data,
					struct task_struct *owner)
{
	struct dlog_metadata *msg;
	struct dlog_sequence *dlog_seq = &(dl_data->dlog_seq);
	wait_queue_head_t *dlog_wait_queue = &(dl_data->dlog_wait_queue);
	char *dlog_cbuf = dl_data->dlog_cbuf;
	u32 size, pad_len;
	struct task_struct *p = find_task_by_vpid(owner->tgid);

	/* number of '\0' padding bytes to next message */
	size = sizeof(struct dlog_metadata) + text_len;
	pad_len = (-size) & (DLOG_BUF_ALIGN - 1);
	size += pad_len;

	while (dlog_seq->dlog_first_seq < dlog_seq->dlog_next_seq) {
		u32 free;

		if (dlog_seq->dlog_next_idx > dlog_seq->dlog_first_idx)
			free = max(__DLOG_BUF_LEN - dlog_seq->dlog_next_idx,
							dlog_seq->dlog_first_idx);
		else
			free = dlog_seq->dlog_first_idx - dlog_seq->dlog_next_idx;

		if (free > size + sizeof(struct dlog_metadata))
			break;

		/* drop old messages until we have enough space */
		dlog_seq->dlog_first_idx = dlog_next(dlog_cbuf, dlog_seq->dlog_first_idx);
		dlog_seq->dlog_first_seq++;
	}

	if (dlog_seq->dlog_next_idx + size + sizeof(struct dlog_metadata) >= __DLOG_BUF_LEN) {
		memset(dlog_cbuf + dlog_seq->dlog_next_idx, 0, sizeof(struct dlog_metadata));
		dlog_seq->dlog_next_idx = 0;
	}

	/* fill message */
	msg = (struct dlog_metadata *)(dlog_cbuf + dlog_seq->dlog_next_idx);
	memcpy(dlog_text(msg), text, text_len);
	msg->text_len = text_len;
	msg->pid = owner->pid;
	memcpy(msg->comm, owner->comm, TASK_COMM_LEN);
	if (p) {
		msg->tgid = owner->tgid;
		memcpy(msg->tgid_comm, p->comm, TASK_COMM_LEN);
	} else {
		msg->tgid = 0;
		msg->tgid_comm[0] = 0;
	}
	msg->ts_nsec = local_clock();
	msg->len = sizeof(struct dlog_metadata) + text_len + pad_len;

	/* insert message */
	dlog_seq->dlog_next_idx += msg->len;
	dlog_seq->dlog_next_seq++;
	wake_up_interruptible(dlog_wait_queue);

}

static size_t dlog_print_time(u64 ts, char *buf)
{
	unsigned long rem_nsec;

	rem_nsec = do_div(ts, 1000000000);

	if (!buf)
		return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);

	return sprintf(buf, "[%5lu.%06lu] ",
			(unsigned long)ts, rem_nsec / 1000);
}

static size_t dlog_print_pid(const struct dlog_metadata *msg, char *buf)
{

	if (!buf)
		return snprintf(NULL, 0, "[%s(%d)|%s(%d)] ",
			msg->comm, msg->pid, msg->tgid_comm, msg->tgid);

	return sprintf(buf, "[%s(%d)|%s(%d)] ",
			msg->comm, msg->pid, msg->tgid_comm, msg->tgid);
}

static size_t dlog_print_prefix(const struct dlog_metadata *msg, char *buf)
{
	size_t len = 0;

	len += dlog_print_time(msg->ts_nsec, buf ? buf + len : NULL);
	len += dlog_print_pid(msg, buf ? buf + len : NULL);
	return len;
}

static size_t dlog_print_text(const struct dlog_metadata *msg, char *buf, size_t size)
{
	const char *text = dlog_text(msg);
	size_t text_size = msg->text_len;
	bool prefix = true;
	bool newline = true;
	size_t len = 0;

	do {
		const char *next = memchr(text, '\n', text_size);
		size_t text_len;

		if (next) {
			text_len = next - text;
			next++;
			text_size -= next - text;
		} else {
			text_len = text_size;
		}

		if (buf) {
			if (dlog_print_prefix(msg, NULL) + text_len + 1 >= size - len)
				break;

			if (prefix)
				len += dlog_print_prefix(msg, buf + len);
			memcpy(buf + len, text, text_len);
			len += text_len;
			if (next || newline)
				buf[len++] = '\n';
		} else {
			/*  buffer size only calculation */
			if (prefix)
				len += dlog_print_prefix(msg, NULL);
			len += text_len;
			if (next || newline)
				len++;
		}

		prefix = true;
		text = next;
	} while (text);

	return len;
}

static int dlog_print_all(struct dlog_data *dl_data, char __user *buf, int size)
{
	char *text;
	int len = 0;
	struct dlog_sequence *dlog_seq = &(dl_data->dlog_seq);
	spinlock_t *dlog_spinlock = &(dl_data->dlog_spinlock);
	char *dlog_cbuf = dl_data->dlog_cbuf;
	u64 seq = dlog_seq->dlog_next_seq;
	u32 idx = dlog_seq->dlog_next_idx;

	text = kmalloc(DLOG_BUF_LINE_MAX + S_PREFIX_MAX, GFP_KERNEL);
	if (!text)
		return -ENOMEM;
	spin_lock_irq(dlog_spinlock);

	if (dlog_seq->dlog_end_seq == -1)
		dlog_seq->dlog_end_seq = dlog_seq->dlog_next_seq;

	if (buf) {

		if (dlog_seq->dlog_clear_seq < dlog_seq->dlog_first_seq) {
			/* messages are gone, move to first available one */
			dlog_seq->dlog_clear_seq = dlog_seq->dlog_first_seq;
			dlog_seq->dlog_clear_idx = dlog_seq->dlog_first_idx;
		}

		seq = dlog_seq->dlog_clear_seq;
		idx = dlog_seq->dlog_clear_idx;

		while (seq < dlog_seq->dlog_end_seq) {
			struct dlog_metadata *msg =
					dlog_buf_from_idx(dlog_cbuf, idx);
			int textlen;

			textlen = dlog_print_text(msg, text,
						 DLOG_BUF_LINE_MAX + S_PREFIX_MAX);
			if (textlen < 0) {
				len = textlen;
				break;
			} else if(len + textlen > size) {
				break;
			}
			idx = dlog_next(dlog_cbuf, idx);
			seq++;

			spin_unlock_irq(dlog_spinlock);
			if (copy_to_user(buf + len, text, textlen))
				len = -EFAULT;
			else
				len += textlen;
			spin_lock_irq(dlog_spinlock);

			if (seq < dlog_seq->dlog_first_seq) {
				/* messages are gone, move to next one */
				seq = dlog_seq->dlog_first_seq;
				idx = dlog_seq->dlog_first_idx;
			}
		}
	}

	dlog_seq->dlog_clear_seq = seq;
	dlog_seq->dlog_clear_idx = idx;

	spin_unlock_irq(dlog_spinlock);

	kfree(text);
	return len;
}

static int do_dlog(int type, struct dlog_data *dl_data, char __user *buf, int len)
{
	int error=0;
	struct dlog_sequence *dlog_seq = &(dl_data->dlog_seq);

	switch (type) {
	case DLOG_ACTION_CLOSE:	/* Close log */
		break;
	case DLOG_ACTION_OPEN:	/* Open log */

		break;
	case DLOG_ACTION_READ_ALL: /* cat /proc/dlog */ /* dumpstate */
		error = -EINVAL;
		if (!buf || len < 0)
			goto out;
		error = 0;
		if (!len)
			goto out;
		if (!access_ok(VERIFY_WRITE, buf, len)) {
			error = -EFAULT;
			goto out;
		}

		error = dlog_print_all(dl_data, buf, len);
		if (error == 0) {
			dlog_seq->dlog_clear_seq = dlog_seq->dlog_first_seq;
			dlog_seq->dlog_clear_idx = dlog_seq->dlog_first_idx;
			dlog_seq->dlog_end_seq = -1;
		}

		break;
	/* Size of the log buffer */
	case DLOG_ACTION_SIZE_BUFFER:
		error = __DLOG_BUF_LEN;
		break;
	default:
		error = -EINVAL;
		break;
	}
out:
	return error;
}

static int vdlog(struct dlog_data *dl_data, const char *fmt, va_list args)
{
	static char textbuf[DLOG_BUF_LINE_MAX];
	char *text = textbuf;
	size_t text_len;
	unsigned long flags;
	int printed_len = 0;
	bool stored = false;
	spinlock_t *dlog_spinlock = &(dl_data->dlog_spinlock);

	local_irq_save(flags);

	spin_lock(dlog_spinlock);

	text_len = vscnprintf(text, sizeof(textbuf), fmt, args);

	/* mark and strip a trailing newline */
	if (text_len && text[text_len-1] == '\n')
		text_len--;

	if (!stored)
		dlog_buf_store(text, text_len, dl_data, current);

	printed_len += text_len;

	spin_unlock(dlog_spinlock);
	local_irq_restore(flags);


	return printed_len;
}

/*
 * dlog - print a deleted entry message
 * @fmt: format string
 */
#define DEFINE_DLOG(_name)\
int dlog_##_name(const char *fmt, ...)\
{\
	va_list args;\
	int r;\
\
	va_start(args, fmt);\
	r = vdlog(&dlog_data_##_name, fmt, args); \
\
	va_end(args);\
\
	return r;\
}

DEFINE_DLOG(mm);
DEFINE_DLOG(efs);
DEFINE_DLOG(etc);