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