Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.10.11.
   1/*
   2 *  linux/kernel/printk.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 *
   6 * Modified to make sys_syslog() more flexible: added commands to
   7 * return the last 4k of kernel messages, regardless of whether
   8 * they've been read or not.  Added option to suppress kernel printk's
   9 * to the console.  Added hook for sending the console messages
  10 * elsewhere, in preparation for a serial line console (someday).
  11 * Ted Ts'o, 2/11/93.
  12 * Modified for sysctl support, 1/8/97, Chris Horn.
  13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
  14 *     manfred@colorfullife.com
  15 * Rewrote bits to get rid of console_lock
  16 *	01Mar01 Andrew Morton
  17 */
  18
  19#include <linux/kernel.h>
  20#include <linux/mm.h>
  21#include <linux/tty.h>
  22#include <linux/tty_driver.h>
  23#include <linux/console.h>
  24#include <linux/init.h>
  25#include <linux/jiffies.h>
  26#include <linux/nmi.h>
  27#include <linux/module.h>
  28#include <linux/moduleparam.h>
  29#include <linux/interrupt.h>			/* For in_interrupt() */
  30#include <linux/delay.h>
  31#include <linux/smp.h>
  32#include <linux/security.h>
  33#include <linux/bootmem.h>
  34#include <linux/memblock.h>
  35#include <linux/syscalls.h>
  36#include <linux/kexec.h>
  37#include <linux/kdb.h>
  38#include <linux/ratelimit.h>
  39#include <linux/kmsg_dump.h>
  40#include <linux/syslog.h>
  41#include <linux/cpu.h>
  42#include <linux/notifier.h>
  43#include <linux/rculist.h>
  44#include <linux/poll.h>
  45
  46#include <asm/uaccess.h>
  47
  48#define CREATE_TRACE_POINTS
  49#include <trace/events/printk.h>
  50
  51/*
  52 * Architectures can override it:
  53 */
  54void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
  55{
  56}
  57
  58/* printk's without a loglevel use this.. */
  59#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
  60
  61/* We show everything that is MORE important than this.. */
  62#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
  63#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
  64
  65DECLARE_WAIT_QUEUE_HEAD(log_wait);
  66
  67int console_printk[4] = {
  68	DEFAULT_CONSOLE_LOGLEVEL,	/* console_loglevel */
  69	DEFAULT_MESSAGE_LOGLEVEL,	/* default_message_loglevel */
  70	MINIMUM_CONSOLE_LOGLEVEL,	/* minimum_console_loglevel */
  71	DEFAULT_CONSOLE_LOGLEVEL,	/* default_console_loglevel */
  72};
  73
  74/*
  75 * Low level drivers may need that to know if they can schedule in
  76 * their unblank() callback or not. So let's export it.
  77 */
  78int oops_in_progress;
  79EXPORT_SYMBOL(oops_in_progress);
  80
  81/*
  82 * console_sem protects the console_drivers list, and also
  83 * provides serialisation for access to the entire console
  84 * driver system.
  85 */
  86static DEFINE_SEMAPHORE(console_sem);
  87struct console *console_drivers;
  88EXPORT_SYMBOL_GPL(console_drivers);
  89
  90/*
  91 * This is used for debugging the mess that is the VT code by
  92 * keeping track if we have the console semaphore held. It's
  93 * definitely not the perfect debug tool (we don't know if _WE_
  94 * hold it are racing, but it helps tracking those weird code
  95 * path in the console code where we end up in places I want
  96 * locked without the console sempahore held
  97 */
  98static int console_locked, console_suspended;
  99
 100/*
 101 * If exclusive_console is non-NULL then only this console is to be printed to.
 102 */
 103static struct console *exclusive_console;
 104
 105/*
 106 *	Array of consoles built from command line options (console=)
 107 */
 108struct console_cmdline
 109{
 110	char	name[8];			/* Name of the driver	    */
 111	int	index;				/* Minor dev. to use	    */
 112	char	*options;			/* Options for the driver   */
 113#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
 114	char	*brl_options;			/* Options for braille driver */
 115#endif
 116};
 117
 118#define MAX_CMDLINECONSOLES 8
 119
 120static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
 121static int selected_console = -1;
 122static int preferred_console = -1;
 123int console_set_on_cmdline;
 124EXPORT_SYMBOL(console_set_on_cmdline);
 125
 126/* Flag: console code may call schedule() */
 127static int console_may_schedule;
 128
 129/*
 130 * The printk log buffer consists of a chain of concatenated variable
 131 * length records. Every record starts with a record header, containing
 132 * the overall length of the record.
 133 *
 134 * The heads to the first and last entry in the buffer, as well as the
 135 * sequence numbers of these both entries are maintained when messages
 136 * are stored..
 137 *
 138 * If the heads indicate available messages, the length in the header
 139 * tells the start next message. A length == 0 for the next message
 140 * indicates a wrap-around to the beginning of the buffer.
 141 *
 142 * Every record carries the monotonic timestamp in microseconds, as well as
 143 * the standard userspace syslog level and syslog facility. The usual
 144 * kernel messages use LOG_KERN; userspace-injected messages always carry
 145 * a matching syslog facility, by default LOG_USER. The origin of every
 146 * message can be reliably determined that way.
 147 *
 148 * The human readable log message directly follows the message header. The
 149 * length of the message text is stored in the header, the stored message
 150 * is not terminated.
 151 *
 152 * Optionally, a message can carry a dictionary of properties (key/value pairs),
 153 * to provide userspace with a machine-readable message context.
 154 *
 155 * Examples for well-defined, commonly used property names are:
 156 *   DEVICE=b12:8               device identifier
 157 *                                b12:8         block dev_t
 158 *                                c127:3        char dev_t
 159 *                                n8            netdev ifindex
 160 *                                +sound:card0  subsystem:devname
 161 *   SUBSYSTEM=pci              driver-core subsystem name
 162 *
 163 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
 164 * follows directly after a '=' character. Every property is terminated by
 165 * a '\0' character. The last property is not terminated.
 166 *
 167 * Example of a message structure:
 168 *   0000  ff 8f 00 00 00 00 00 00      monotonic time in nsec
 169 *   0008  34 00                        record is 52 bytes long
 170 *   000a        0b 00                  text is 11 bytes long
 171 *   000c              1f 00            dictionary is 23 bytes long
 172 *   000e                    03 00      LOG_KERN (facility) LOG_ERR (level)
 173 *   0010  69 74 27 73 20 61 20 6c      "it's a l"
 174 *         69 6e 65                     "ine"
 175 *   001b           44 45 56 49 43      "DEVIC"
 176 *         45 3d 62 38 3a 32 00 44      "E=b8:2\0D"
 177 *         52 49 56 45 52 3d 62 75      "RIVER=bu"
 178 *         67                           "g"
 179 *   0032     00 00 00                  padding to next message header
 180 *
 181 * The 'struct log' buffer header must never be directly exported to
 182 * userspace, it is a kernel-private implementation detail that might
 183 * need to be changed in the future, when the requirements change.
 184 *
 185 * /dev/kmsg exports the structured data in the following line format:
 186 *   "level,sequnum,timestamp;<message text>\n"
 187 *
 188 * The optional key/value pairs are attached as continuation lines starting
 189 * with a space character and terminated by a newline. All possible
 190 * non-prinatable characters are escaped in the "\xff" notation.
 191 *
 192 * Users of the export format should ignore possible additional values
 193 * separated by ',', and find the message after the ';' character.
 194 */
 195
 196enum log_flags {
 197	LOG_NOCONS	= 1,	/* already flushed, do not print to console */
 198	LOG_NEWLINE	= 2,	/* text ended with a newline */
 199	LOG_PREFIX	= 4,	/* text started with a prefix */
 200	LOG_CONT	= 8,	/* text is a fragment of a continuation line */
 201};
 202
 203struct log {
 204	u64 ts_nsec;		/* timestamp in nanoseconds */
 205	u16 len;		/* length of entire record */
 206	u16 text_len;		/* length of text buffer */
 207	u16 dict_len;		/* length of dictionary buffer */
 208	u8 facility;		/* syslog facility */
 209	u8 flags:5;		/* internal record flags */
 210	u8 level:3;		/* syslog level */
 211};
 212
 213/*
 214 * The logbuf_lock protects kmsg buffer, indices, counters. It is also
 215 * used in interesting ways to provide interlocking in console_unlock();
 216 */
 217static DEFINE_RAW_SPINLOCK(logbuf_lock);
 218
 219/* the next printk record to read by syslog(READ) or /proc/kmsg */
 220static u64 syslog_seq;
 221static u32 syslog_idx;
 222static enum log_flags syslog_prev;
 223static size_t syslog_partial;
 224
 225/* index and sequence number of the first record stored in the buffer */
 226static u64 log_first_seq;
 227static u32 log_first_idx;
 228
 229/* index and sequence number of the next record to store in the buffer */
 230static u64 log_next_seq;
 231#ifdef CONFIG_PRINTK
 232static u32 log_next_idx;
 233
 234/* the next printk record to read after the last 'clear' command */
 235static u64 clear_seq;
 236static u32 clear_idx;
 237
 238#define PREFIX_MAX		32
 239#define LOG_LINE_MAX		1024 - PREFIX_MAX
 240
 241/* record buffer */
 242#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
 243#define LOG_ALIGN 4
 244#else
 245#define LOG_ALIGN __alignof__(struct log)
 246#endif
 247#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
 248static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
 249static char *log_buf = __log_buf;
 250static u32 log_buf_len = __LOG_BUF_LEN;
 251
 252/* cpu currently holding logbuf_lock */
 253static volatile unsigned int logbuf_cpu = UINT_MAX;
 254
 255/* human readable text of the record */
 256static char *log_text(const struct log *msg)
 257{
 258	return (char *)msg + sizeof(struct log);
 259}
 260
 261/* optional key/value pair dictionary attached to the record */
 262static char *log_dict(const struct log *msg)
 263{
 264	return (char *)msg + sizeof(struct log) + msg->text_len;
 265}
 266
 267/* get record by index; idx must point to valid msg */
 268static struct log *log_from_idx(u32 idx)
 269{
 270	struct log *msg = (struct log *)(log_buf + idx);
 271
 272	/*
 273	 * A length == 0 record is the end of buffer marker. Wrap around and
 274	 * read the message at the start of the buffer.
 275	 */
 276	if (!msg->len)
 277		return (struct log *)log_buf;
 278	return msg;
 279}
 280
 281/* get next record; idx must point to valid msg */
 282static u32 log_next(u32 idx)
 283{
 284	struct log *msg = (struct log *)(log_buf + idx);
 285
 286	/* length == 0 indicates the end of the buffer; wrap */
 287	/*
 288	 * A length == 0 record is the end of buffer marker. Wrap around and
 289	 * read the message at the start of the buffer as *this* one, and
 290	 * return the one after that.
 291	 */
 292	if (!msg->len) {
 293		msg = (struct log *)log_buf;
 294		return msg->len;
 295	}
 296	return idx + msg->len;
 297}
 298
 299/* insert record into the buffer, discard old ones, update heads */
 300static void log_store(int facility, int level,
 301		      enum log_flags flags, u64 ts_nsec,
 302		      const char *dict, u16 dict_len,
 303		      const char *text, u16 text_len)
 304{
 305	struct log *msg;
 306	u32 size, pad_len;
 307
 308	/* number of '\0' padding bytes to next message */
 309	size = sizeof(struct log) + text_len + dict_len;
 310	pad_len = (-size) & (LOG_ALIGN - 1);
 311	size += pad_len;
 312
 313	while (log_first_seq < log_next_seq) {
 314		u32 free;
 315
 316		if (log_next_idx > log_first_idx)
 317			free = max(log_buf_len - log_next_idx, log_first_idx);
 318		else
 319			free = log_first_idx - log_next_idx;
 320
 321		if (free > size + sizeof(struct log))
 322			break;
 323
 324		/* drop old messages until we have enough contiuous space */
 325		log_first_idx = log_next(log_first_idx);
 326		log_first_seq++;
 327	}
 328
 329	if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
 330		/*
 331		 * This message + an additional empty header does not fit
 332		 * at the end of the buffer. Add an empty header with len == 0
 333		 * to signify a wrap around.
 334		 */
 335		memset(log_buf + log_next_idx, 0, sizeof(struct log));
 336		log_next_idx = 0;
 337	}
 338
 339	/* fill message */
 340	msg = (struct log *)(log_buf + log_next_idx);
 341	memcpy(log_text(msg), text, text_len);
 342	msg->text_len = text_len;
 343	memcpy(log_dict(msg), dict, dict_len);
 344	msg->dict_len = dict_len;
 345	msg->facility = facility;
 346	msg->level = level & 7;
 347	msg->flags = flags & 0x1f;
 348	if (ts_nsec > 0)
 349		msg->ts_nsec = ts_nsec;
 350	else
 351		msg->ts_nsec = local_clock();
 352	memset(log_dict(msg) + dict_len, 0, pad_len);
 353	msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
 354
 355	/* insert message */
 356	log_next_idx += msg->len;
 357	log_next_seq++;
 358}
 359
 360/* /dev/kmsg - userspace message inject/listen interface */
 361struct devkmsg_user {
 362	u64 seq;
 363	u32 idx;
 364	struct mutex lock;
 365	char buf[8192];
 366};
 367
 368static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv,
 369			      unsigned long count, loff_t pos)
 370{
 371	char *buf, *line;
 372	int i;
 373	int level = default_message_loglevel;
 374	int facility = 1;	/* LOG_USER */
 375	size_t len = iov_length(iv, count);
 376	ssize_t ret = len;
 377
 378	if (len > LOG_LINE_MAX)
 379		return -EINVAL;
 380	buf = kmalloc(len+1, GFP_KERNEL);
 381	if (buf == NULL)
 382		return -ENOMEM;
 383
 384	line = buf;
 385	for (i = 0; i < count; i++) {
 386		if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len))
 387			goto out;
 388		line += iv[i].iov_len;
 389	}
 390
 391	/*
 392	 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
 393	 * the decimal value represents 32bit, the lower 3 bit are the log
 394	 * level, the rest are the log facility.
 395	 *
 396	 * If no prefix or no userspace facility is specified, we
 397	 * enforce LOG_USER, to be able to reliably distinguish
 398	 * kernel-generated messages from userspace-injected ones.
 399	 */
 400	line = buf;
 401	if (line[0] == '<') {
 402		char *endp = NULL;
 403
 404		i = simple_strtoul(line+1, &endp, 10);
 405		if (endp && endp[0] == '>') {
 406			level = i & 7;
 407			if (i >> 3)
 408				facility = i >> 3;
 409			endp++;
 410			len -= endp - line;
 411			line = endp;
 412		}
 413	}
 414	line[len] = '\0';
 415
 416	printk_emit(facility, level, NULL, 0, "%s", line);
 417out:
 418	kfree(buf);
 419	return ret;
 420}
 421
 422static ssize_t devkmsg_read(struct file *file, char __user *buf,
 423			    size_t count, loff_t *ppos)
 424{
 425	struct devkmsg_user *user = file->private_data;
 426	struct log *msg;
 427	u64 ts_usec;
 428	size_t i;
 429	size_t len;
 430	ssize_t ret;
 431
 432	if (!user)
 433		return -EBADF;
 434
 435	ret = mutex_lock_interruptible(&user->lock);
 436	if (ret)
 437		return ret;
 438	raw_spin_lock_irq(&logbuf_lock);
 439	while (user->seq == log_next_seq) {
 440		if (file->f_flags & O_NONBLOCK) {
 441			ret = -EAGAIN;
 442			raw_spin_unlock_irq(&logbuf_lock);
 443			goto out;
 444		}
 445
 446		raw_spin_unlock_irq(&logbuf_lock);
 447		ret = wait_event_interruptible(log_wait,
 448					       user->seq != log_next_seq);
 449		if (ret)
 450			goto out;
 451		raw_spin_lock_irq(&logbuf_lock);
 452	}
 453
 454	if (user->seq < log_first_seq) {
 455		/* our last seen message is gone, return error and reset */
 456		user->idx = log_first_idx;
 457		user->seq = log_first_seq;
 458		ret = -EPIPE;
 459		raw_spin_unlock_irq(&logbuf_lock);
 460		goto out;
 461	}
 462
 463	msg = log_from_idx(user->idx);
 464	ts_usec = msg->ts_nsec;
 465	do_div(ts_usec, 1000);
 466	len = sprintf(user->buf, "%u,%llu,%llu;",
 467		      (msg->facility << 3) | msg->level, user->seq, ts_usec);
 468
 469	/* escape non-printable characters */
 470	for (i = 0; i < msg->text_len; i++) {
 471		unsigned char c = log_text(msg)[i];
 472
 473		if (c < ' ' || c >= 127 || c == '\\')
 474			len += sprintf(user->buf + len, "\\x%02x", c);
 475		else
 476			user->buf[len++] = c;
 477	}
 478	user->buf[len++] = '\n';
 479
 480	if (msg->dict_len) {
 481		bool line = true;
 482
 483		for (i = 0; i < msg->dict_len; i++) {
 484			unsigned char c = log_dict(msg)[i];
 485
 486			if (line) {
 487				user->buf[len++] = ' ';
 488				line = false;
 489			}
 490
 491			if (c == '\0') {
 492				user->buf[len++] = '\n';
 493				line = true;
 494				continue;
 495			}
 496
 497			if (c < ' ' || c >= 127 || c == '\\') {
 498				len += sprintf(user->buf + len, "\\x%02x", c);
 499				continue;
 500			}
 501
 502			user->buf[len++] = c;
 503		}
 504		user->buf[len++] = '\n';
 505	}
 506
 507	user->idx = log_next(user->idx);
 508	user->seq++;
 509	raw_spin_unlock_irq(&logbuf_lock);
 510
 511	if (len > count) {
 512		ret = -EINVAL;
 513		goto out;
 514	}
 515
 516	if (copy_to_user(buf, user->buf, len)) {
 517		ret = -EFAULT;
 518		goto out;
 519	}
 520	ret = len;
 521out:
 522	mutex_unlock(&user->lock);
 523	return ret;
 524}
 525
 526static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
 527{
 528	struct devkmsg_user *user = file->private_data;
 529	loff_t ret = 0;
 530
 531	if (!user)
 532		return -EBADF;
 533	if (offset)
 534		return -ESPIPE;
 535
 536	raw_spin_lock_irq(&logbuf_lock);
 537	switch (whence) {
 538	case SEEK_SET:
 539		/* the first record */
 540		user->idx = log_first_idx;
 541		user->seq = log_first_seq;
 542		break;
 543	case SEEK_DATA:
 544		/*
 545		 * The first record after the last SYSLOG_ACTION_CLEAR,
 546		 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
 547		 * changes no global state, and does not clear anything.
 548		 */
 549		user->idx = clear_idx;
 550		user->seq = clear_seq;
 551		break;
 552	case SEEK_END:
 553		/* after the last record */
 554		user->idx = log_next_idx;
 555		user->seq = log_next_seq;
 556		break;
 557	default:
 558		ret = -EINVAL;
 559	}
 560	raw_spin_unlock_irq(&logbuf_lock);
 561	return ret;
 562}
 563
 564static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
 565{
 566	struct devkmsg_user *user = file->private_data;
 567	int ret = 0;
 568
 569	if (!user)
 570		return POLLERR|POLLNVAL;
 571
 572	poll_wait(file, &log_wait, wait);
 573
 574	raw_spin_lock_irq(&logbuf_lock);
 575	if (user->seq < log_next_seq) {
 576		/* return error when data has vanished underneath us */
 577		if (user->seq < log_first_seq)
 578			ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
 579		ret = POLLIN|POLLRDNORM;
 580	}
 581	raw_spin_unlock_irq(&logbuf_lock);
 582
 583	return ret;
 584}
 585
 586static int devkmsg_open(struct inode *inode, struct file *file)
 587{
 588	struct devkmsg_user *user;
 589	int err;
 590
 591	/* write-only does not need any file context */
 592	if ((file->f_flags & O_ACCMODE) == O_WRONLY)
 593		return 0;
 594
 595	err = security_syslog(SYSLOG_ACTION_READ_ALL);
 596	if (err)
 597		return err;
 598
 599	user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
 600	if (!user)
 601		return -ENOMEM;
 602
 603	mutex_init(&user->lock);
 604
 605	raw_spin_lock_irq(&logbuf_lock);
 606	user->idx = log_first_idx;
 607	user->seq = log_first_seq;
 608	raw_spin_unlock_irq(&logbuf_lock);
 609
 610	file->private_data = user;
 611	return 0;
 612}
 613
 614static int devkmsg_release(struct inode *inode, struct file *file)
 615{
 616	struct devkmsg_user *user = file->private_data;
 617
 618	if (!user)
 619		return 0;
 620
 621	mutex_destroy(&user->lock);
 622	kfree(user);
 623	return 0;
 624}
 625
 626const struct file_operations kmsg_fops = {
 627	.open = devkmsg_open,
 628	.read = devkmsg_read,
 629	.aio_write = devkmsg_writev,
 630	.llseek = devkmsg_llseek,
 631	.poll = devkmsg_poll,
 632	.release = devkmsg_release,
 633};
 634
 635#ifdef CONFIG_KEXEC
 636/*
 637 * This appends the listed symbols to /proc/vmcoreinfo
 638 *
 639 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
 640 * obtain access to symbols that are otherwise very difficult to locate.  These
 641 * symbols are specifically used so that utilities can access and extract the
 642 * dmesg log from a vmcore file after a crash.
 643 */
 644void log_buf_kexec_setup(void)
 645{
 646	VMCOREINFO_SYMBOL(log_buf);
 647	VMCOREINFO_SYMBOL(log_buf_len);
 648	VMCOREINFO_SYMBOL(log_first_idx);
 649	VMCOREINFO_SYMBOL(log_next_idx);
 650}
 651#endif
 652
 653/* requested log_buf_len from kernel cmdline */
 654static unsigned long __initdata new_log_buf_len;
 655
 656/* save requested log_buf_len since it's too early to process it */
 657static int __init log_buf_len_setup(char *str)
 658{
 659	unsigned size = memparse(str, &str);
 660
 661	if (size)
 662		size = roundup_pow_of_two(size);
 663	if (size > log_buf_len)
 664		new_log_buf_len = size;
 665
 666	return 0;
 667}
 668early_param("log_buf_len", log_buf_len_setup);
 669
 670void __init setup_log_buf(int early)
 671{
 672	unsigned long flags;
 673	char *new_log_buf;
 674	int free;
 675
 676	if (!new_log_buf_len)
 677		return;
 678
 679	if (early) {
 680		unsigned long mem;
 681
 682		mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
 683		if (!mem)
 684			return;
 685		new_log_buf = __va(mem);
 686	} else {
 687		new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
 688	}
 689
 690	if (unlikely(!new_log_buf)) {
 691		pr_err("log_buf_len: %ld bytes not available\n",
 692			new_log_buf_len);
 693		return;
 694	}
 695
 696	raw_spin_lock_irqsave(&logbuf_lock, flags);
 697	log_buf_len = new_log_buf_len;
 698	log_buf = new_log_buf;
 699	new_log_buf_len = 0;
 700	free = __LOG_BUF_LEN - log_next_idx;
 701	memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
 702	raw_spin_unlock_irqrestore(&logbuf_lock, flags);
 703
 704	pr_info("log_buf_len: %d\n", log_buf_len);
 705	pr_info("early log buf free: %d(%d%%)\n",
 706		free, (free * 100) / __LOG_BUF_LEN);
 707}
 708
 709#ifdef CONFIG_BOOT_PRINTK_DELAY
 710
 711static int boot_delay; /* msecs delay after each printk during bootup */
 712static unsigned long long loops_per_msec;	/* based on boot_delay */
 713
 714static int __init boot_delay_setup(char *str)
 715{
 716	unsigned long lpj;
 717
 718	lpj = preset_lpj ? preset_lpj : 1000000;	/* some guess */
 719	loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
 720
 721	get_option(&str, &boot_delay);
 722	if (boot_delay > 10 * 1000)
 723		boot_delay = 0;
 724
 725	pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
 726		"HZ: %d, loops_per_msec: %llu\n",
 727		boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
 728	return 1;
 729}
 730__setup("boot_delay=", boot_delay_setup);
 731
 732static void boot_delay_msec(void)
 733{
 734	unsigned long long k;
 735	unsigned long timeout;
 736
 737	if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
 738		return;
 739
 740	k = (unsigned long long)loops_per_msec * boot_delay;
 741
 742	timeout = jiffies + msecs_to_jiffies(boot_delay);
 743	while (k) {
 744		k--;
 745		cpu_relax();
 746		/*
 747		 * use (volatile) jiffies to prevent
 748		 * compiler reduction; loop termination via jiffies
 749		 * is secondary and may or may not happen.
 750		 */
 751		if (time_after(jiffies, timeout))
 752			break;
 753		touch_nmi_watchdog();
 754	}
 755}
 756#else
 757static inline void boot_delay_msec(void)
 758{
 759}
 760#endif
 761
 762#ifdef CONFIG_SECURITY_DMESG_RESTRICT
 763int dmesg_restrict = 1;
 764#else
 765int dmesg_restrict;
 766#endif
 767
 768static int syslog_action_restricted(int type)
 769{
 770	if (dmesg_restrict)
 771		return 1;
 772	/* Unless restricted, we allow "read all" and "get buffer size" for everybody */
 773	return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
 774}
 775
 776static int check_syslog_permissions(int type, bool from_file)
 777{
 778	/*
 779	 * If this is from /proc/kmsg and we've already opened it, then we've
 780	 * already done the capabilities checks at open time.
 781	 */
 782	if (from_file && type != SYSLOG_ACTION_OPEN)
 783		return 0;
 784
 785	if (syslog_action_restricted(type)) {
 786		if (capable(CAP_SYSLOG))
 787			return 0;
 788		/* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
 789		if (capable(CAP_SYS_ADMIN)) {
 790			printk_once(KERN_WARNING "%s (%d): "
 791				 "Attempt to access syslog with CAP_SYS_ADMIN "
 792				 "but no CAP_SYSLOG (deprecated).\n",
 793				 current->comm, task_pid_nr(current));
 794			return 0;
 795		}
 796		return -EPERM;
 797	}
 798	return 0;
 799}
 800
 801#if defined(CONFIG_PRINTK_TIME)
 802static bool printk_time = 1;
 803#else
 804static bool printk_time;
 805#endif
 806module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
 807
 808static size_t print_time(u64 ts, char *buf)
 809{
 810	unsigned long rem_nsec;
 811
 812	if (!printk_time)
 813		return 0;
 814
 815	if (!buf)
 816		return 15;
 817
 818	rem_nsec = do_div(ts, 1000000000);
 819	return sprintf(buf, "[%5lu.%06lu] ",
 820		       (unsigned long)ts, rem_nsec / 1000);
 821}
 822
 823static size_t print_prefix(const struct log *msg, bool syslog, char *buf)
 824{
 825	size_t len = 0;
 826	unsigned int prefix = (msg->facility << 3) | msg->level;
 827
 828	if (syslog) {
 829		if (buf) {
 830			len += sprintf(buf, "<%u>", prefix);
 831		} else {
 832			len += 3;
 833			if (prefix > 999)
 834				len += 3;
 835			else if (prefix > 99)
 836				len += 2;
 837			else if (prefix > 9)
 838				len++;
 839		}
 840	}
 841
 842	len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
 843	return len;
 844}
 845
 846static size_t msg_print_text(const struct log *msg, enum log_flags prev,
 847			     bool syslog, char *buf, size_t size)
 848{
 849	const char *text = log_text(msg);
 850	size_t text_size = msg->text_len;
 851	bool prefix = true;
 852	bool newline = true;
 853	size_t len = 0;
 854
 855	if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))
 856		prefix = false;
 857
 858	if (msg->flags & LOG_CONT) {
 859		if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE))
 860			prefix = false;
 861
 862		if (!(msg->flags & LOG_NEWLINE))
 863			newline = false;
 864	}
 865
 866	do {
 867		const char *next = memchr(text, '\n', text_size);
 868		size_t text_len;
 869
 870		if (next) {
 871			text_len = next - text;
 872			next++;
 873			text_size -= next - text;
 874		} else {
 875			text_len = text_size;
 876		}
 877
 878		if (buf) {
 879			if (print_prefix(msg, syslog, NULL) +
 880			    text_len + 1 >= size - len)
 881				break;
 882
 883			if (prefix)
 884				len += print_prefix(msg, syslog, buf + len);
 885			memcpy(buf + len, text, text_len);
 886			len += text_len;
 887			if (next || newline)
 888				buf[len++] = '\n';
 889		} else {
 890			/* SYSLOG_ACTION_* buffer size only calculation */
 891			if (prefix)
 892				len += print_prefix(msg, syslog, NULL);
 893			len += text_len;
 894			if (next || newline)
 895				len++;
 896		}
 897
 898		prefix = true;
 899		text = next;
 900	} while (text);
 901
 902	return len;
 903}
 904
 905static int syslog_print(char __user *buf, int size)
 906{
 907	char *text;
 908	struct log *msg;
 909	int len = 0;
 910
 911	text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
 912	if (!text)
 913		return -ENOMEM;
 914
 915	while (size > 0) {
 916		size_t n;
 917		size_t skip;
 918
 919		raw_spin_lock_irq(&logbuf_lock);
 920		if (syslog_seq < log_first_seq) {
 921			/* messages are gone, move to first one */
 922			syslog_seq = log_first_seq;
 923			syslog_idx = log_first_idx;
 924			syslog_prev = 0;
 925			syslog_partial = 0;
 926		}
 927		if (syslog_seq == log_next_seq) {
 928			raw_spin_unlock_irq(&logbuf_lock);
 929			break;
 930		}
 931
 932		skip = syslog_partial;
 933		msg = log_from_idx(syslog_idx);
 934		n = msg_print_text(msg, syslog_prev, true, text,
 935				   LOG_LINE_MAX + PREFIX_MAX);
 936		if (n - syslog_partial <= size) {
 937			/* message fits into buffer, move forward */
 938			syslog_idx = log_next(syslog_idx);
 939			syslog_seq++;
 940			syslog_prev = msg->flags;
 941			n -= syslog_partial;
 942			syslog_partial = 0;
 943		} else if (!len){
 944			/* partial read(), remember position */
 945			n = size;
 946			syslog_partial += n;
 947		} else
 948			n = 0;
 949		raw_spin_unlock_irq(&logbuf_lock);
 950
 951		if (!n)
 952			break;
 953
 954		if (copy_to_user(buf, text + skip, n)) {
 955			if (!len)
 956				len = -EFAULT;
 957			break;
 958		}
 959
 960		len += n;
 961		size -= n;
 962		buf += n;
 963	}
 964
 965	kfree(text);
 966	return len;
 967}
 968
 969static int syslog_print_all(char __user *buf, int size, bool clear)
 970{
 971	char *text;
 972	int len = 0;
 973
 974	text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
 975	if (!text)
 976		return -ENOMEM;
 977
 978	raw_spin_lock_irq(&logbuf_lock);
 979	if (buf) {
 980		u64 next_seq;
 981		u64 seq;
 982		u32 idx;
 983		enum log_flags prev;
 984
 985		if (clear_seq < log_first_seq) {
 986			/* messages are gone, move to first available one */
 987			clear_seq = log_first_seq;
 988			clear_idx = log_first_idx;
 989		}
 990
 991		/*
 992		 * Find first record that fits, including all following records,
 993		 * into the user-provided buffer for this dump.
 994		 */
 995		seq = clear_seq;
 996		idx = clear_idx;
 997		prev = 0;
 998		while (seq < log_next_seq) {
 999			struct log *msg = log_from_idx(idx);
1000
1001			len += msg_print_text(msg, prev, true, NULL, 0);
1002			prev = msg->flags;
1003			idx = log_next(idx);
1004			seq++;
1005		}
1006
1007		/* move first record forward until length fits into the buffer */
1008		seq = clear_seq;
1009		idx = clear_idx;
1010		prev = 0;
1011		while (len > size && seq < log_next_seq) {
1012			struct log *msg = log_from_idx(idx);
1013
1014			len -= msg_print_text(msg, prev, true, NULL, 0);
1015			prev = msg->flags;
1016			idx = log_next(idx);
1017			seq++;
1018		}
1019
1020		/* last message fitting into this dump */
1021		next_seq = log_next_seq;
1022
1023		len = 0;
1024		prev = 0;
1025		while (len >= 0 && seq < next_seq) {
1026			struct log *msg = log_from_idx(idx);
1027			int textlen;
1028
1029			textlen = msg_print_text(msg, prev, true, text,
1030						 LOG_LINE_MAX + PREFIX_MAX);
1031			if (textlen < 0) {
1032				len = textlen;
1033				break;
1034			}
1035			idx = log_next(idx);
1036			seq++;
1037			prev = msg->flags;
1038
1039			raw_spin_unlock_irq(&logbuf_lock);
1040			if (copy_to_user(buf + len, text, textlen))
1041				len = -EFAULT;
1042			else
1043				len += textlen;
1044			raw_spin_lock_irq(&logbuf_lock);
1045
1046			if (seq < log_first_seq) {
1047				/* messages are gone, move to next one */
1048				seq = log_first_seq;
1049				idx = log_first_idx;
1050				prev = 0;
1051			}
1052		}
1053	}
1054
1055	if (clear) {
1056		clear_seq = log_next_seq;
1057		clear_idx = log_next_idx;
1058	}
1059	raw_spin_unlock_irq(&logbuf_lock);
1060
1061	kfree(text);
1062	return len;
1063}
1064
1065int do_syslog(int type, char __user *buf, int len, bool from_file)
1066{
1067	bool clear = false;
1068	static int saved_console_loglevel = -1;
1069	int error;
1070
1071	error = check_syslog_permissions(type, from_file);
1072	if (error)
1073		goto out;
1074
1075	error = security_syslog(type);
1076	if (error)
1077		return error;
1078
1079	switch (type) {
1080	case SYSLOG_ACTION_CLOSE:	/* Close log */
1081		break;
1082	case SYSLOG_ACTION_OPEN:	/* Open log */
1083		break;
1084	case SYSLOG_ACTION_READ:	/* Read from log */
1085		error = -EINVAL;
1086		if (!buf || len < 0)
1087			goto out;
1088		error = 0;
1089		if (!len)
1090			goto out;
1091		if (!access_ok(VERIFY_WRITE, buf, len)) {
1092			error = -EFAULT;
1093			goto out;
1094		}
1095		error = wait_event_interruptible(log_wait,
1096						 syslog_seq != log_next_seq);
1097		if (error)
1098			goto out;
1099		error = syslog_print(buf, len);
1100		break;
1101	/* Read/clear last kernel messages */
1102	case SYSLOG_ACTION_READ_CLEAR:
1103		clear = true;
1104		/* FALL THRU */
1105	/* Read last kernel messages */
1106	case SYSLOG_ACTION_READ_ALL:
1107		error = -EINVAL;
1108		if (!buf || len < 0)
1109			goto out;
1110		error = 0;
1111		if (!len)
1112			goto out;
1113		if (!access_ok(VERIFY_WRITE, buf, len)) {
1114			error = -EFAULT;
1115			goto out;
1116		}
1117		error = syslog_print_all(buf, len, clear);
1118		break;
1119	/* Clear ring buffer */
1120	case SYSLOG_ACTION_CLEAR:
1121		syslog_print_all(NULL, 0, true);
1122		break;
1123	/* Disable logging to console */
1124	case SYSLOG_ACTION_CONSOLE_OFF:
1125		if (saved_console_loglevel == -1)
1126			saved_console_loglevel = console_loglevel;
1127		console_loglevel = minimum_console_loglevel;
1128		break;
1129	/* Enable logging to console */
1130	case SYSLOG_ACTION_CONSOLE_ON:
1131		if (saved_console_loglevel != -1) {
1132			console_loglevel = saved_console_loglevel;
1133			saved_console_loglevel = -1;
1134		}
1135		break;
1136	/* Set level of messages printed to console */
1137	case SYSLOG_ACTION_CONSOLE_LEVEL:
1138		error = -EINVAL;
1139		if (len < 1 || len > 8)
1140			goto out;
1141		if (len < minimum_console_loglevel)
1142			len = minimum_console_loglevel;
1143		console_loglevel = len;
1144		/* Implicitly re-enable logging to console */
1145		saved_console_loglevel = -1;
1146		error = 0;
1147		break;
1148	/* Number of chars in the log buffer */
1149	case SYSLOG_ACTION_SIZE_UNREAD:
1150		raw_spin_lock_irq(&logbuf_lock);
1151		if (syslog_seq < log_first_seq) {
1152			/* messages are gone, move to first one */
1153			syslog_seq = log_first_seq;
1154			syslog_idx = log_first_idx;
1155			syslog_prev = 0;
1156			syslog_partial = 0;
1157		}
1158		if (from_file) {
1159			/*
1160			 * Short-cut for poll(/"proc/kmsg") which simply checks
1161			 * for pending data, not the size; return the count of
1162			 * records, not the length.
1163			 */
1164			error = log_next_idx - syslog_idx;
1165		} else {
1166			u64 seq = syslog_seq;
1167			u32 idx = syslog_idx;
1168			enum log_flags prev = syslog_prev;
1169
1170			error = 0;
1171			while (seq < log_next_seq) {
1172				struct log *msg = log_from_idx(idx);
1173
1174				error += msg_print_text(msg, prev, true, NULL, 0);
1175				idx = log_next(idx);
1176				seq++;
1177				prev = msg->flags;
1178			}
1179			error -= syslog_partial;
1180		}
1181		raw_spin_unlock_irq(&logbuf_lock);
1182		break;
1183	/* Size of the log buffer */
1184	case SYSLOG_ACTION_SIZE_BUFFER:
1185		error = log_buf_len;
1186		break;
1187	default:
1188		error = -EINVAL;
1189		break;
1190	}
1191out:
1192	return error;
1193}
1194
1195SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
1196{
1197	return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
1198}
1199
1200static bool __read_mostly ignore_loglevel;
1201
1202static int __init ignore_loglevel_setup(char *str)
1203{
1204	ignore_loglevel = 1;
1205	printk(KERN_INFO "debug: ignoring loglevel setting.\n");
1206
1207	return 0;
1208}
1209
1210early_param("ignore_loglevel", ignore_loglevel_setup);
1211module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
1212MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
1213	"print all kernel messages to the console.");
1214
1215/*
1216 * Call the console drivers, asking them to write out
1217 * log_buf[start] to log_buf[end - 1].
1218 * The console_lock must be held.
1219 */
1220static void call_console_drivers(int level, const char *text, size_t len)
1221{
1222	struct console *con;
1223
1224	trace_console(text, 0, len, len);
1225
1226	if (level >= console_loglevel && !ignore_loglevel)
1227		return;
1228	if (!console_drivers)
1229		return;
1230
1231	for_each_console(con) {
1232		if (exclusive_console && con != exclusive_console)
1233			continue;
1234		if (!(con->flags & CON_ENABLED))
1235			continue;
1236		if (!con->write)
1237			continue;
1238		if (!cpu_online(smp_processor_id()) &&
1239		    !(con->flags & CON_ANYTIME))
1240			continue;
1241		con->write(con, text, len);
1242	}
1243}
1244
1245/*
1246 * Zap console related locks when oopsing. Only zap at most once
1247 * every 10 seconds, to leave time for slow consoles to print a
1248 * full oops.
1249 */
1250static void zap_locks(void)
1251{
1252	static unsigned long oops_timestamp;
1253
1254	if (time_after_eq(jiffies, oops_timestamp) &&
1255			!time_after(jiffies, oops_timestamp + 30 * HZ))
1256		return;
1257
1258	oops_timestamp = jiffies;
1259
1260	debug_locks_off();
1261	/* If a crash is occurring, make sure we can't deadlock */
1262	raw_spin_lock_init(&logbuf_lock);
1263	/* And make sure that we print immediately */
1264	sema_init(&console_sem, 1);
1265}
1266
1267/* Check if we have any console registered that can be called early in boot. */
1268static int have_callable_console(void)
1269{
1270	struct console *con;
1271
1272	for_each_console(con)
1273		if (con->flags & CON_ANYTIME)
1274			return 1;
1275
1276	return 0;
1277}
1278
1279/*
1280 * Can we actually use the console at this time on this cpu?
1281 *
1282 * Console drivers may assume that per-cpu resources have
1283 * been allocated. So unless they're explicitly marked as
1284 * being able to cope (CON_ANYTIME) don't call them until
1285 * this CPU is officially up.
1286 */
1287static inline int can_use_console(unsigned int cpu)
1288{
1289	return cpu_online(cpu) || have_callable_console();
1290}
1291
1292/*
1293 * Try to get console ownership to actually show the kernel
1294 * messages from a 'printk'. Return true (and with the
1295 * console_lock held, and 'console_locked' set) if it
1296 * is successful, false otherwise.
1297 *
1298 * This gets called with the 'logbuf_lock' spinlock held and
1299 * interrupts disabled. It should return with 'lockbuf_lock'
1300 * released but interrupts still disabled.
1301 */
1302static int console_trylock_for_printk(unsigned int cpu)
1303	__releases(&logbuf_lock)
1304{
1305	int retval = 0, wake = 0;
1306
1307	if (console_trylock()) {
1308		retval = 1;
1309
1310		/*
1311		 * If we can't use the console, we need to release
1312		 * the console semaphore by hand to avoid flushing
1313		 * the buffer. We need to hold the console semaphore
1314		 * in order to do this test safely.
1315		 */
1316		if (!can_use_console(cpu)) {
1317			console_locked = 0;
1318			wake = 1;
1319			retval = 0;
1320		}
1321	}
1322	logbuf_cpu = UINT_MAX;
1323	if (wake)
1324		up(&console_sem);
1325	raw_spin_unlock(&logbuf_lock);
1326	return retval;
1327}
1328
1329int printk_delay_msec __read_mostly;
1330
1331static inline void printk_delay(void)
1332{
1333	if (unlikely(printk_delay_msec)) {
1334		int m = printk_delay_msec;
1335
1336		while (m--) {
1337			mdelay(1);
1338			touch_nmi_watchdog();
1339		}
1340	}
1341}
1342
1343/*
1344 * Continuation lines are buffered, and not committed to the record buffer
1345 * until the line is complete, or a race forces it. The line fragments
1346 * though, are printed immediately to the consoles to ensure everything has
1347 * reached the console in case of a kernel crash.
1348 */
1349static struct cont {
1350	char buf[LOG_LINE_MAX];
1351	size_t len;			/* length == 0 means unused buffer */
1352	size_t cons;			/* bytes written to console */
1353	struct task_struct *owner;	/* task of first print*/
1354	u64 ts_nsec;			/* time of first print */
1355	u8 level;			/* log level of first message */
1356	u8 facility;			/* log level of first message */
1357	bool flushed:1;			/* buffer sealed and committed */
1358} cont;
1359
1360static void cont_flush(enum log_flags flags)
1361{
1362	if (cont.flushed)
1363		return;
1364	if (cont.len == 0)
1365		return;
1366
1367	log_store(cont.facility, cont.level, LOG_NOCONS | flags,
1368		  cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1369
1370	cont.flushed = true;
1371}
1372
1373static bool cont_add(int facility, int level, const char *text, size_t len)
1374{
1375	if (cont.len && cont.flushed)
1376		return false;
1377
1378	if (cont.len + len > sizeof(cont.buf)) {
1379		/* the line gets too long, split it up in separate records */
1380		cont_flush(LOG_CONT);
1381		return false;
1382	}
1383
1384	if (!cont.len) {
1385		cont.facility = facility;
1386		cont.level = level;
1387		cont.owner = current;
1388		cont.ts_nsec = local_clock();
1389		cont.cons = 0;
1390		cont.flushed = false;
1391	}
1392
1393	memcpy(cont.buf + cont.len, text, len);
1394	cont.len += len;
1395	return true;
1396}
1397
1398static size_t cont_print_text(char *text, size_t size)
1399{
1400	size_t textlen = 0;
1401	size_t len;
1402
1403	if (cont.cons == 0) {
1404		textlen += print_time(cont.ts_nsec, text);
1405		size -= textlen;
1406	}
1407
1408	len = cont.len - cont.cons;
1409	if (len > 0) {
1410		if (len+1 > size)
1411			len = size-1;
1412		memcpy(text + textlen, cont.buf + cont.cons, len);
1413		textlen += len;
1414		cont.cons = cont.len;
1415	}
1416
1417	if (cont.flushed) {
1418		text[textlen++] = '\n';
1419		/* got everything, release buffer */
1420		cont.len = 0;
1421	}
1422	return textlen;
1423}
1424
1425asmlinkage int vprintk_emit(int facility, int level,
1426			    const char *dict, size_t dictlen,
1427			    const char *fmt, va_list args)
1428{
1429	static int recursion_bug;
1430	static char textbuf[LOG_LINE_MAX];
1431	char *text = textbuf;
1432	size_t text_len;
1433	enum log_flags lflags = 0;
1434	unsigned long flags;
1435	int this_cpu;
1436	int printed_len = 0;
1437
1438	boot_delay_msec();
1439	printk_delay();
1440
1441	/* This stops the holder of console_sem just where we want him */
1442	local_irq_save(flags);
1443	this_cpu = smp_processor_id();
1444
1445	/*
1446	 * Ouch, printk recursed into itself!
1447	 */
1448	if (unlikely(logbuf_cpu == this_cpu)) {
1449		/*
1450		 * If a crash is occurring during printk() on this CPU,
1451		 * then try to get the crash message out but make sure
1452		 * we can't deadlock. Otherwise just return to avoid the
1453		 * recursion and return - but flag the recursion so that
1454		 * it can be printed at the next appropriate moment:
1455		 */
1456		if (!oops_in_progress && !lockdep_recursing(current)) {
1457			recursion_bug = 1;
1458			goto out_restore_irqs;
1459		}
1460		zap_locks();
1461	}
1462
1463	lockdep_off();
1464	raw_spin_lock(&logbuf_lock);
1465	logbuf_cpu = this_cpu;
1466
1467	if (recursion_bug) {
1468		static const char recursion_msg[] =
1469			"BUG: recent printk recursion!";
1470
1471		recursion_bug = 0;
1472		printed_len += strlen(recursion_msg);
1473		/* emit KERN_CRIT message */
1474		log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
1475			  NULL, 0, recursion_msg, printed_len);
1476	}
1477
1478	/*
1479	 * The printf needs to come first; we need the syslog
1480	 * prefix which might be passed-in as a parameter.
1481	 */
1482	text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
1483
1484	/* mark and strip a trailing newline */
1485	if (text_len && text[text_len-1] == '\n') {
1486		text_len--;
1487		lflags |= LOG_NEWLINE;
1488	}
1489
1490	/* strip syslog prefix and extract log level or control flags */
1491	if (text[0] == '<' && text[1] && text[2] == '>') {
1492		switch (text[1]) {
1493		case '0' ... '7':
1494			if (level == -1)
1495				level = text[1] - '0';
1496		case 'd':	/* KERN_DEFAULT */
1497			lflags |= LOG_PREFIX;
1498		case 'c':	/* KERN_CONT */
1499			text += 3;
1500			text_len -= 3;
1501		}
1502	}
1503
1504	if (level == -1)
1505		level = default_message_loglevel;
1506
1507	if (dict)
1508		lflags |= LOG_PREFIX|LOG_NEWLINE;
1509
1510	if (!(lflags & LOG_NEWLINE)) {
1511		/*
1512		 * Flush the conflicting buffer. An earlier newline was missing,
1513		 * or another task also prints continuation lines.
1514		 */
1515		if (cont.len && (lflags & LOG_PREFIX || cont.owner != current))
1516			cont_flush(0);
1517
1518		/* buffer line if possible, otherwise store it right away */
1519		if (!cont_add(facility, level, text, text_len))
1520			log_store(facility, level, lflags | LOG_CONT, 0,
1521				  dict, dictlen, text, text_len);
1522	} else {
1523		bool stored = false;
1524
1525		/*
1526		 * If an earlier newline was missing and it was the same task,
1527		 * either merge it with the current buffer and flush, or if
1528		 * there was a race with interrupts (prefix == true) then just
1529		 * flush it out and store this line separately.
1530		 */
1531		if (cont.len && cont.owner == current) {
1532			if (!(lflags & LOG_PREFIX))
1533				stored = cont_add(facility, level, text, text_len);
1534			cont_flush(0);
1535		}
1536
1537		if (!stored)
1538			log_store(facility, level, lflags, 0,
1539				  dict, dictlen, text, text_len);
1540	}
1541	printed_len += text_len;
1542
1543	/*
1544	 * Try to acquire and then immediately release the console semaphore.
1545	 * The release will print out buffers and wake up /dev/kmsg and syslog()
1546	 * users.
1547	 *
1548	 * The console_trylock_for_printk() function will release 'logbuf_lock'
1549	 * regardless of whether it actually gets the console semaphore or not.
1550	 */
1551	if (console_trylock_for_printk(this_cpu))
1552		console_unlock();
1553
1554	lockdep_on();
1555out_restore_irqs:
1556	local_irq_restore(flags);
1557
1558	return printed_len;
1559}
1560EXPORT_SYMBOL(vprintk_emit);
1561
1562asmlinkage int vprintk(const char *fmt, va_list args)
1563{
1564	return vprintk_emit(0, -1, NULL, 0, fmt, args);
1565}
1566EXPORT_SYMBOL(vprintk);
1567
1568asmlinkage int printk_emit(int facility, int level,
1569			   const char *dict, size_t dictlen,
1570			   const char *fmt, ...)
1571{
1572	va_list args;
1573	int r;
1574
1575	va_start(args, fmt);
1576	r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1577	va_end(args);
1578
1579	return r;
1580}
1581EXPORT_SYMBOL(printk_emit);
1582
1583/**
1584 * printk - print a kernel message
1585 * @fmt: format string
1586 *
1587 * This is printk(). It can be called from any context. We want it to work.
1588 *
1589 * We try to grab the console_lock. If we succeed, it's easy - we log the
1590 * output and call the console drivers.  If we fail to get the semaphore, we
1591 * place the output into the log buffer and return. The current holder of
1592 * the console_sem will notice the new output in console_unlock(); and will
1593 * send it to the consoles before releasing the lock.
1594 *
1595 * One effect of this deferred printing is that code which calls printk() and
1596 * then changes console_loglevel may break. This is because console_loglevel
1597 * is inspected when the actual printing occurs.
1598 *
1599 * See also:
1600 * printf(3)
1601 *
1602 * See the vsnprintf() documentation for format string extensions over C99.
1603 */
1604asmlinkage int printk(const char *fmt, ...)
1605{
1606	va_list args;
1607	int r;
1608
1609#ifdef CONFIG_KGDB_KDB
1610	if (unlikely(kdb_trap_printk)) {
1611		va_start(args, fmt);
1612		r = vkdb_printf(fmt, args);
1613		va_end(args);
1614		return r;
1615	}
1616#endif
1617	va_start(args, fmt);
1618	r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1619	va_end(args);
1620
1621	return r;
1622}
1623EXPORT_SYMBOL(printk);
1624
1625#else
1626
1627#define LOG_LINE_MAX		0
1628#define PREFIX_MAX		0
1629static struct cont {
1630	size_t len;
1631	size_t cons;
1632	u8 level;
1633	bool flushed:1;
1634} cont;
1635static struct log *log_from_idx(u32 idx) { return NULL; }
1636static u32 log_next(u32 idx) { return 0; }
1637static void call_console_drivers(int level, const char *text, size_t len) {}
1638static size_t msg_print_text(const struct log *msg, enum log_flags prev,
1639			     bool syslog, char *buf, size_t size) { return 0; }
1640static size_t cont_print_text(char *text, size_t size) { return 0; }
1641
1642#endif /* CONFIG_PRINTK */
1643
1644static int __add_preferred_console(char *name, int idx, char *options,
1645				   char *brl_options)
1646{
1647	struct console_cmdline *c;
1648	int i;
1649
1650	/*
1651	 *	See if this tty is not yet registered, and
1652	 *	if we have a slot free.
1653	 */
1654	for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1655		if (strcmp(console_cmdline[i].name, name) == 0 &&
1656			  console_cmdline[i].index == idx) {
1657				if (!brl_options)
1658					selected_console = i;
1659				return 0;
1660		}
1661	if (i == MAX_CMDLINECONSOLES)
1662		return -E2BIG;
1663	if (!brl_options)
1664		selected_console = i;
1665	c = &console_cmdline[i];
1666	strlcpy(c->name, name, sizeof(c->name));
1667	c->options = options;
1668#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1669	c->brl_options = brl_options;
1670#endif
1671	c->index = idx;
1672	return 0;
1673}
1674/*
1675 * Set up a list of consoles.  Called from init/main.c
1676 */
1677static int __init console_setup(char *str)
1678{
1679	char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
1680	char *s, *options, *brl_options = NULL;
1681	int idx;
1682
1683#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1684	if (!memcmp(str, "brl,", 4)) {
1685		brl_options = "";
1686		str += 4;
1687	} else if (!memcmp(str, "brl=", 4)) {
1688		brl_options = str + 4;
1689		str = strchr(brl_options, ',');
1690		if (!str) {
1691			printk(KERN_ERR "need port name after brl=\n");
1692			return 1;
1693		}
1694		*(str++) = 0;
1695	}
1696#endif
1697
1698	/*
1699	 * Decode str into name, index, options.
1700	 */
1701	if (str[0] >= '0' && str[0] <= '9') {
1702		strcpy(buf, "ttyS");
1703		strncpy(buf + 4, str, sizeof(buf) - 5);
1704	} else {
1705		strncpy(buf, str, sizeof(buf) - 1);
1706	}
1707	buf[sizeof(buf) - 1] = 0;
1708	if ((options = strchr(str, ',')) != NULL)
1709		*(options++) = 0;
1710#ifdef __sparc__
1711	if (!strcmp(str, "ttya"))
1712		strcpy(buf, "ttyS0");
1713	if (!strcmp(str, "ttyb"))
1714		strcpy(buf, "ttyS1");
1715#endif
1716	for (s = buf; *s; s++)
1717		if ((*s >= '0' && *s <= '9') || *s == ',')
1718			break;
1719	idx = simple_strtoul(s, NULL, 10);
1720	*s = 0;
1721
1722	__add_preferred_console(buf, idx, options, brl_options);
1723	console_set_on_cmdline = 1;
1724	return 1;
1725}
1726__setup("console=", console_setup);
1727
1728/**
1729 * add_preferred_console - add a device to the list of preferred consoles.
1730 * @name: device name
1731 * @idx: device index
1732 * @options: options for this console
1733 *
1734 * The last preferred console added will be used for kernel messages
1735 * and stdin/out/err for init.  Normally this is used by console_setup
1736 * above to handle user-supplied console arguments; however it can also
1737 * be used by arch-specific code either to override the user or more
1738 * commonly to provide a default console (ie from PROM variables) when
1739 * the user has not supplied one.
1740 */
1741int add_preferred_console(char *name, int idx, char *options)
1742{
1743	return __add_preferred_console(name, idx, options, NULL);
1744}
1745
1746int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1747{
1748	struct console_cmdline *c;
1749	int i;
1750
1751	for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1752		if (strcmp(console_cmdline[i].name, name) == 0 &&
1753			  console_cmdline[i].index == idx) {
1754				c = &console_cmdline[i];
1755				strlcpy(c->name, name_new, sizeof(c->name));
1756				c->name[sizeof(c->name) - 1] = 0;
1757				c->options = options;
1758				c->index = idx_new;
1759				return i;
1760		}
1761	/* not found */
1762	return -1;
1763}
1764
1765bool console_suspend_enabled = 1;
1766EXPORT_SYMBOL(console_suspend_enabled);
1767
1768static int __init console_suspend_disable(char *str)
1769{
1770	console_suspend_enabled = 0;
1771	return 1;
1772}
1773__setup("no_console_suspend", console_suspend_disable);
1774module_param_named(console_suspend, console_suspend_enabled,
1775		bool, S_IRUGO | S_IWUSR);
1776MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1777	" and hibernate operations");
1778
1779/**
1780 * suspend_console - suspend the console subsystem
1781 *
1782 * This disables printk() while we go into suspend states
1783 */
1784void suspend_console(void)
1785{
1786	if (!console_suspend_enabled)
1787		return;
1788	printk("Suspending console(s) (use no_console_suspend to debug)\n");
1789	console_lock();
1790	console_suspended = 1;
1791	up(&console_sem);
1792}
1793
1794void resume_console(void)
1795{
1796	if (!console_suspend_enabled)
1797		return;
1798	down(&console_sem);
1799	console_suspended = 0;
1800	console_unlock();
1801}
1802
1803/**
1804 * console_cpu_notify - print deferred console messages after CPU hotplug
1805 * @self: notifier struct
1806 * @action: CPU hotplug event
1807 * @hcpu: unused
1808 *
1809 * If printk() is called from a CPU that is not online yet, the messages
1810 * will be spooled but will not show up on the console.  This function is
1811 * called when a new CPU comes online (or fails to come up), and ensures
1812 * that any such output gets printed.
1813 */
1814static int __cpuinit console_cpu_notify(struct notifier_block *self,
1815	unsigned long action, void *hcpu)
1816{
1817	switch (action) {
1818	case CPU_ONLINE:
1819	case CPU_DEAD:
1820	case CPU_DYING:
1821	case CPU_DOWN_FAILED:
1822	case CPU_UP_CANCELED:
1823		console_lock();
1824		console_unlock();
1825	}
1826	return NOTIFY_OK;
1827}
1828
1829/**
1830 * console_lock - lock the console system for exclusive use.
1831 *
1832 * Acquires a lock which guarantees that the caller has
1833 * exclusive access to the console system and the console_drivers list.
1834 *
1835 * Can sleep, returns nothing.
1836 */
1837void console_lock(void)
1838{
1839	BUG_ON(in_interrupt());
1840	down(&console_sem);
1841	if (console_suspended)
1842		return;
1843	console_locked = 1;
1844	console_may_schedule = 1;
1845}
1846EXPORT_SYMBOL(console_lock);
1847
1848/**
1849 * console_trylock - try to lock the console system for exclusive use.
1850 *
1851 * Tried to acquire a lock which guarantees that the caller has
1852 * exclusive access to the console system and the console_drivers list.
1853 *
1854 * returns 1 on success, and 0 on failure to acquire the lock.
1855 */
1856int console_trylock(void)
1857{
1858	if (down_trylock(&console_sem))
1859		return 0;
1860	if (console_suspended) {
1861		up(&console_sem);
1862		return 0;
1863	}
1864	console_locked = 1;
1865	console_may_schedule = 0;
1866	return 1;
1867}
1868EXPORT_SYMBOL(console_trylock);
1869
1870int is_console_locked(void)
1871{
1872	return console_locked;
1873}
1874
1875/*
1876 * Delayed printk version, for scheduler-internal messages:
1877 */
1878#define PRINTK_BUF_SIZE		512
1879
1880#define PRINTK_PENDING_WAKEUP	0x01
1881#define PRINTK_PENDING_SCHED	0x02
1882
1883static DEFINE_PER_CPU(int, printk_pending);
1884static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
1885
1886void printk_tick(void)
1887{
1888	if (__this_cpu_read(printk_pending)) {
1889		int pending = __this_cpu_xchg(printk_pending, 0);
1890		if (pending & PRINTK_PENDING_SCHED) {
1891			char *buf = __get_cpu_var(printk_sched_buf);
1892			printk(KERN_WARNING "[sched_delayed] %s", buf);
1893		}
1894		if (pending & PRINTK_PENDING_WAKEUP)
1895			wake_up_interruptible(&log_wait);
1896	}
1897}
1898
1899int printk_needs_cpu(int cpu)
1900{
1901	if (cpu_is_offline(cpu))
1902		printk_tick();
1903	return __this_cpu_read(printk_pending);
1904}
1905
1906void wake_up_klogd(void)
1907{
1908	if (waitqueue_active(&log_wait))
1909		this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
1910}
1911
1912/* the next printk record to write to the console */
1913static u64 console_seq;
1914static u32 console_idx;
1915static enum log_flags console_prev;
1916
1917/**
1918 * console_unlock - unlock the console system
1919 *
1920 * Releases the console_lock which the caller holds on the console system
1921 * and the console driver list.
1922 *
1923 * While the console_lock was held, console output may have been buffered
1924 * by printk().  If this is the case, console_unlock(); emits
1925 * the output prior to releasing the lock.
1926 *
1927 * If there is output waiting, we wake /dev/kmsg and syslog() users.
1928 *
1929 * console_unlock(); may be called from any context.
1930 */
1931void console_unlock(void)
1932{
1933	static char text[LOG_LINE_MAX + PREFIX_MAX];
1934	static u64 seen_seq;
1935	unsigned long flags;
1936	bool wake_klogd = false;
1937	bool retry;
1938
1939	if (console_suspended) {
1940		up(&console_sem);
1941		return;
1942	}
1943
1944	console_may_schedule = 0;
1945
1946	/* flush buffered message fragment immediately to console */
1947	raw_spin_lock_irqsave(&logbuf_lock, flags);
1948	if (cont.len && (cont.cons < cont.len || cont.flushed)) {
1949		size_t len;
1950
1951		len = cont_print_text(text, sizeof(text));
1952		raw_spin_unlock(&logbuf_lock);
1953		stop_critical_timings();
1954		call_console_drivers(cont.level, text, len);
1955		start_critical_timings();
1956		local_irq_restore(flags);
1957	} else
1958		raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1959
1960again:
1961	for (;;) {
1962		struct log *msg;
1963		size_t len;
1964		int level;
1965
1966		raw_spin_lock_irqsave(&logbuf_lock, flags);
1967		if (seen_seq != log_next_seq) {
1968			wake_klogd = true;
1969			seen_seq = log_next_seq;
1970		}
1971
1972		if (console_seq < log_first_seq) {
1973			/* messages are gone, move to first one */
1974			console_seq = log_first_seq;
1975			console_idx = log_first_idx;
1976			console_prev = 0;
1977		}
1978skip:
1979		if (console_seq == log_next_seq)
1980			break;
1981
1982		msg = log_from_idx(console_idx);
1983		if (msg->flags & LOG_NOCONS) {
1984			/*
1985			 * Skip record we have buffered and already printed
1986			 * directly to the console when we received it.
1987			 */
1988			console_idx = log_next(console_idx);
1989			console_seq++;
1990			/*
1991			 * We will get here again when we register a new
1992			 * CON_PRINTBUFFER console. Clear the flag so we
1993			 * will properly dump everything later.
1994			 */
1995			msg->flags &= ~LOG_NOCONS;
1996			goto skip;
1997		}
1998
1999		level = msg->level;
2000		len = msg_print_text(msg, console_prev, false,
2001				     text, sizeof(text));
2002		console_idx = log_next(console_idx);
2003		console_seq++;
2004		console_prev = msg->flags;
2005		raw_spin_unlock(&logbuf_lock);
2006
2007		stop_critical_timings();	/* don't trace print latency */
2008		call_console_drivers(level, text, len);
2009		start_critical_timings();
2010		local_irq_restore(flags);
2011	}
2012	console_locked = 0;
2013
2014	/* Release the exclusive_console once it is used */
2015	if (unlikely(exclusive_console))
2016		exclusive_console = NULL;
2017
2018	raw_spin_unlock(&logbuf_lock);
2019
2020	up(&console_sem);
2021
2022	/*
2023	 * Someone could have filled up the buffer again, so re-check if there's
2024	 * something to flush. In case we cannot trylock the console_sem again,
2025	 * there's a new owner and the console_unlock() from them will do the
2026	 * flush, no worries.
2027	 */
2028	raw_spin_lock(&logbuf_lock);
2029	retry = console_seq != log_next_seq;
2030	raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2031
2032	if (retry && console_trylock())
2033		goto again;
2034
2035	if (wake_klogd)
2036		wake_up_klogd();
2037}
2038EXPORT_SYMBOL(console_unlock);
2039
2040/**
2041 * console_conditional_schedule - yield the CPU if required
2042 *
2043 * If the console code is currently allowed to sleep, and
2044 * if this CPU should yield the CPU to another task, do
2045 * so here.
2046 *
2047 * Must be called within console_lock();.
2048 */
2049void __sched console_conditional_schedule(void)
2050{
2051	if (console_may_schedule)
2052		cond_resched();
2053}
2054EXPORT_SYMBOL(console_conditional_schedule);
2055
2056void console_unblank(void)
2057{
2058	struct console *c;
2059
2060	/*
2061	 * console_unblank can no longer be called in interrupt context unless
2062	 * oops_in_progress is set to 1..
2063	 */
2064	if (oops_in_progress) {
2065		if (down_trylock(&console_sem) != 0)
2066			return;
2067	} else
2068		console_lock();
2069
2070	console_locked = 1;
2071	console_may_schedule = 0;
2072	for_each_console(c)
2073		if ((c->flags & CON_ENABLED) && c->unblank)
2074			c->unblank();
2075	console_unlock();
2076}
2077
2078/*
2079 * Return the console tty driver structure and its associated index
2080 */
2081struct tty_driver *console_device(int *index)
2082{
2083	struct console *c;
2084	struct tty_driver *driver = NULL;
2085
2086	console_lock();
2087	for_each_console(c) {
2088		if (!c->device)
2089			continue;
2090		driver = c->device(c, index);
2091		if (driver)
2092			break;
2093	}
2094	console_unlock();
2095	return driver;
2096}
2097
2098/*
2099 * Prevent further output on the passed console device so that (for example)
2100 * serial drivers can disable console output before suspending a port, and can
2101 * re-enable output afterwards.
2102 */
2103void console_stop(struct console *console)
2104{
2105	console_lock();
2106	console->flags &= ~CON_ENABLED;
2107	console_unlock();
2108}
2109EXPORT_SYMBOL(console_stop);
2110
2111void console_start(struct console *console)
2112{
2113	console_lock();
2114	console->flags |= CON_ENABLED;
2115	console_unlock();
2116}
2117EXPORT_SYMBOL(console_start);
2118
2119static int __read_mostly keep_bootcon;
2120
2121static int __init keep_bootcon_setup(char *str)
2122{
2123	keep_bootcon = 1;
2124	printk(KERN_INFO "debug: skip boot console de-registration.\n");
2125
2126	return 0;
2127}
2128
2129early_param("keep_bootcon", keep_bootcon_setup);
2130
2131/*
2132 * The console driver calls this routine during kernel initialization
2133 * to register the console printing procedure with printk() and to
2134 * print any messages that were printed by the kernel before the
2135 * console driver was initialized.
2136 *
2137 * This can happen pretty early during the boot process (because of
2138 * early_printk) - sometimes before setup_arch() completes - be careful
2139 * of what kernel features are used - they may not be initialised yet.
2140 *
2141 * There are two types of consoles - bootconsoles (early_printk) and
2142 * "real" consoles (everything which is not a bootconsole) which are
2143 * handled differently.
2144 *  - Any number of bootconsoles can be registered at any time.
2145 *  - As soon as a "real" console is registered, all bootconsoles
2146 *    will be unregistered automatically.
2147 *  - Once a "real" console is registered, any attempt to register a
2148 *    bootconsoles will be rejected
2149 */
2150void register_console(struct console *newcon)
2151{
2152	int i;
2153	unsigned long flags;
2154	struct console *bcon = NULL;
2155
2156	/*
2157	 * before we register a new CON_BOOT console, make sure we don't
2158	 * already have a valid console
2159	 */
2160	if (console_drivers && newcon->flags & CON_BOOT) {
2161		/* find the last or real console */
2162		for_each_console(bcon) {
2163			if (!(bcon->flags & CON_BOOT)) {
2164				printk(KERN_INFO "Too late to register bootconsole %s%d\n",
2165					newcon->name, newcon->index);
2166				return;
2167			}
2168		}
2169	}
2170
2171	if (console_drivers && console_drivers->flags & CON_BOOT)
2172		bcon = console_drivers;
2173
2174	if (preferred_console < 0 || bcon || !console_drivers)
2175		preferred_console = selected_console;
2176
2177	if (newcon->early_setup)
2178		newcon->early_setup();
2179
2180	/*
2181	 *	See if we want to use this console driver. If we
2182	 *	didn't select a console we take the first one
2183	 *	that registers here.
2184	 */
2185	if (preferred_console < 0) {
2186		if (newcon->index < 0)
2187			newcon->index = 0;
2188		if (newcon->setup == NULL ||
2189		    newcon->setup(newcon, NULL) == 0) {
2190			newcon->flags |= CON_ENABLED;
2191			if (newcon->device) {
2192				newcon->flags |= CON_CONSDEV;
2193				preferred_console = 0;
2194			}
2195		}
2196	}
2197
2198	/*
2199	 *	See if this console matches one we selected on
2200	 *	the command line.
2201	 */
2202	for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
2203			i++) {
2204		if (strcmp(console_cmdline[i].name, newcon->name) != 0)
2205			continue;
2206		if (newcon->index >= 0 &&
2207		    newcon->index != console_cmdline[i].index)
2208			continue;
2209		if (newcon->index < 0)
2210			newcon->index = console_cmdline[i].index;
2211#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2212		if (console_cmdline[i].brl_options) {
2213			newcon->flags |= CON_BRL;
2214			braille_register_console(newcon,
2215					console_cmdline[i].index,
2216					console_cmdline[i].options,
2217					console_cmdline[i].brl_options);
2218			return;
2219		}
2220#endif
2221		if (newcon->setup &&
2222		    newcon->setup(newcon, console_cmdline[i].options) != 0)
2223			break;
2224		newcon->flags |= CON_ENABLED;
2225		newcon->index = console_cmdline[i].index;
2226		if (i == selected_console) {
2227			newcon->flags |= CON_CONSDEV;
2228			preferred_console = selected_console;
2229		}
2230		break;
2231	}
2232
2233	if (!(newcon->flags & CON_ENABLED))
2234		return;
2235
2236	/*
2237	 * If we have a bootconsole, and are switching to a real console,
2238	 * don't print everything out again, since when the boot console, and
2239	 * the real console are the same physical device, it's annoying to
2240	 * see the beginning boot messages twice
2241	 */
2242	if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
2243		newcon->flags &= ~CON_PRINTBUFFER;
2244
2245	/*
2246	 *	Put this console in the list - keep the
2247	 *	preferred driver at the head of the list.
2248	 */
2249	console_lock();
2250	if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2251		newcon->next = console_drivers;
2252		console_drivers = newcon;
2253		if (newcon->next)
2254			newcon->next->flags &= ~CON_CONSDEV;
2255	} else {
2256		newcon->next = console_drivers->next;
2257		console_drivers->next = newcon;
2258	}
2259	if (newcon->flags & CON_PRINTBUFFER) {
2260		/*
2261		 * console_unlock(); will print out the buffered messages
2262		 * for us.
2263		 */
2264		raw_spin_lock_irqsave(&logbuf_lock, flags);
2265		console_seq = syslog_seq;
2266		console_idx = syslog_idx;
2267		console_prev = syslog_prev;
2268		raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2269		/*
2270		 * We're about to replay the log buffer.  Only do this to the
2271		 * just-registered console to avoid excessive message spam to
2272		 * the already-registered consoles.
2273		 */
2274		exclusive_console = newcon;
2275	}
2276	console_unlock();
2277	console_sysfs_notify();
2278
2279	/*
2280	 * By unregistering the bootconsoles after we enable the real console
2281	 * we get the "console xxx enabled" message on all the consoles -
2282	 * boot consoles, real consoles, etc - this is to ensure that end
2283	 * users know there might be something in the kernel's log buffer that
2284	 * went to the bootconsole (that they do not see on the real console)
2285	 */
2286	if (bcon &&
2287	    ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2288	    !keep_bootcon) {
2289		/* we need to iterate through twice, to make sure we print
2290		 * everything out, before we unregister the console(s)
2291		 */
2292		printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
2293			newcon->name, newcon->index);
2294		for_each_console(bcon)
2295			if (bcon->flags & CON_BOOT)
2296				unregister_console(bcon);
2297	} else {
2298		printk(KERN_INFO "%sconsole [%s%d] enabled\n",
2299			(newcon->flags & CON_BOOT) ? "boot" : "" ,
2300			newcon->name, newcon->index);
2301	}
2302}
2303EXPORT_SYMBOL(register_console);
2304
2305int unregister_console(struct console *console)
2306{
2307        struct console *a, *b;
2308	int res = 1;
2309
2310#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2311	if (console->flags & CON_BRL)
2312		return braille_unregister_console(console);
2313#endif
2314
2315	console_lock();
2316	if (console_drivers == console) {
2317		console_drivers=console->next;
2318		res = 0;
2319	} else if (console_drivers) {
2320		for (a=console_drivers->next, b=console_drivers ;
2321		     a; b=a, a=b->next) {
2322			if (a == console) {
2323				b->next = a->next;
2324				res = 0;
2325				break;
2326			}
2327		}
2328	}
2329
2330	/*
2331	 * If this isn't the last console and it has CON_CONSDEV set, we
2332	 * need to set it on the next preferred console.
2333	 */
2334	if (console_drivers != NULL && console->flags & CON_CONSDEV)
2335		console_drivers->flags |= CON_CONSDEV;
2336
2337	console_unlock();
2338	console_sysfs_notify();
2339	return res;
2340}
2341EXPORT_SYMBOL(unregister_console);
2342
2343static int __init printk_late_init(void)
2344{
2345	struct console *con;
2346
2347	for_each_console(con) {
2348		if (!keep_bootcon && con->flags & CON_BOOT) {
2349			printk(KERN_INFO "turn off boot console %s%d\n",
2350				con->name, con->index);
2351			unregister_console(con);
2352		}
2353	}
2354	hotcpu_notifier(console_cpu_notify, 0);
2355	return 0;
2356}
2357late_initcall(printk_late_init);
2358
2359#if defined CONFIG_PRINTK
2360
2361int printk_sched(const char *fmt, ...)
2362{
2363	unsigned long flags;
2364	va_list args;
2365	char *buf;
2366	int r;
2367
2368	local_irq_save(flags);
2369	buf = __get_cpu_var(printk_sched_buf);
2370
2371	va_start(args, fmt);
2372	r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
2373	va_end(args);
2374
2375	__this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
2376	local_irq_restore(flags);
2377
2378	return r;
2379}
2380
2381/*
2382 * printk rate limiting, lifted from the networking subsystem.
2383 *
2384 * This enforces a rate limit: not more than 10 kernel messages
2385 * every 5s to make a denial-of-service attack impossible.
2386 */
2387DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2388
2389int __printk_ratelimit(const char *func)
2390{
2391	return ___ratelimit(&printk_ratelimit_state, func);
2392}
2393EXPORT_SYMBOL(__printk_ratelimit);
2394
2395/**
2396 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2397 * @caller_jiffies: pointer to caller's state
2398 * @interval_msecs: minimum interval between prints
2399 *
2400 * printk_timed_ratelimit() returns true if more than @interval_msecs
2401 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2402 * returned true.
2403 */
2404bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2405			unsigned int interval_msecs)
2406{
2407	if (*caller_jiffies == 0
2408			|| !time_in_range(jiffies, *caller_jiffies,
2409					*caller_jiffies
2410					+ msecs_to_jiffies(interval_msecs))) {
2411		*caller_jiffies = jiffies;
2412		return true;
2413	}
2414	return false;
2415}
2416EXPORT_SYMBOL(printk_timed_ratelimit);
2417
2418static DEFINE_SPINLOCK(dump_list_lock);
2419static LIST_HEAD(dump_list);
2420
2421/**
2422 * kmsg_dump_register - register a kernel log dumper.
2423 * @dumper: pointer to the kmsg_dumper structure
2424 *
2425 * Adds a kernel log dumper to the system. The dump callback in the
2426 * structure will be called when the kernel oopses or panics and must be
2427 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2428 */
2429int kmsg_dump_register(struct kmsg_dumper *dumper)
2430{
2431	unsigned long flags;
2432	int err = -EBUSY;
2433
2434	/* The dump callback needs to be set */
2435	if (!dumper->dump)
2436		return -EINVAL;
2437
2438	spin_lock_irqsave(&dump_list_lock, flags);
2439	/* Don't allow registering multiple times */
2440	if (!dumper->registered) {
2441		dumper->registered = 1;
2442		list_add_tail_rcu(&dumper->list, &dump_list);
2443		err = 0;
2444	}
2445	spin_unlock_irqrestore(&dump_list_lock, flags);
2446
2447	return err;
2448}
2449EXPORT_SYMBOL_GPL(kmsg_dump_register);
2450
2451/**
2452 * kmsg_dump_unregister - unregister a kmsg dumper.
2453 * @dumper: pointer to the kmsg_dumper structure
2454 *
2455 * Removes a dump device from the system. Returns zero on success and
2456 * %-EINVAL otherwise.
2457 */
2458int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2459{
2460	unsigned long flags;
2461	int err = -EINVAL;
2462
2463	spin_lock_irqsave(&dump_list_lock, flags);
2464	if (dumper->registered) {
2465		dumper->registered = 0;
2466		list_del_rcu(&dumper->list);
2467		err = 0;
2468	}
2469	spin_unlock_irqrestore(&dump_list_lock, flags);
2470	synchronize_rcu();
2471
2472	return err;
2473}
2474EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2475
2476static bool always_kmsg_dump;
2477module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2478
2479/**
2480 * kmsg_dump - dump kernel log to kernel message dumpers.
2481 * @reason: the reason (oops, panic etc) for dumping
2482 *
2483 * Call each of the registered dumper's dump() callback, which can
2484 * retrieve the kmsg records with kmsg_dump_get_line() or
2485 * kmsg_dump_get_buffer().
2486 */
2487void kmsg_dump(enum kmsg_dump_reason reason)
2488{
2489	struct kmsg_dumper *dumper;
2490	unsigned long flags;
2491
2492	if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2493		return;
2494
2495	rcu_read_lock();
2496	list_for_each_entry_rcu(dumper, &dump_list, list) {
2497		if (dumper->max_reason && reason > dumper->max_reason)
2498			continue;
2499
2500		/* initialize iterator with data about the stored records */
2501		dumper->active = true;
2502
2503		raw_spin_lock_irqsave(&logbuf_lock, flags);
2504		dumper->cur_seq = clear_seq;
2505		dumper->cur_idx = clear_idx;
2506		dumper->next_seq = log_next_seq;
2507		dumper->next_idx = log_next_idx;
2508		raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2509
2510		/* invoke dumper which will iterate over records */
2511		dumper->dump(dumper, reason);
2512
2513		/* reset iterator */
2514		dumper->active = false;
2515	}
2516	rcu_read_unlock();
2517}
2518
2519/**
2520 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2521 * @dumper: registered kmsg dumper
2522 * @syslog: include the "<4>" prefixes
2523 * @line: buffer to copy the line to
2524 * @size: maximum size of the buffer
2525 * @len: length of line placed into buffer
2526 *
2527 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2528 * record, and copy one record into the provided buffer.
2529 *
2530 * Consecutive calls will return the next available record moving
2531 * towards the end of the buffer with the youngest messages.
2532 *
2533 * A return value of FALSE indicates that there are no more records to
2534 * read.
2535 *
2536 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2537 */
2538bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2539			       char *line, size_t size, size_t *len)
2540{
2541	struct log *msg;
2542	size_t l = 0;
2543	bool ret = false;
2544
2545	if (!dumper->active)
2546		goto out;
2547
2548	if (dumper->cur_seq < log_first_seq) {
2549		/* messages are gone, move to first available one */
2550		dumper->cur_seq = log_first_seq;
2551		dumper->cur_idx = log_first_idx;
2552	}
2553
2554	/* last entry */
2555	if (dumper->cur_seq >= log_next_seq)
2556		goto out;
2557
2558	msg = log_from_idx(dumper->cur_idx);
2559	l = msg_print_text(msg, 0, syslog, line, size);
2560
2561	dumper->cur_idx = log_next(dumper->cur_idx);
2562	dumper->cur_seq++;
2563	ret = true;
2564out:
2565	if (len)
2566		*len = l;
2567	return ret;
2568}
2569
2570/**
2571 * kmsg_dump_get_line - retrieve one kmsg log line
2572 * @dumper: registered kmsg dumper
2573 * @syslog: include the "<4>" prefixes
2574 * @line: buffer to copy the line to
2575 * @size: maximum size of the buffer
2576 * @len: length of line placed into buffer
2577 *
2578 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2579 * record, and copy one record into the provided buffer.
2580 *
2581 * Consecutive calls will return the next available record moving
2582 * towards the end of the buffer with the youngest messages.
2583 *
2584 * A return value of FALSE indicates that there are no more records to
2585 * read.
2586 */
2587bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2588			char *line, size_t size, size_t *len)
2589{
2590	unsigned long flags;
2591	bool ret;
2592
2593	raw_spin_lock_irqsave(&logbuf_lock, flags);
2594	ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
2595	raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2596
2597	return ret;
2598}
2599EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2600
2601/**
2602 * kmsg_dump_get_buffer - copy kmsg log lines
2603 * @dumper: registered kmsg dumper
2604 * @syslog: include the "<4>" prefixes
2605 * @buf: buffer to copy the line to
2606 * @size: maximum size of the buffer
2607 * @len: length of line placed into buffer
2608 *
2609 * Start at the end of the kmsg buffer and fill the provided buffer
2610 * with as many of the the *youngest* kmsg records that fit into it.
2611 * If the buffer is large enough, all available kmsg records will be
2612 * copied with a single call.
2613 *
2614 * Consecutive calls will fill the buffer with the next block of
2615 * available older records, not including the earlier retrieved ones.
2616 *
2617 * A return value of FALSE indicates that there are no more records to
2618 * read.
2619 */
2620bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2621			  char *buf, size_t size, size_t *len)
2622{
2623	unsigned long flags;
2624	u64 seq;
2625	u32 idx;
2626	u64 next_seq;
2627	u32 next_idx;
2628	enum log_flags prev;
2629	size_t l = 0;
2630	bool ret = false;
2631
2632	if (!dumper->active)
2633		goto out;
2634
2635	raw_spin_lock_irqsave(&logbuf_lock, flags);
2636	if (dumper->cur_seq < log_first_seq) {
2637		/* messages are gone, move to first available one */
2638		dumper->cur_seq = log_first_seq;
2639		dumper->cur_idx = log_first_idx;
2640	}
2641
2642	/* last entry */
2643	if (dumper->cur_seq >= dumper->next_seq) {
2644		raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2645		goto out;
2646	}
2647
2648	/* calculate length of entire buffer */
2649	seq = dumper->cur_seq;
2650	idx = dumper->cur_idx;
2651	prev = 0;
2652	while (seq < dumper->next_seq) {
2653		struct log *msg = log_from_idx(idx);
2654
2655		l += msg_print_text(msg, prev, true, NULL, 0);
2656		idx = log_next(idx);
2657		seq++;
2658		prev = msg->flags;
2659	}
2660
2661	/* move first record forward until length fits into the buffer */
2662	seq = dumper->cur_seq;
2663	idx = dumper->cur_idx;
2664	prev = 0;
2665	while (l > size && seq < dumper->next_seq) {
2666		struct log *msg = log_from_idx(idx);
2667
2668		l -= msg_print_text(msg, prev, true, NULL, 0);
2669		idx = log_next(idx);
2670		seq++;
2671		prev = msg->flags;
2672	}
2673
2674	/* last message in next interation */
2675	next_seq = seq;
2676	next_idx = idx;
2677
2678	l = 0;
2679	prev = 0;
2680	while (seq < dumper->next_seq) {
2681		struct log *msg = log_from_idx(idx);
2682
2683		l += msg_print_text(msg, prev, syslog, buf + l, size - l);
2684		idx = log_next(idx);
2685		seq++;
2686		prev = msg->flags;
2687	}
2688
2689	dumper->next_seq = next_seq;
2690	dumper->next_idx = next_idx;
2691	ret = true;
2692	raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2693out:
2694	if (len)
2695		*len = l;
2696	return ret;
2697}
2698EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2699
2700/**
2701 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2702 * @dumper: registered kmsg dumper
2703 *
2704 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2705 * kmsg_dump_get_buffer() can be called again and used multiple
2706 * times within the same dumper.dump() callback.
2707 *
2708 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2709 */
2710void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2711{
2712	dumper->cur_seq = clear_seq;
2713	dumper->cur_idx = clear_idx;
2714	dumper->next_seq = log_next_seq;
2715	dumper->next_idx = log_next_idx;
2716}
2717
2718/**
2719 * kmsg_dump_rewind - reset the interator
2720 * @dumper: registered kmsg dumper
2721 *
2722 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2723 * kmsg_dump_get_buffer() can be called again and used multiple
2724 * times within the same dumper.dump() callback.
2725 */
2726void kmsg_dump_rewind(struct kmsg_dumper *dumper)
2727{
2728	unsigned long flags;
2729
2730	raw_spin_lock_irqsave(&logbuf_lock, flags);
2731	kmsg_dump_rewind_nolock(dumper);
2732	raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2733}
2734EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
2735#endif