1 /*
2  * drivers/misc/logger.c
3  *
4  * A Logging Subsystem
5  *
6  * Copyright (C) 2007-2008 Google, Inc.
7  *
8  * Robert Love <rlove@google.com>
9  *
10  * This software is licensed under the terms of the GNU General Public
11  * License version 2, as published by the Free Software Foundation, and
12  * may be copied, distributed, and modified under those terms.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 #include <linux/sched.h>
21 #include <linux/module.h>
22 #include <linux/fs.h>
23 #include <linux/miscdevice.h>
24 #include <linux/uaccess.h>
25 #include <linux/poll.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include "logger.h"
29 
30 #include <asm/ioctls.h>
31 
32 /*
33  * struct logger_log - represents a specific log, such as 'main' or 'radio'
34  *
35  * This structure lives from module insertion until module removal, so it does
36  * not need additional reference counting. The structure is protected by the
37  * mutex 'mutex'.
38  */
39 struct logger_log {
40 	unsigned char		*buffer;/* the ring buffer itself */
41 	struct miscdevice	misc;	/* misc device representing the log */
42 	wait_queue_head_t	wq;	/* wait queue for readers */
43 	struct list_head	readers; /* this log's readers */
44 	struct mutex		mutex;	/* mutex protecting buffer */
45 	size_t			w_off;	/* current write head offset */
46 	size_t			head;	/* new readers start here */
47 	size_t			size;	/* size of the log */
48 };
49 
50 /*
51  * struct logger_reader - a logging device open for reading
52  *
53  * This object lives from open to release, so we don't need additional
54  * reference counting. The structure is protected by log->mutex.
55  */
56 struct logger_reader {
57 	struct logger_log	*log;	/* associated log */
58 	struct list_head	list;	/* entry in logger_log's list */
59 	size_t			r_off;	/* current read head offset */
60 };
61 
62 /* logger_offset - returns index 'n' into the log via (optimized) modulus */
63 #define logger_offset(n)	((n) & (log->size - 1))
64 
65 /*
66  * file_get_log - Given a file structure, return the associated log
67  *
68  * This isn't aesthetic. We have several goals:
69  *
70  *	1) Need to quickly obtain the associated log during an I/O operation
71  *	2) Readers need to maintain state (logger_reader)
72  *	3) Writers need to be very fast (open() should be a near no-op)
73  *
74  * In the reader case, we can trivially go file->logger_reader->logger_log.
75  * For a writer, we don't want to maintain a logger_reader, so we just go
76  * file->logger_log. Thus what file->private_data points at depends on whether
77  * or not the file was opened for reading. This function hides that dirtiness.
78  */
file_get_log(struct file * file)79 static inline struct logger_log *file_get_log(struct file *file)
80 {
81 	if (file->f_mode & FMODE_READ) {
82 		struct logger_reader *reader = file->private_data;
83 		return reader->log;
84 	} else
85 		return file->private_data;
86 }
87 
88 /*
89  * get_entry_len - Grabs the length of the payload of the next entry starting
90  * from 'off'.
91  *
92  * Caller needs to hold log->mutex.
93  */
get_entry_len(struct logger_log * log,size_t off)94 static __u32 get_entry_len(struct logger_log *log, size_t off)
95 {
96 	__u16 val;
97 
98 	switch (log->size - off) {
99 	case 1:
100 		memcpy(&val, log->buffer + off, 1);
101 		memcpy(((char *) &val) + 1, log->buffer, 1);
102 		break;
103 	default:
104 		memcpy(&val, log->buffer + off, 2);
105 	}
106 
107 	return sizeof(struct logger_entry) + val;
108 }
109 
110 /*
111  * do_read_log_to_user - reads exactly 'count' bytes from 'log' into the
112  * user-space buffer 'buf'. Returns 'count' on success.
113  *
114  * Caller must hold log->mutex.
115  */
do_read_log_to_user(struct logger_log * log,struct logger_reader * reader,char __user * buf,size_t count)116 static ssize_t do_read_log_to_user(struct logger_log *log,
117 				   struct logger_reader *reader,
118 				   char __user *buf,
119 				   size_t count)
120 {
121 	size_t len;
122 
123 	/*
124 	 * We read from the log in two disjoint operations. First, we read from
125 	 * the current read head offset up to 'count' bytes or to the end of
126 	 * the log, whichever comes first.
127 	 */
128 	len = min(count, log->size - reader->r_off);
129 	if (copy_to_user(buf, log->buffer + reader->r_off, len))
130 		return -EFAULT;
131 
132 	/*
133 	 * Second, we read any remaining bytes, starting back at the head of
134 	 * the log.
135 	 */
136 	if (count != len)
137 		if (copy_to_user(buf + len, log->buffer, count - len))
138 			return -EFAULT;
139 
140 	reader->r_off = logger_offset(reader->r_off + count);
141 
142 	return count;
143 }
144 
145 /*
146  * logger_read - our log's read() method
147  *
148  * Behavior:
149  *
150  *	- O_NONBLOCK works
151  *	- If there are no log entries to read, blocks until log is written to
152  *	- Atomically reads exactly one log entry
153  *
154  * Optimal read size is LOGGER_ENTRY_MAX_LEN. Will set errno to EINVAL if read
155  * buffer is insufficient to hold next entry.
156  */
logger_read(struct file * file,char __user * buf,size_t count,loff_t * pos)157 static ssize_t logger_read(struct file *file, char __user *buf,
158 			   size_t count, loff_t *pos)
159 {
160 	struct logger_reader *reader = file->private_data;
161 	struct logger_log *log = reader->log;
162 	ssize_t ret;
163 	DEFINE_WAIT(wait);
164 
165 start:
166 	while (1) {
167 		prepare_to_wait(&log->wq, &wait, TASK_INTERRUPTIBLE);
168 
169 		mutex_lock(&log->mutex);
170 		ret = (log->w_off == reader->r_off);
171 		mutex_unlock(&log->mutex);
172 		if (!ret)
173 			break;
174 
175 		if (file->f_flags & O_NONBLOCK) {
176 			ret = -EAGAIN;
177 			break;
178 		}
179 
180 		if (signal_pending(current)) {
181 			ret = -EINTR;
182 			break;
183 		}
184 
185 		schedule();
186 	}
187 
188 	finish_wait(&log->wq, &wait);
189 	if (ret)
190 		return ret;
191 
192 	mutex_lock(&log->mutex);
193 
194 	/* is there still something to read or did we race? */
195 	if (unlikely(log->w_off == reader->r_off)) {
196 		mutex_unlock(&log->mutex);
197 		goto start;
198 	}
199 
200 	/* get the size of the next entry */
201 	ret = get_entry_len(log, reader->r_off);
202 	if (count < ret) {
203 		ret = -EINVAL;
204 		goto out;
205 	}
206 
207 	/* get exactly one entry from the log */
208 	ret = do_read_log_to_user(log, reader, buf, ret);
209 
210 out:
211 	mutex_unlock(&log->mutex);
212 
213 	return ret;
214 }
215 
216 /*
217  * get_next_entry - return the offset of the first valid entry at least 'len'
218  * bytes after 'off'.
219  *
220  * Caller must hold log->mutex.
221  */
get_next_entry(struct logger_log * log,size_t off,size_t len)222 static size_t get_next_entry(struct logger_log *log, size_t off, size_t len)
223 {
224 	size_t count = 0;
225 
226 	do {
227 		size_t nr = get_entry_len(log, off);
228 		off = logger_offset(off + nr);
229 		count += nr;
230 	} while (count < len);
231 
232 	return off;
233 }
234 
235 /*
236  * clock_interval - is a < c < b in mod-space? Put another way, does the line
237  * from a to b cross c?
238  */
clock_interval(size_t a,size_t b,size_t c)239 static inline int clock_interval(size_t a, size_t b, size_t c)
240 {
241 	if (b < a) {
242 		if (a < c || b >= c)
243 			return 1;
244 	} else {
245 		if (a < c && b >= c)
246 			return 1;
247 	}
248 
249 	return 0;
250 }
251 
252 /*
253  * fix_up_readers - walk the list of all readers and "fix up" any who were
254  * lapped by the writer; also do the same for the default "start head".
255  * We do this by "pulling forward" the readers and start head to the first
256  * entry after the new write head.
257  *
258  * The caller needs to hold log->mutex.
259  */
fix_up_readers(struct logger_log * log,size_t len)260 static void fix_up_readers(struct logger_log *log, size_t len)
261 {
262 	size_t old = log->w_off;
263 	size_t new = logger_offset(old + len);
264 	struct logger_reader *reader;
265 
266 	if (clock_interval(old, new, log->head))
267 		log->head = get_next_entry(log, log->head, len);
268 
269 	list_for_each_entry(reader, &log->readers, list)
270 		if (clock_interval(old, new, reader->r_off))
271 			reader->r_off = get_next_entry(log, reader->r_off, len);
272 }
273 
274 /*
275  * do_write_log - writes 'len' bytes from 'buf' to 'log'
276  *
277  * The caller needs to hold log->mutex.
278  */
do_write_log(struct logger_log * log,const void * buf,size_t count)279 static void do_write_log(struct logger_log *log, const void *buf, size_t count)
280 {
281 	size_t len;
282 
283 	len = min(count, log->size - log->w_off);
284 	memcpy(log->buffer + log->w_off, buf, len);
285 
286 	if (count != len)
287 		memcpy(log->buffer, buf + len, count - len);
288 
289 	log->w_off = logger_offset(log->w_off + count);
290 
291 }
292 
293 /*
294  * do_write_log_user - writes 'len' bytes from the user-space buffer 'buf' to
295  * the log 'log'
296  *
297  * The caller needs to hold log->mutex.
298  *
299  * Returns 'count' on success, negative error code on failure.
300  */
do_write_log_from_user(struct logger_log * log,const void __user * buf,size_t count)301 static ssize_t do_write_log_from_user(struct logger_log *log,
302 				      const void __user *buf, size_t count)
303 {
304 	size_t len;
305 
306 	len = min(count, log->size - log->w_off);
307 	if (len && copy_from_user(log->buffer + log->w_off, buf, len))
308 		return -EFAULT;
309 
310 	if (count != len)
311 		if (copy_from_user(log->buffer, buf + len, count - len))
312 			return -EFAULT;
313 
314 	log->w_off = logger_offset(log->w_off + count);
315 
316 	return count;
317 }
318 
319 /*
320  * logger_aio_write - our write method, implementing support for write(),
321  * writev(), and aio_write(). Writes are our fast path, and we try to optimize
322  * them above all else.
323  */
logger_aio_write(struct kiocb * iocb,const struct iovec * iov,unsigned long nr_segs,loff_t ppos)324 ssize_t logger_aio_write(struct kiocb *iocb, const struct iovec *iov,
325 			 unsigned long nr_segs, loff_t ppos)
326 {
327 	struct logger_log *log = file_get_log(iocb->ki_filp);
328 	size_t orig = log->w_off;
329 	struct logger_entry header;
330 	struct timespec now;
331 	ssize_t ret = 0;
332 
333 	now = current_kernel_time();
334 
335 	header.pid = current->tgid;
336 	header.tid = current->pid;
337 	header.sec = now.tv_sec;
338 	header.nsec = now.tv_nsec;
339 	header.len = min_t(size_t, iocb->ki_left, LOGGER_ENTRY_MAX_PAYLOAD);
340 
341 	/* null writes succeed, return zero */
342 	if (unlikely(!header.len))
343 		return 0;
344 
345 	mutex_lock(&log->mutex);
346 
347 	/*
348 	 * Fix up any readers, pulling them forward to the first readable
349 	 * entry after (what will be) the new write offset. We do this now
350 	 * because if we partially fail, we can end up with clobbered log
351 	 * entries that encroach on readable buffer.
352 	 */
353 	fix_up_readers(log, sizeof(struct logger_entry) + header.len);
354 
355 	do_write_log(log, &header, sizeof(struct logger_entry));
356 
357 	while (nr_segs-- > 0) {
358 		size_t len;
359 		ssize_t nr;
360 
361 		/* figure out how much of this vector we can keep */
362 		len = min_t(size_t, iov->iov_len, header.len - ret);
363 
364 		/* write out this segment's payload */
365 		nr = do_write_log_from_user(log, iov->iov_base, len);
366 		if (unlikely(nr < 0)) {
367 			log->w_off = orig;
368 			mutex_unlock(&log->mutex);
369 			return nr;
370 		}
371 
372 		iov++;
373 		ret += nr;
374 	}
375 
376 	mutex_unlock(&log->mutex);
377 
378 	/* wake up any blocked readers */
379 	wake_up_interruptible(&log->wq);
380 
381 	return ret;
382 }
383 
384 static struct logger_log *get_log_from_minor(int);
385 
386 /*
387  * logger_open - the log's open() file operation
388  *
389  * Note how near a no-op this is in the write-only case. Keep it that way!
390  */
logger_open(struct inode * inode,struct file * file)391 static int logger_open(struct inode *inode, struct file *file)
392 {
393 	struct logger_log *log;
394 	int ret;
395 
396 	ret = nonseekable_open(inode, file);
397 	if (ret)
398 		return ret;
399 
400 	log = get_log_from_minor(MINOR(inode->i_rdev));
401 	if (!log)
402 		return -ENODEV;
403 
404 	if (file->f_mode & FMODE_READ) {
405 		struct logger_reader *reader;
406 
407 		reader = kmalloc(sizeof(struct logger_reader), GFP_KERNEL);
408 		if (!reader)
409 			return -ENOMEM;
410 
411 		reader->log = log;
412 		INIT_LIST_HEAD(&reader->list);
413 
414 		mutex_lock(&log->mutex);
415 		reader->r_off = log->head;
416 		list_add_tail(&reader->list, &log->readers);
417 		mutex_unlock(&log->mutex);
418 
419 		file->private_data = reader;
420 	} else
421 		file->private_data = log;
422 
423 	return 0;
424 }
425 
426 /*
427  * logger_release - the log's release file operation
428  *
429  * Note this is a total no-op in the write-only case. Keep it that way!
430  */
logger_release(struct inode * ignored,struct file * file)431 static int logger_release(struct inode *ignored, struct file *file)
432 {
433 	if (file->f_mode & FMODE_READ) {
434 		struct logger_reader *reader = file->private_data;
435 		list_del(&reader->list);
436 		kfree(reader);
437 	}
438 
439 	return 0;
440 }
441 
442 /*
443  * logger_poll - the log's poll file operation, for poll/select/epoll
444  *
445  * Note we always return POLLOUT, because you can always write() to the log.
446  * Note also that, strictly speaking, a return value of POLLIN does not
447  * guarantee that the log is readable without blocking, as there is a small
448  * chance that the writer can lap the reader in the interim between poll()
449  * returning and the read() request.
450  */
logger_poll(struct file * file,poll_table * wait)451 static unsigned int logger_poll(struct file *file, poll_table *wait)
452 {
453 	struct logger_reader *reader;
454 	struct logger_log *log;
455 	unsigned int ret = POLLOUT | POLLWRNORM;
456 
457 	if (!(file->f_mode & FMODE_READ))
458 		return ret;
459 
460 	reader = file->private_data;
461 	log = reader->log;
462 
463 	poll_wait(file, &log->wq, wait);
464 
465 	mutex_lock(&log->mutex);
466 	if (log->w_off != reader->r_off)
467 		ret |= POLLIN | POLLRDNORM;
468 	mutex_unlock(&log->mutex);
469 
470 	return ret;
471 }
472 
logger_ioctl(struct file * file,unsigned int cmd,unsigned long arg)473 static long logger_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
474 {
475 	struct logger_log *log = file_get_log(file);
476 	struct logger_reader *reader;
477 	long ret = -ENOTTY;
478 
479 	mutex_lock(&log->mutex);
480 
481 	switch (cmd) {
482 	case LOGGER_GET_LOG_BUF_SIZE:
483 		ret = log->size;
484 		break;
485 	case LOGGER_GET_LOG_LEN:
486 		if (!(file->f_mode & FMODE_READ)) {
487 			ret = -EBADF;
488 			break;
489 		}
490 		reader = file->private_data;
491 		if (log->w_off >= reader->r_off)
492 			ret = log->w_off - reader->r_off;
493 		else
494 			ret = (log->size - reader->r_off) + log->w_off;
495 		break;
496 	case LOGGER_GET_NEXT_ENTRY_LEN:
497 		if (!(file->f_mode & FMODE_READ)) {
498 			ret = -EBADF;
499 			break;
500 		}
501 		reader = file->private_data;
502 		if (log->w_off != reader->r_off)
503 			ret = get_entry_len(log, reader->r_off);
504 		else
505 			ret = 0;
506 		break;
507 	case LOGGER_FLUSH_LOG:
508 		if (!(file->f_mode & FMODE_WRITE)) {
509 			ret = -EBADF;
510 			break;
511 		}
512 		list_for_each_entry(reader, &log->readers, list)
513 			reader->r_off = log->w_off;
514 		log->head = log->w_off;
515 		ret = 0;
516 		break;
517 	}
518 
519 	mutex_unlock(&log->mutex);
520 
521 	return ret;
522 }
523 
524 static const struct file_operations logger_fops = {
525 	.owner = THIS_MODULE,
526 	.read = logger_read,
527 	.aio_write = logger_aio_write,
528 	.poll = logger_poll,
529 	.unlocked_ioctl = logger_ioctl,
530 	.compat_ioctl = logger_ioctl,
531 	.open = logger_open,
532 	.release = logger_release,
533 };
534 
535 /*
536  * Defines a log structure with name 'NAME' and a size of 'SIZE' bytes, which
537  * must be a power of two, greater than LOGGER_ENTRY_MAX_LEN, and less than
538  * LONG_MAX minus LOGGER_ENTRY_MAX_LEN.
539  */
540 #define DEFINE_LOGGER_DEVICE(VAR, NAME, SIZE) \
541 static unsigned char _buf_ ## VAR[SIZE]; \
542 static struct logger_log VAR = { \
543 	.buffer = _buf_ ## VAR, \
544 	.misc = { \
545 		.minor = MISC_DYNAMIC_MINOR, \
546 		.name = NAME, \
547 		.fops = &logger_fops, \
548 		.parent = NULL, \
549 	}, \
550 	.wq = __WAIT_QUEUE_HEAD_INITIALIZER(VAR .wq), \
551 	.readers = LIST_HEAD_INIT(VAR .readers), \
552 	.mutex = __MUTEX_INITIALIZER(VAR .mutex), \
553 	.w_off = 0, \
554 	.head = 0, \
555 	.size = SIZE, \
556 };
557 
558 DEFINE_LOGGER_DEVICE(log_main, LOGGER_LOG_MAIN, 256*1024)
559 DEFINE_LOGGER_DEVICE(log_events, LOGGER_LOG_EVENTS, 256*1024)
560 DEFINE_LOGGER_DEVICE(log_radio, LOGGER_LOG_RADIO, 256*1024)
561 DEFINE_LOGGER_DEVICE(log_system, LOGGER_LOG_SYSTEM, 256*1024)
562 
get_log_from_minor(int minor)563 static struct logger_log *get_log_from_minor(int minor)
564 {
565 	if (log_main.misc.minor == minor)
566 		return &log_main;
567 	if (log_events.misc.minor == minor)
568 		return &log_events;
569 	if (log_radio.misc.minor == minor)
570 		return &log_radio;
571 	if (log_system.misc.minor == minor)
572 		return &log_system;
573 	return NULL;
574 }
575 
init_log(struct logger_log * log)576 static int __init init_log(struct logger_log *log)
577 {
578 	int ret;
579 
580 	ret = misc_register(&log->misc);
581 	if (unlikely(ret)) {
582 		printk(KERN_ERR "logger: failed to register misc "
583 		       "device for log '%s'!\n", log->misc.name);
584 		return ret;
585 	}
586 
587 	printk(KERN_INFO "logger: created %luK log '%s'\n",
588 	       (unsigned long) log->size >> 10, log->misc.name);
589 
590 	return 0;
591 }
592 
logger_init(void)593 static int __init logger_init(void)
594 {
595 	int ret;
596 
597 	ret = init_log(&log_main);
598 	if (unlikely(ret))
599 		goto out;
600 
601 	ret = init_log(&log_events);
602 	if (unlikely(ret))
603 		goto out;
604 
605 	ret = init_log(&log_radio);
606 	if (unlikely(ret))
607 		goto out;
608 
609 	ret = init_log(&log_system);
610 	if (unlikely(ret))
611 		goto out;
612 
613 out:
614 	return ret;
615 }
616 device_initcall(logger_init);
617