blob: a4280499db0f57ae2f71c70a8ef8ad79b9606f77 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Jesper Juhl40dc5652005-10-30 15:02:46 -080013 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
Christian Kujau624dffc2006-01-15 02:43:54 +010014 * manfred@colorfullife.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Rewrote bits to get rid of console_lock
Francois Camie1f8e872008-10-15 22:01:59 -070016 * 01Mar01 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/console.h>
24#include <linux/init.h>
Randy Dunlapbfe8df32007-10-16 01:23:46 -070025#include <linux/jiffies.h>
26#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Jan Engelhardt3b9c0412006-06-25 05:48:15 -070028#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h> /* For in_interrupt() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
31#include <linux/smp.h>
32#include <linux/security.h>
33#include <linux/bootmem.h>
Mike Travis162a7e72011-05-24 17:13:20 -070034#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/syscalls.h>
Neil Horman04d491a2009-04-02 16:58:57 -070036#include <linux/kexec.h>
Jason Wesseld37d39a2010-05-20 21:04:27 -050037#include <linux/kdb.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020038#include <linux/ratelimit.h>
Simon Kagstrom456b5652009-10-16 14:09:18 +020039#include <linux/kmsg_dump.h>
Kees Cook00234592010-02-03 15:36:43 -080040#include <linux/syslog.h>
Kevin Cernekee034260d2010-06-03 22:11:25 -070041#include <linux/cpu.h>
42#include <linux/notifier.h>
Huang Yingfb842b02011-01-12 16:59:43 -080043#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
46
Johannes Berg95100352011-11-24 20:03:08 +010047#define CREATE_TRACE_POINTS
48#include <trace/events/printk.h>
49
Ingo Molnar076f9772008-01-30 13:33:06 +010050/*
51 * Architectures can override it:
52 */
Jiri Slabye17ba732008-05-12 15:44:40 +020053void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
Ingo Molnar076f9772008-01-30 13:33:06 +010054{
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
58
Tony Lindgren109a3af2005-05-09 14:10:26 -070059#ifdef CONFIG_DEBUG_LL
60extern void printascii(char *);
61#endif
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* printk's without a loglevel use this.. */
Mandeep Singh Baines5af5bcb2011-03-22 16:34:23 -070064#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* We show everything that is MORE important than this.. */
67#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
68#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
69
70DECLARE_WAIT_QUEUE_HEAD(log_wait);
71
72int console_printk[4] = {
73 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
74 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
75 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
76 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010080 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * their unblank() callback or not. So let's export it.
82 */
83int oops_in_progress;
84EXPORT_SYMBOL(oops_in_progress);
85
86/*
87 * console_sem protects the console_drivers list, and also
88 * provides serialisation for access to the entire console
89 * driver system.
90 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000091static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020093EXPORT_SYMBOL_GPL(console_drivers);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * This is used for debugging the mess that is the VT code by
97 * keeping track if we have the console semaphore held. It's
98 * definitely not the perfect debug tool (we don't know if _WE_
99 * hold it are racing, but it helps tracking those weird code
100 * path in the console code where we end up in places I want
101 * locked without the console sempahore held
102 */
Linus Torvalds557240b2006-06-19 18:16:01 -0700103static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
106 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
107 * It is also used in interesting ways to provide interlocking in
Torben Hohnac751ef2011-01-25 15:07:35 -0800108 * console_unlock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200110static DEFINE_RAW_SPINLOCK(logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800112#define LOG_BUF_MASK (log_buf_len-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
114
115/*
116 * The indices into log_buf are not constrained to log_buf_len - they
117 * must be masked before subscripting
118 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800119static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
120static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
121static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700124 * If exclusive_console is non-NULL then only this console is to be printed to.
125 */
126static struct console *exclusive_console;
127
128/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * Array of consoles built from command line options (console=)
130 */
131struct console_cmdline
132{
133 char name[8]; /* Name of the driver */
134 int index; /* Minor dev. to use */
135 char *options; /* Options for the driver */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700136#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
137 char *brl_options; /* Options for braille driver */
138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141#define MAX_CMDLINECONSOLES 8
142
143static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
144static int selected_console = -1;
145static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100146int console_set_on_cmdline;
147EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149/* Flag: console code may call schedule() */
150static int console_may_schedule;
151
Matt Mackalld59745c2005-05-01 08:59:02 -0700152#ifdef CONFIG_PRINTK
153
154static char __log_buf[__LOG_BUF_LEN];
155static char *log_buf = __log_buf;
156static int log_buf_len = __LOG_BUF_LEN;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800157static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
Gustavo F. Padovancea83882010-03-05 13:42:58 -0800158static int saved_console_loglevel = -1;
Matt Mackalld59745c2005-05-01 08:59:02 -0700159
Neil Horman04d491a2009-04-02 16:58:57 -0700160#ifdef CONFIG_KEXEC
161/*
162 * This appends the listed symbols to /proc/vmcoreinfo
163 *
164 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
165 * obtain access to symbols that are otherwise very difficult to locate. These
166 * symbols are specifically used so that utilities can access and extract the
167 * dmesg log from a vmcore file after a crash.
168 */
169void log_buf_kexec_setup(void)
170{
171 VMCOREINFO_SYMBOL(log_buf);
172 VMCOREINFO_SYMBOL(log_end);
173 VMCOREINFO_SYMBOL(log_buf_len);
174 VMCOREINFO_SYMBOL(logged_chars);
175}
176#endif
177
Mike Travis162a7e72011-05-24 17:13:20 -0700178/* requested log_buf_len from kernel cmdline */
179static unsigned long __initdata new_log_buf_len;
180
181/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static int __init log_buf_len_setup(char *str)
183{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800184 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if (size)
187 size = roundup_pow_of_two(size);
Mike Travis162a7e72011-05-24 17:13:20 -0700188 if (size > log_buf_len)
189 new_log_buf_len = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Mike Travis162a7e72011-05-24 17:13:20 -0700191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Mike Travis162a7e72011-05-24 17:13:20 -0700193early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Mike Travis162a7e72011-05-24 17:13:20 -0700195void __init setup_log_buf(int early)
196{
197 unsigned long flags;
198 unsigned start, dest_idx, offset;
199 char *new_log_buf;
200 int free;
201
202 if (!new_log_buf_len)
203 return;
204
205 if (early) {
206 unsigned long mem;
207
208 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200209 if (!mem)
Mike Travis162a7e72011-05-24 17:13:20 -0700210 return;
211 new_log_buf = __va(mem);
212 } else {
213 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
214 }
215
216 if (unlikely(!new_log_buf)) {
217 pr_err("log_buf_len: %ld bytes not available\n",
218 new_log_buf_len);
219 return;
220 }
221
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200222 raw_spin_lock_irqsave(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700223 log_buf_len = new_log_buf_len;
224 log_buf = new_log_buf;
225 new_log_buf_len = 0;
226 free = __LOG_BUF_LEN - log_end;
227
228 offset = start = min(con_start, log_start);
229 dest_idx = 0;
230 while (start != log_end) {
231 unsigned log_idx_mask = start & (__LOG_BUF_LEN - 1);
232
233 log_buf[dest_idx] = __log_buf[log_idx_mask];
234 start++;
235 dest_idx++;
236 }
237 log_start -= offset;
238 con_start -= offset;
239 log_end -= offset;
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200240 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700241
242 pr_info("log_buf_len: %d\n", log_buf_len);
243 pr_info("early log buf free: %d(%d%%)\n",
244 free, (free * 100) / __LOG_BUF_LEN);
245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700247#ifdef CONFIG_BOOT_PRINTK_DELAY
248
Namhyung Kim674dff62010-10-26 14:22:48 -0700249static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700250static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700251
252static int __init boot_delay_setup(char *str)
253{
254 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700255
256 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
257 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
258
259 get_option(&str, &boot_delay);
260 if (boot_delay > 10 * 1000)
261 boot_delay = 0;
262
Dave Young3a3b6ed2009-09-22 16:43:31 -0700263 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
264 "HZ: %d, loops_per_msec: %llu\n",
265 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700266 return 1;
267}
268__setup("boot_delay=", boot_delay_setup);
269
270static void boot_delay_msec(void)
271{
272 unsigned long long k;
273 unsigned long timeout;
274
275 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
276 return;
277
Dave Young3a3b6ed2009-09-22 16:43:31 -0700278 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700279
280 timeout = jiffies + msecs_to_jiffies(boot_delay);
281 while (k) {
282 k--;
283 cpu_relax();
284 /*
285 * use (volatile) jiffies to prevent
286 * compiler reduction; loop termination via jiffies
287 * is secondary and may or may not happen.
288 */
289 if (time_after(jiffies, timeout))
290 break;
291 touch_nmi_watchdog();
292 }
293}
294#else
295static inline void boot_delay_msec(void)
296{
297}
298#endif
299
Arve Hjønnevåg572b2622009-01-15 19:07:27 -0800300/*
301 * Return the number of unread characters in the log buffer.
302 */
303static int log_buf_get_len(void)
304{
305 return logged_chars;
306}
307
308/*
San Mehatda8de7a2009-08-06 10:25:21 -0700309 * Clears the ring-buffer
310 */
311void log_buf_clear(void)
312{
313 logged_chars = 0;
314}
315
316/*
Arve Hjønnevåg572b2622009-01-15 19:07:27 -0800317 * Copy a range of characters from the log buffer.
318 */
319int log_buf_copy(char *dest, int idx, int len)
320{
321 int ret, max;
322 bool took_lock = false;
323
324 if (!oops_in_progress) {
Colin Cross609a71d2012-02-01 16:02:23 -0800325 raw_spin_lock_irq(&logbuf_lock);
Arve Hjønnevåg572b2622009-01-15 19:07:27 -0800326 took_lock = true;
327 }
328
329 max = log_buf_get_len();
330 if (idx < 0 || idx >= max) {
331 ret = -1;
332 } else {
Arve Hjønnevågee51cfa2008-04-14 21:35:25 -0700333 if (len > max - idx)
334 len = max - idx;
Arve Hjønnevåg572b2622009-01-15 19:07:27 -0800335 ret = len;
336 idx += (log_end - max);
337 while (len-- > 0)
338 dest[len] = LOG_BUF(idx + len);
339 }
340
341 if (took_lock)
Colin Cross609a71d2012-02-01 16:02:23 -0800342 raw_spin_unlock_irq(&logbuf_lock);
Arve Hjønnevåg572b2622009-01-15 19:07:27 -0800343
344 return ret;
345}
346
Dan Rosenbergeaf06b22010-11-11 14:05:18 -0800347#ifdef CONFIG_SECURITY_DMESG_RESTRICT
348int dmesg_restrict = 1;
349#else
350int dmesg_restrict;
351#endif
352
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800353static int syslog_action_restricted(int type)
354{
355 if (dmesg_restrict)
356 return 1;
357 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
358 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
359}
360
361static int check_syslog_permissions(int type, bool from_file)
362{
363 /*
364 * If this is from /proc/kmsg and we've already opened it, then we've
365 * already done the capabilities checks at open time.
366 */
367 if (from_file && type != SYSLOG_ACTION_OPEN)
368 return 0;
369
370 if (syslog_action_restricted(type)) {
371 if (capable(CAP_SYSLOG))
372 return 0;
373 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
374 if (capable(CAP_SYS_ADMIN)) {
Jonathan Niederf2c0d022011-08-08 06:22:43 +0200375 printk_once(KERN_WARNING "%s (%d): "
376 "Attempt to access syslog with CAP_SYS_ADMIN "
377 "but no CAP_SYSLOG (deprecated).\n",
378 current->comm, task_pid_nr(current));
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800379 return 0;
380 }
381 return -EPERM;
382 }
383 return 0;
384}
385
Kees Cook00234592010-02-03 15:36:43 -0800386int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800388 unsigned i, j, limit, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int do_clear = 0;
390 char c;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800391 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800393 error = check_syslog_permissions(type, from_file);
394 if (error)
395 goto out;
Eric Paris12b30522010-11-15 18:36:29 -0500396
397 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (error)
399 return error;
400
401 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -0800402 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800404 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800406 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 error = -EINVAL;
408 if (!buf || len < 0)
409 goto out;
410 error = 0;
411 if (!len)
412 goto out;
413 if (!access_ok(VERIFY_WRITE, buf, len)) {
414 error = -EFAULT;
415 goto out;
416 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800417 error = wait_event_interruptible(log_wait,
418 (log_start - log_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (error)
420 goto out;
421 i = 0;
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200422 raw_spin_lock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 while (!error && (log_start != log_end) && i < len) {
424 c = LOG_BUF(log_start);
425 log_start++;
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200426 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 error = __put_user(c,buf);
428 buf++;
429 i++;
430 cond_resched();
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200431 raw_spin_lock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200433 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (!error)
435 error = i;
436 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800437 /* Read/clear last kernel messages */
438 case SYSLOG_ACTION_READ_CLEAR:
Jesper Juhl40dc5652005-10-30 15:02:46 -0800439 do_clear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -0800441 /* Read last kernel messages */
442 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 error = -EINVAL;
444 if (!buf || len < 0)
445 goto out;
446 error = 0;
447 if (!len)
448 goto out;
449 if (!access_ok(VERIFY_WRITE, buf, len)) {
450 error = -EFAULT;
451 goto out;
452 }
453 count = len;
454 if (count > log_buf_len)
455 count = log_buf_len;
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200456 raw_spin_lock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (count > logged_chars)
458 count = logged_chars;
459 if (do_clear)
460 logged_chars = 0;
461 limit = log_end;
462 /*
463 * __put_user() could sleep, and while we sleep
Jesper Juhl40dc5652005-10-30 15:02:46 -0800464 * printk() could overwrite the messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * we try to copy to user space. Therefore
466 * the messages are copied in reverse. <manfreds>
467 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800468 for (i = 0; i < count && !error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 j = limit-1-i;
470 if (j + log_buf_len < log_end)
471 break;
472 c = LOG_BUF(j);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200473 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 error = __put_user(c,&buf[count-1-i]);
475 cond_resched();
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200476 raw_spin_lock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200478 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (error)
480 break;
481 error = i;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800482 if (i != count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int offset = count-error;
484 /* buffer overflow during copy, correct user buffer. */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800485 for (i = 0; i < error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (__get_user(c,&buf[i+offset]) ||
487 __put_user(c,&buf[i])) {
488 error = -EFAULT;
489 break;
490 }
491 cond_resched();
492 }
493 }
494 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800495 /* Clear ring buffer */
496 case SYSLOG_ACTION_CLEAR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 logged_chars = 0;
498 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800499 /* Disable logging to console */
500 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +0200501 if (saved_console_loglevel == -1)
502 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 console_loglevel = minimum_console_loglevel;
504 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800505 /* Enable logging to console */
506 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +0200507 if (saved_console_loglevel != -1) {
508 console_loglevel = saved_console_loglevel;
509 saved_console_loglevel = -1;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800512 /* Set level of messages printed to console */
513 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 error = -EINVAL;
515 if (len < 1 || len > 8)
516 goto out;
517 if (len < minimum_console_loglevel)
518 len = minimum_console_loglevel;
519 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +0200520 /* Implicitly re-enable logging to console */
521 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 error = 0;
523 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800524 /* Number of chars in the log buffer */
525 case SYSLOG_ACTION_SIZE_UNREAD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 error = log_end - log_start;
527 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800528 /* Size of the log buffer */
529 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 error = log_buf_len;
531 break;
532 default:
533 error = -EINVAL;
534 break;
535 }
536out:
537 return error;
538}
539
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100540SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Kees Cook00234592010-02-03 15:36:43 -0800542 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Jason Wessel67fc4e02010-05-20 21:04:21 -0500545#ifdef CONFIG_KGDB_KDB
546/* kdb dmesg command needs access to the syslog buffer. do_syslog()
547 * uses locks so it cannot be used during debugging. Just tell kdb
548 * where the start and end of the physical and logical logs are. This
549 * is equivalent to do_syslog(3).
550 */
551void kdb_syslog_data(char *syslog_data[4])
552{
553 syslog_data[0] = log_buf;
554 syslog_data[1] = log_buf + log_buf_len;
555 syslog_data[2] = log_buf + log_end -
556 (logged_chars < log_buf_len ? logged_chars : log_buf_len);
557 syslog_data[3] = log_buf + log_end;
558}
559#endif /* CONFIG_KGDB_KDB */
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/*
562 * Call the console drivers on a range of log_buf
563 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800564static void __call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct console *con;
567
Robin Getz4d091612009-07-01 21:08:37 -0400568 for_each_console(con) {
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700569 if (exclusive_console && con != exclusive_console)
570 continue;
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700571 if ((con->flags & CON_ENABLED) && con->write &&
572 (cpu_online(smp_processor_id()) ||
573 (con->flags & CON_ANYTIME)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 con->write(con, &LOG_BUF(start), end - start);
575 }
576}
577
Rusty Russell2329abf2012-01-13 09:32:18 +1030578static bool __read_mostly ignore_loglevel;
Ingo Molnar792908222006-12-06 20:40:51 -0800579
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800580static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800581{
582 ignore_loglevel = 1;
583 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
584
Ingo Molnarc4772d92008-01-31 22:45:23 +0100585 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -0800586}
587
Ingo Molnarc4772d92008-01-31 22:45:23 +0100588early_param("ignore_loglevel", ignore_loglevel_setup);
Rusty Russell29d4d6d2012-01-13 09:32:17 +1030589module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
Yanmin Zhang0eca6b72011-10-31 17:11:25 -0700590MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
591 "print all kernel messages to the console.");
Ingo Molnar792908222006-12-06 20:40:51 -0800592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
594 * Write out chars from start to end - 1 inclusive
595 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800596static void _call_console_drivers(unsigned start,
597 unsigned end, int msg_log_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Johannes Berg95100352011-11-24 20:03:08 +0100599 trace_console(&LOG_BUF(0), start, end, log_buf_len);
600
Ingo Molnar792908222006-12-06 20:40:51 -0800601 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 console_drivers && start != end) {
603 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
604 /* wrapped write */
605 __call_console_drivers(start & LOG_BUF_MASK,
606 log_buf_len);
607 __call_console_drivers(0, end & LOG_BUF_MASK);
608 } else {
609 __call_console_drivers(start, end);
610 }
611 }
612}
613
614/*
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100615 * Parse the syslog header <[0-9]*>. The decimal value represents 32bit, the
616 * lower 3 bit are the log level, the rest are the log facility. In case
617 * userspace passes usual userspace syslog messages to /dev/kmsg or
618 * /dev/ttyprintk, the log prefix might contain the facility. Printk needs
619 * to extract the correct log level for in-kernel processing, and not mangle
620 * the original value.
621 *
622 * If a prefix is found, the length of the prefix is returned. If 'level' is
623 * passed, it will be filled in with the log level without a possible facility
624 * value. If 'special' is passed, the special printk prefix chars are accepted
625 * and returned. If no valid header is found, 0 is returned and the passed
626 * variables are not touched.
627 */
628static size_t log_prefix(const char *p, unsigned int *level, char *special)
629{
630 unsigned int lev = 0;
631 char sp = '\0';
632 size_t len;
633
634 if (p[0] != '<' || !p[1])
635 return 0;
636 if (p[2] == '>') {
637 /* usual single digit level number or special char */
638 switch (p[1]) {
639 case '0' ... '7':
640 lev = p[1] - '0';
641 break;
642 case 'c': /* KERN_CONT */
643 case 'd': /* KERN_DEFAULT */
644 sp = p[1];
645 break;
646 default:
647 return 0;
648 }
649 len = 3;
650 } else {
651 /* multi digit including the level and facility number */
652 char *endp = NULL;
653
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100654 lev = (simple_strtoul(&p[1], &endp, 10) & 7);
655 if (endp == NULL || endp[0] != '>')
656 return 0;
657 len = (endp + 1) - p;
658 }
659
660 /* do not accept special char if not asked for */
661 if (sp && !special)
662 return 0;
663
664 if (special) {
665 *special = sp;
666 /* return special char, do not touch level */
667 if (sp)
668 return len;
669 }
670
671 if (level)
672 *level = lev;
673 return len;
674}
675
676/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * Call the console drivers, asking them to write out
678 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -0800679 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800681static void call_console_drivers(unsigned start, unsigned end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800683 unsigned cur_index, start_print;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 static int msg_level = -1;
685
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800686 BUG_ON(((int)(start - end)) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 cur_index = start;
689 start_print = start;
690 while (cur_index != end) {
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100691 if (msg_level < 0 && ((end - cur_index) > 2)) {
692 /* strip log prefix */
693 cur_index += log_prefix(&LOG_BUF(cur_index), &msg_level, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 start_print = cur_index;
695 }
696 while (cur_index != end) {
697 char c = LOG_BUF(cur_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jesper Juhl40dc5652005-10-30 15:02:46 -0800699 cur_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (c == '\n') {
701 if (msg_level < 0) {
702 /*
703 * printk() has already given us loglevel tags in
704 * the buffer. This code is here in case the
705 * log buffer has wrapped right round and scribbled
706 * on those tags
707 */
708 msg_level = default_message_loglevel;
709 }
710 _call_console_drivers(start_print, cur_index, msg_level);
711 msg_level = -1;
712 start_print = cur_index;
713 break;
714 }
715 }
716 }
717 _call_console_drivers(start_print, end, msg_level);
718}
719
720static void emit_log_char(char c)
721{
722 LOG_BUF(log_end) = c;
723 log_end++;
724 if (log_end - log_start > log_buf_len)
725 log_start = log_end - log_buf_len;
726 if (log_end - con_start > log_buf_len)
727 con_start = log_end - log_buf_len;
728 if (logged_chars < log_buf_len)
729 logged_chars++;
730}
731
732/*
733 * Zap console related locks when oopsing. Only zap at most once
734 * every 10 seconds, to leave time for slow consoles to print a
735 * full oops.
736 */
737static void zap_locks(void)
738{
739 static unsigned long oops_timestamp;
740
741 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800742 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return;
744
745 oops_timestamp = jiffies;
746
Peter Zijlstra94d24fc2011-06-07 11:17:30 +0200747 debug_locks_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* If a crash is occurring, make sure we can't deadlock */
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200749 raw_spin_lock_init(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +0000751 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754#if defined(CONFIG_PRINTK_TIME)
Rusty Russell2329abf2012-01-13 09:32:18 +1030755static bool printk_time = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756#else
Rusty Russell2329abf2012-01-13 09:32:18 +1030757static bool printk_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#endif
Randy Dunlape84845c2007-07-15 23:40:25 -0700759module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Matthew Garrettc22ab332012-03-05 14:59:10 -0800761static bool always_kmsg_dump;
762module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
763
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700764/* Check if we have any console registered that can be called early in boot. */
765static int have_callable_console(void)
766{
767 struct console *con;
768
Robin Getz4d091612009-07-01 21:08:37 -0400769 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700770 if (con->flags & CON_ANYTIME)
771 return 1;
772
773 return 0;
774}
775
Martin Waitzddad86c2005-11-13 16:08:14 -0800776/**
777 * printk - print a kernel message
778 * @fmt: format string
779 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800780 * This is printk(). It can be called from any context. We want it to work.
Jesper Juhl40dc5652005-10-30 15:02:46 -0800781 *
Torben Hohnac751ef2011-01-25 15:07:35 -0800782 * We try to grab the console_lock. If we succeed, it's easy - we log the output and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * call the console drivers. If we fail to get the semaphore we place the output
784 * into the log buffer and return. The current holder of the console_sem will
Torben Hohnac751ef2011-01-25 15:07:35 -0800785 * notice the new output in console_unlock(); and will send it to the
786 * consoles before releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 *
788 * One effect of this deferred printing is that code which calls printk() and
789 * then changes console_loglevel may break. This is because console_loglevel
790 * is inspected when the actual printing occurs.
Martin Waitzddad86c2005-11-13 16:08:14 -0800791 *
792 * See also:
793 * printf(3)
Andi Kleen20036fd2008-10-15 22:02:02 -0700794 *
795 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798asmlinkage int printk(const char *fmt, ...)
799{
800 va_list args;
801 int r;
802
Jason Wesseld37d39a2010-05-20 21:04:27 -0500803#ifdef CONFIG_KGDB_KDB
804 if (unlikely(kdb_trap_printk)) {
805 va_start(args, fmt);
806 r = vkdb_printf(fmt, args);
807 va_end(args);
808 return r;
809 }
810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 va_start(args, fmt);
812 r = vprintk(fmt, args);
813 va_end(args);
814
815 return r;
816}
817
David Howellsfe217732005-09-06 15:16:34 -0700818/* cpu currently holding logbuf_lock */
819static volatile unsigned int printk_cpu = UINT_MAX;
820
Linus Torvalds266c2e02008-03-24 19:25:08 -0700821/*
822 * Can we actually use the console at this time on this cpu?
823 *
824 * Console drivers may assume that per-cpu resources have
825 * been allocated. So unless they're explicitly marked as
826 * being able to cope (CON_ANYTIME) don't call them until
827 * this CPU is officially up.
828 */
829static inline int can_use_console(unsigned int cpu)
830{
831 return cpu_online(cpu) || have_callable_console();
832}
833
834/*
835 * Try to get console ownership to actually show the kernel
836 * messages from a 'printk'. Return true (and with the
Torben Hohnac751ef2011-01-25 15:07:35 -0800837 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -0700838 * is successful, false otherwise.
839 *
840 * This gets called with the 'logbuf_lock' spinlock held and
841 * interrupts disabled. It should return with 'lockbuf_lock'
842 * released but interrupts still disabled.
843 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800844static int console_trylock_for_printk(unsigned int cpu)
Namhyung Kim8155c022010-10-26 14:22:47 -0700845 __releases(&logbuf_lock)
Linus Torvalds266c2e02008-03-24 19:25:08 -0700846{
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +0200847 int retval = 0, wake = 0;
Linus Torvalds266c2e02008-03-24 19:25:08 -0700848
Torben Hohnac751ef2011-01-25 15:07:35 -0800849 if (console_trylock()) {
Linus Torvalds093a07e2008-04-15 13:09:54 -0700850 retval = 1;
851
852 /*
853 * If we can't use the console, we need to release
854 * the console semaphore by hand to avoid flushing
855 * the buffer. We need to hold the console semaphore
856 * in order to do this test safely.
857 */
858 if (!can_use_console(cpu)) {
859 console_locked = 0;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +0200860 wake = 1;
Linus Torvalds093a07e2008-04-15 13:09:54 -0700861 retval = 0;
862 }
863 }
Linus Torvalds266c2e02008-03-24 19:25:08 -0700864 printk_cpu = UINT_MAX;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +0200865 if (wake)
866 up(&console_sem);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200867 raw_spin_unlock(&logbuf_lock);
Linus Torvalds266c2e02008-03-24 19:25:08 -0700868 return retval;
869}
Tejun Heo3b8945e2008-05-12 21:21:04 +0200870static const char recursion_bug_msg [] =
871 KERN_CRIT "BUG: recent printk recursion!\n";
872static int recursion_bug;
Jiri Kosinaedb123e2008-12-04 12:39:49 +0100873static int new_text_line = 1;
Tejun Heo3b8945e2008-05-12 21:21:04 +0200874static char printk_buf[1024];
Ingo Molnar32a76002008-01-25 21:07:58 +0100875
Dave Youngaf913222009-09-22 16:43:33 -0700876int printk_delay_msec __read_mostly;
877
878static inline void printk_delay(void)
879{
880 if (unlikely(printk_delay_msec)) {
881 int m = printk_delay_msec;
882
883 while (m--) {
884 mdelay(1);
885 touch_nmi_watchdog();
886 }
887 }
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890asmlinkage int vprintk(const char *fmt, va_list args)
891{
Ingo Molnar32a76002008-01-25 21:07:58 +0100892 int printed_len = 0;
Nick Andrew09159302008-05-12 21:21:04 +0200893 int current_log_level = default_message_loglevel;
Nick Andrewac60ad72008-05-12 21:21:04 +0200894 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +0100895 int this_cpu;
896 char *p;
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100897 size_t plen;
898 char special;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700900 boot_delay_msec();
Dave Youngaf913222009-09-22 16:43:33 -0700901 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /* This stops the holder of console_sem just where we want him */
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +0200904 local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100905 this_cpu = smp_processor_id();
906
907 /*
908 * Ouch, printk recursed into itself!
909 */
910 if (unlikely(printk_cpu == this_cpu)) {
911 /*
912 * If a crash is occurring during printk() on this CPU,
913 * then try to get the crash message out but make sure
914 * we can't deadlock. Otherwise just return to avoid the
915 * recursion and return - but flag the recursion so that
916 * it can be printed at the next appropriate moment:
917 */
Peter Zijlstra94d24fc2011-06-07 11:17:30 +0200918 if (!oops_in_progress && !lockdep_recursing(current)) {
Tejun Heo3b8945e2008-05-12 21:21:04 +0200919 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100920 goto out_restore_irqs;
921 }
922 zap_locks();
923 }
924
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700925 lockdep_off();
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200926 raw_spin_lock(&logbuf_lock);
Ingo Molnar32a76002008-01-25 21:07:58 +0100927 printk_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Tejun Heo3b8945e2008-05-12 21:21:04 +0200929 if (recursion_bug) {
930 recursion_bug = 0;
931 strcpy(printk_buf, recursion_bug_msg);
Hiroshi Shimamoto26cc2712008-12-19 10:23:03 -0800932 printed_len = strlen(recursion_bug_msg);
Ingo Molnar32a76002008-01-25 21:07:58 +0100933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* Emit the output into the temporary buffer */
Ingo Molnar32a76002008-01-25 21:07:58 +0100935 printed_len += vscnprintf(printk_buf + printed_len,
Tejun Heocf3680b2008-02-14 10:32:07 +0900936 sizeof(printk_buf) - printed_len, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Tony Lindgren109a3af2005-05-09 14:10:26 -0700938#ifdef CONFIG_DEBUG_LL
939 printascii(printk_buf);
940#endif
941
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700942 p = printk_buf;
943
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100944 /* Read log level and handle special printk prefix */
945 plen = log_prefix(p, &current_log_level, &special);
946 if (plen) {
947 p += plen;
948
949 switch (special) {
950 case 'c': /* Strip <c> KERN_CONT, continue line */
951 plen = 0;
952 break;
953 case 'd': /* Strip <d> KERN_DEFAULT, start new line */
954 plen = 0;
955 default:
956 if (!new_text_line) {
957 emit_log_char('\n');
958 new_text_line = 1;
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700959 }
960 }
961 }
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /*
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100964 * Copy the output into log_buf. If the caller didn't provide
965 * the appropriate log prefix, we insert them here
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100967 for (; *p; p++) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200968 if (new_text_line) {
Nick Andrewac60ad72008-05-12 21:21:04 +0200969 new_text_line = 0;
970
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100971 if (plen) {
972 /* Copy original log prefix */
973 int i;
974
975 for (i = 0; i < plen; i++)
976 emit_log_char(printk_buf[i]);
977 printed_len += plen;
978 } else {
979 /* Add log prefix */
980 emit_log_char('<');
981 emit_log_char(current_log_level + '0');
982 emit_log_char('>');
983 printed_len += 3;
984 }
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (printk_time) {
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100987 /* Add the current time stamp */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 char tbuf[50], *tp;
989 unsigned tlen;
990 unsigned long long t;
991 unsigned long nanosec_rem;
992
Ingo Molnar326e96b2008-01-27 08:03:54 +0100993 t = cpu_clock(printk_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 nanosec_rem = do_div(t, 1000000000);
Nick Andrewac60ad72008-05-12 21:21:04 +0200995 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
996 (unsigned long) t,
997 nanosec_rem / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 for (tp = tbuf; tp < tbuf + tlen; tp++)
1000 emit_log_char(*tp);
Guillaume Chazarain025510c2006-01-08 01:02:41 -08001001 printed_len += tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (!*p)
1005 break;
1006 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 emit_log_char(*p);
1009 if (*p == '\n')
Nick Andrewac60ad72008-05-12 21:21:04 +02001010 new_text_line = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
Linus Torvalds266c2e02008-03-24 19:25:08 -07001013 /*
1014 * Try to acquire and then immediately release the
1015 * console semaphore. The release will do all the
1016 * actual magic (print out buffers, wake up klogd,
1017 * etc).
1018 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001019 * The console_trylock_for_printk() function
Linus Torvalds266c2e02008-03-24 19:25:08 -07001020 * will release 'logbuf_lock' regardless of whether it
1021 * actually gets the semaphore or not.
1022 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001023 if (console_trylock_for_printk(this_cpu))
1024 console_unlock();
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001025
Linus Torvalds266c2e02008-03-24 19:25:08 -07001026 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +01001027out_restore_irqs:
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001028 local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return printed_len;
1031}
1032EXPORT_SYMBOL(printk);
1033EXPORT_SYMBOL(vprintk);
1034
Matt Mackalld59745c2005-05-01 08:59:02 -07001035#else
1036
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -08001037static void call_console_drivers(unsigned start, unsigned end)
Jesper Juhl40dc5652005-10-30 15:02:46 -08001038{
1039}
Matt Mackalld59745c2005-05-01 08:59:02 -07001040
1041#endif
1042
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001043static int __add_preferred_console(char *name, int idx, char *options,
1044 char *brl_options)
1045{
1046 struct console_cmdline *c;
1047 int i;
1048
1049 /*
1050 * See if this tty is not yet registered, and
1051 * if we have a slot free.
1052 */
1053 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1054 if (strcmp(console_cmdline[i].name, name) == 0 &&
1055 console_cmdline[i].index == idx) {
1056 if (!brl_options)
1057 selected_console = i;
1058 return 0;
1059 }
1060 if (i == MAX_CMDLINECONSOLES)
1061 return -E2BIG;
1062 if (!brl_options)
1063 selected_console = i;
1064 c = &console_cmdline[i];
1065 strlcpy(c->name, name, sizeof(c->name));
1066 c->options = options;
1067#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1068 c->brl_options = brl_options;
1069#endif
1070 c->index = idx;
1071 return 0;
1072}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001073/*
1074 * Set up a list of consoles. Called from init/main.c
1075 */
1076static int __init console_setup(char *str)
1077{
Yinghai Lueaa944a2007-07-15 23:37:27 -07001078 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001079 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001080 int idx;
1081
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001082#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1083 if (!memcmp(str, "brl,", 4)) {
1084 brl_options = "";
1085 str += 4;
1086 } else if (!memcmp(str, "brl=", 4)) {
1087 brl_options = str + 4;
1088 str = strchr(brl_options, ',');
1089 if (!str) {
1090 printk(KERN_ERR "need port name after brl=\n");
1091 return 1;
1092 }
1093 *(str++) = 0;
1094 }
1095#endif
1096
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001097 /*
1098 * Decode str into name, index, options.
1099 */
1100 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001101 strcpy(buf, "ttyS");
1102 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001103 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001104 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001105 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001106 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001107 if ((options = strchr(str, ',')) != NULL)
1108 *(options++) = 0;
1109#ifdef __sparc__
1110 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001111 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001112 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001113 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001114#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001115 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001116 if ((*s >= '0' && *s <= '9') || *s == ',')
1117 break;
1118 idx = simple_strtoul(s, NULL, 10);
1119 *s = 0;
1120
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001121 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001122 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001123 return 1;
1124}
1125__setup("console=", console_setup);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001128 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001129 * @name: device name
1130 * @idx: device index
1131 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001132 *
1133 * The last preferred console added will be used for kernel messages
1134 * and stdin/out/err for init. Normally this is used by console_setup
1135 * above to handle user-supplied console arguments; however it can also
1136 * be used by arch-specific code either to override the user or more
1137 * commonly to provide a default console (ie from PROM variables) when
1138 * the user has not supplied one.
1139 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001140int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001141{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001142 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001143}
1144
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001145int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001146{
1147 struct console_cmdline *c;
1148 int i;
1149
1150 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1151 if (strcmp(console_cmdline[i].name, name) == 0 &&
1152 console_cmdline[i].index == idx) {
1153 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -07001154 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001155 c->name[sizeof(c->name) - 1] = 0;
1156 c->options = options;
1157 c->index = idx_new;
1158 return i;
1159 }
1160 /* not found */
1161 return -1;
1162}
1163
Rusty Russell2329abf2012-01-13 09:32:18 +10301164bool console_suspend_enabled = 1;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001165EXPORT_SYMBOL(console_suspend_enabled);
1166
1167static int __init console_suspend_disable(char *str)
1168{
1169 console_suspend_enabled = 0;
1170 return 1;
1171}
1172__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07001173module_param_named(console_suspend, console_suspend_enabled,
1174 bool, S_IRUGO | S_IWUSR);
1175MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1176 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001177
Matt Mackall3c0547b2005-05-16 21:53:47 -07001178/**
Linus Torvalds557240b2006-06-19 18:16:01 -07001179 * suspend_console - suspend the console subsystem
1180 *
1181 * This disables printk() while we go into suspend states
1182 */
1183void suspend_console(void)
1184{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001185 if (!console_suspend_enabled)
1186 return;
Pavel Machek0d630812008-07-23 21:28:32 -07001187 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08001188 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001189 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001190 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001191}
1192
1193void resume_console(void)
1194{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001195 if (!console_suspend_enabled)
1196 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001197 down(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001198 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001199 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001200}
1201
1202/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07001203 * console_cpu_notify - print deferred console messages after CPU hotplug
1204 * @self: notifier struct
1205 * @action: CPU hotplug event
1206 * @hcpu: unused
1207 *
1208 * If printk() is called from a CPU that is not online yet, the messages
1209 * will be spooled but will not show up on the console. This function is
1210 * called when a new CPU comes online (or fails to come up), and ensures
1211 * that any such output gets printed.
1212 */
1213static int __cpuinit console_cpu_notify(struct notifier_block *self,
1214 unsigned long action, void *hcpu)
1215{
1216 switch (action) {
1217 case CPU_ONLINE:
1218 case CPU_DEAD:
Kevin Cernekee034260d2010-06-03 22:11:25 -07001219 case CPU_DOWN_FAILED:
1220 case CPU_UP_CANCELED:
Torben Hohnac751ef2011-01-25 15:07:35 -08001221 console_lock();
1222 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07001223 }
1224 return NOTIFY_OK;
1225}
1226
1227/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001228 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001230 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * exclusive access to the console system and the console_drivers list.
1232 *
1233 * Can sleep, returns nothing.
1234 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001235void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +02001237 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001239 if (console_suspended)
1240 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 console_locked = 1;
1242 console_may_schedule = 1;
1243}
Torben Hohnac751ef2011-01-25 15:07:35 -08001244EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Torben Hohnac751ef2011-01-25 15:07:35 -08001246/**
1247 * console_trylock - try to lock the console system for exclusive use.
1248 *
1249 * Tried to acquire a lock which guarantees that the caller has
1250 * exclusive access to the console system and the console_drivers list.
1251 *
1252 * returns 1 on success, and 0 on failure to acquire the lock.
1253 */
1254int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 if (down_trylock(&console_sem))
Torben Hohnac751ef2011-01-25 15:07:35 -08001257 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001258 if (console_suspended) {
1259 up(&console_sem);
Torben Hohnac751ef2011-01-25 15:07:35 -08001260 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 console_locked = 1;
1263 console_may_schedule = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001264 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
Torben Hohnac751ef2011-01-25 15:07:35 -08001266EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268int is_console_locked(void)
1269{
1270 return console_locked;
1271}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001273/*
1274 * Delayed printk facility, for scheduler-internal messages:
1275 */
1276#define PRINTK_BUF_SIZE 512
1277
1278#define PRINTK_PENDING_WAKEUP 0x01
1279#define PRINTK_PENDING_SCHED 0x02
1280
Peter Zijlstrab845b512008-08-08 21:47:09 +02001281static DEFINE_PER_CPU(int, printk_pending);
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001282static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001283
1284void printk_tick(void)
1285{
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001286 if (__this_cpu_read(printk_pending)) {
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001287 int pending = __this_cpu_xchg(printk_pending, 0);
1288 if (pending & PRINTK_PENDING_SCHED) {
1289 char *buf = __get_cpu_var(printk_sched_buf);
1290 printk(KERN_WARNING "[sched_delayed] %s", buf);
1291 }
1292 if (pending & PRINTK_PENDING_WAKEUP)
1293 wake_up_interruptible(&log_wait);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001294 }
1295}
1296
1297int printk_needs_cpu(int cpu)
1298{
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001299 if (cpu_is_offline(cpu))
Heiko Carstens61ab2542010-11-26 13:00:59 +01001300 printk_tick();
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001301 return __this_cpu_read(printk_pending);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001302}
1303
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001304void wake_up_klogd(void)
1305{
Peter Zijlstrab845b512008-08-08 21:47:09 +02001306 if (waitqueue_active(&log_wait))
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001307 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001311 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001313 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * and the console driver list.
1315 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001316 * While the console_lock was held, console output may have been buffered
1317 * by printk(). If this is the case, console_unlock(); emits
1318 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 *
1320 * If there is output waiting for klogd, we wake it up.
1321 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001322 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001324void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 unsigned long flags;
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -08001327 unsigned _con_start, _log_end;
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001328 unsigned wake_klogd = 0, retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Linus Torvalds557240b2006-06-19 18:16:01 -07001330 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001331 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001332 return;
1333 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07001334
1335 console_may_schedule = 0;
1336
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001337again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 for ( ; ; ) {
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001339 raw_spin_lock_irqsave(&logbuf_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 wake_klogd |= log_start - log_end;
1341 if (con_start == log_end)
1342 break; /* Nothing to print */
1343 _con_start = con_start;
1344 _log_end = log_end;
1345 con_start = log_end; /* Flush */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001346 raw_spin_unlock(&logbuf_lock);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001347 stop_critical_timings(); /* don't trace print latency */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 call_console_drivers(_con_start, _log_end);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001349 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 local_irq_restore(flags);
1351 }
1352 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07001353
1354 /* Release the exclusive_console once it is used */
1355 if (unlikely(exclusive_console))
1356 exclusive_console = NULL;
1357
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001358 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001359
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001360 up(&console_sem);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001361
1362 /*
1363 * Someone could have filled up the buffer again, so re-check if there's
1364 * something to flush. In case we cannot trylock the console_sem again,
1365 * there's a new owner and the console_unlock() from them will do the
1366 * flush, no worries.
1367 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001368 raw_spin_lock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001369 if (con_start != log_end)
1370 retry = 1;
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08001371 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1372
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001373 if (retry && console_trylock())
1374 goto again;
1375
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001376 if (wake_klogd)
1377 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
Torben Hohnac751ef2011-01-25 15:07:35 -08001379EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Martin Waitzddad86c2005-11-13 16:08:14 -08001381/**
1382 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 *
1384 * If the console code is currently allowed to sleep, and
1385 * if this CPU should yield the CPU to another task, do
1386 * so here.
1387 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001388 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 */
1390void __sched console_conditional_schedule(void)
1391{
1392 if (console_may_schedule)
1393 cond_resched();
1394}
1395EXPORT_SYMBOL(console_conditional_schedule);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397void console_unblank(void)
1398{
1399 struct console *c;
1400
1401 /*
1402 * console_unblank can no longer be called in interrupt context unless
1403 * oops_in_progress is set to 1..
1404 */
1405 if (oops_in_progress) {
1406 if (down_trylock(&console_sem) != 0)
1407 return;
1408 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08001409 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 console_locked = 1;
1412 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04001413 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if ((c->flags & CON_ENABLED) && c->unblank)
1415 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08001416 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419/*
1420 * Return the console tty driver structure and its associated index
1421 */
1422struct tty_driver *console_device(int *index)
1423{
1424 struct console *c;
1425 struct tty_driver *driver = NULL;
1426
Torben Hohnac751ef2011-01-25 15:07:35 -08001427 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04001428 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (!c->device)
1430 continue;
1431 driver = c->device(c, index);
1432 if (driver)
1433 break;
1434 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001435 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return driver;
1437}
1438
1439/*
1440 * Prevent further output on the passed console device so that (for example)
1441 * serial drivers can disable console output before suspending a port, and can
1442 * re-enable output afterwards.
1443 */
1444void console_stop(struct console *console)
1445{
Torben Hohnac751ef2011-01-25 15:07:35 -08001446 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08001448 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450EXPORT_SYMBOL(console_stop);
1451
1452void console_start(struct console *console)
1453{
Torben Hohnac751ef2011-01-25 15:07:35 -08001454 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08001456 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458EXPORT_SYMBOL(console_start);
1459
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07001460static int __read_mostly keep_bootcon;
1461
1462static int __init keep_bootcon_setup(char *str)
1463{
1464 keep_bootcon = 1;
1465 printk(KERN_INFO "debug: skip boot console de-registration.\n");
1466
1467 return 0;
1468}
1469
1470early_param("keep_bootcon", keep_bootcon_setup);
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/*
1473 * The console driver calls this routine during kernel initialization
1474 * to register the console printing procedure with printk() and to
1475 * print any messages that were printed by the kernel before the
1476 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04001477 *
1478 * This can happen pretty early during the boot process (because of
1479 * early_printk) - sometimes before setup_arch() completes - be careful
1480 * of what kernel features are used - they may not be initialised yet.
1481 *
1482 * There are two types of consoles - bootconsoles (early_printk) and
1483 * "real" consoles (everything which is not a bootconsole) which are
1484 * handled differently.
1485 * - Any number of bootconsoles can be registered at any time.
1486 * - As soon as a "real" console is registered, all bootconsoles
1487 * will be unregistered automatically.
1488 * - Once a "real" console is registered, any attempt to register a
1489 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
Robin Getz4d091612009-07-01 21:08:37 -04001491void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001493 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04001495 struct console *bcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Robin Getz4d091612009-07-01 21:08:37 -04001497 /*
1498 * before we register a new CON_BOOT console, make sure we don't
1499 * already have a valid console
1500 */
1501 if (console_drivers && newcon->flags & CON_BOOT) {
1502 /* find the last or real console */
1503 for_each_console(bcon) {
1504 if (!(bcon->flags & CON_BOOT)) {
1505 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1506 newcon->name, newcon->index);
1507 return;
1508 }
1509 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001510 }
1511
Robin Getz4d091612009-07-01 21:08:37 -04001512 if (console_drivers && console_drivers->flags & CON_BOOT)
1513 bcon = console_drivers;
1514
1515 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 preferred_console = selected_console;
1517
Robin Getz4d091612009-07-01 21:08:37 -04001518 if (newcon->early_setup)
1519 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /*
1522 * See if we want to use this console driver. If we
1523 * didn't select a console we take the first one
1524 * that registers here.
1525 */
1526 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04001527 if (newcon->index < 0)
1528 newcon->index = 0;
1529 if (newcon->setup == NULL ||
1530 newcon->setup(newcon, NULL) == 0) {
1531 newcon->flags |= CON_ENABLED;
1532 if (newcon->device) {
1533 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02001534 preferred_console = 0;
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537 }
1538
1539 /*
1540 * See if this console matches one we selected on
1541 * the command line.
1542 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001543 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1544 i++) {
Robin Getz4d091612009-07-01 21:08:37 -04001545 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001547 if (newcon->index >= 0 &&
1548 newcon->index != console_cmdline[i].index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001550 if (newcon->index < 0)
1551 newcon->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001552#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1553 if (console_cmdline[i].brl_options) {
Robin Getz4d091612009-07-01 21:08:37 -04001554 newcon->flags |= CON_BRL;
1555 braille_register_console(newcon,
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001556 console_cmdline[i].index,
1557 console_cmdline[i].options,
1558 console_cmdline[i].brl_options);
1559 return;
1560 }
1561#endif
Robin Getz4d091612009-07-01 21:08:37 -04001562 if (newcon->setup &&
1563 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
Robin Getz4d091612009-07-01 21:08:37 -04001565 newcon->flags |= CON_ENABLED;
1566 newcon->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001567 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04001568 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001569 preferred_console = selected_console;
1570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
1572 }
1573
Robin Getz4d091612009-07-01 21:08:37 -04001574 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return;
1576
Robin Getz8259cf42009-07-09 13:08:37 -04001577 /*
1578 * If we have a bootconsole, and are switching to a real console,
1579 * don't print everything out again, since when the boot console, and
1580 * the real console are the same physical device, it's annoying to
1581 * see the beginning boot messages twice
1582 */
1583 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04001584 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /*
1587 * Put this console in the list - keep the
1588 * preferred driver at the head of the list.
1589 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001590 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04001591 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1592 newcon->next = console_drivers;
1593 console_drivers = newcon;
1594 if (newcon->next)
1595 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 } else {
Robin Getz4d091612009-07-01 21:08:37 -04001597 newcon->next = console_drivers->next;
1598 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Robin Getz4d091612009-07-01 21:08:37 -04001600 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08001602 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * for us.
1604 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001605 raw_spin_lock_irqsave(&logbuf_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 con_start = log_start;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001607 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07001608 /*
1609 * We're about to replay the log buffer. Only do this to the
1610 * just-registered console to avoid excessive message spam to
1611 * the already-registered consoles.
1612 */
1613 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001615 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01001616 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04001617
1618 /*
1619 * By unregistering the bootconsoles after we enable the real console
1620 * we get the "console xxx enabled" message on all the consoles -
1621 * boot consoles, real consoles, etc - this is to ensure that end
1622 * users know there might be something in the kernel's log buffer that
1623 * went to the bootconsole (that they do not see on the real console)
1624 */
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07001625 if (bcon &&
1626 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
1627 !keep_bootcon) {
Robin Getz8259cf42009-07-09 13:08:37 -04001628 /* we need to iterate through twice, to make sure we print
1629 * everything out, before we unregister the console(s)
1630 */
1631 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1632 newcon->name, newcon->index);
1633 for_each_console(bcon)
1634 if (bcon->flags & CON_BOOT)
1635 unregister_console(bcon);
1636 } else {
1637 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1638 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1639 newcon->name, newcon->index);
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642EXPORT_SYMBOL(register_console);
1643
Jesper Juhl40dc5652005-10-30 15:02:46 -08001644int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001646 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int res = 1;
1648
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001649#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1650 if (console->flags & CON_BRL)
1651 return braille_unregister_console(console);
1652#endif
1653
Torben Hohnac751ef2011-01-25 15:07:35 -08001654 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (console_drivers == console) {
1656 console_drivers=console->next;
1657 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001658 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 for (a=console_drivers->next, b=console_drivers ;
1660 a; b=a, a=b->next) {
1661 if (a == console) {
1662 b->next = a->next;
1663 res = 0;
1664 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001668
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001669 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001670 * If this isn't the last console and it has CON_CONSDEV set, we
1671 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001673 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001674 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Torben Hohnac751ef2011-01-25 15:07:35 -08001676 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01001677 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return res;
1679}
1680EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001681
Kevin Cernekee034260d2010-06-03 22:11:25 -07001682static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04001683{
Robin Getz4d091612009-07-01 21:08:37 -04001684 struct console *con;
1685
1686 for_each_console(con) {
Nishanth Aravamudan4c30c6f2011-08-25 15:59:11 -07001687 if (!keep_bootcon && con->flags & CON_BOOT) {
Robin Getzcb00e992007-08-21 23:14:58 -04001688 printk(KERN_INFO "turn off boot console %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04001689 con->name, con->index);
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07001690 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04001691 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001692 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07001693 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04001694 return 0;
1695}
Kevin Cernekee034260d2010-06-03 22:11:25 -07001696late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04001697
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001698#if defined CONFIG_PRINTK
Dave Young717115e2008-07-25 01:45:58 -07001699
Peter Zijlstra600e1452012-03-15 12:35:37 +01001700int printk_sched(const char *fmt, ...)
1701{
1702 unsigned long flags;
1703 va_list args;
1704 char *buf;
1705 int r;
1706
1707 local_irq_save(flags);
1708 buf = __get_cpu_var(printk_sched_buf);
1709
1710 va_start(args, fmt);
1711 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
1712 va_end(args);
1713
1714 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
1715 local_irq_restore(flags);
1716
1717 return r;
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720/*
1721 * printk rate limiting, lifted from the networking subsystem.
1722 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001723 * This enforces a rate limit: not more than 10 kernel messages
1724 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001726DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1727
Christian Borntraeger5c828712009-10-23 14:58:11 +02001728int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Christian Borntraeger5c828712009-10-23 14:58:11 +02001730 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
Christian Borntraeger5c828712009-10-23 14:58:11 +02001732EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001733
1734/**
1735 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1736 * @caller_jiffies: pointer to caller's state
1737 * @interval_msecs: minimum interval between prints
1738 *
1739 * printk_timed_ratelimit() returns true if more than @interval_msecs
1740 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1741 * returned true.
1742 */
1743bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1744 unsigned int interval_msecs)
1745{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01001746 if (*caller_jiffies == 0
1747 || !time_in_range(jiffies, *caller_jiffies,
1748 *caller_jiffies
1749 + msecs_to_jiffies(interval_msecs))) {
1750 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08001751 return true;
1752 }
1753 return false;
1754}
1755EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001756
1757static DEFINE_SPINLOCK(dump_list_lock);
1758static LIST_HEAD(dump_list);
1759
1760/**
1761 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08001762 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02001763 *
1764 * Adds a kernel log dumper to the system. The dump callback in the
1765 * structure will be called when the kernel oopses or panics and must be
1766 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1767 */
1768int kmsg_dump_register(struct kmsg_dumper *dumper)
1769{
1770 unsigned long flags;
1771 int err = -EBUSY;
1772
1773 /* The dump callback needs to be set */
1774 if (!dumper->dump)
1775 return -EINVAL;
1776
1777 spin_lock_irqsave(&dump_list_lock, flags);
1778 /* Don't allow registering multiple times */
1779 if (!dumper->registered) {
1780 dumper->registered = 1;
Huang Yingfb842b02011-01-12 16:59:43 -08001781 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001782 err = 0;
1783 }
1784 spin_unlock_irqrestore(&dump_list_lock, flags);
1785
1786 return err;
1787}
1788EXPORT_SYMBOL_GPL(kmsg_dump_register);
1789
1790/**
1791 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08001792 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02001793 *
1794 * Removes a dump device from the system. Returns zero on success and
1795 * %-EINVAL otherwise.
1796 */
1797int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1798{
1799 unsigned long flags;
1800 int err = -EINVAL;
1801
1802 spin_lock_irqsave(&dump_list_lock, flags);
1803 if (dumper->registered) {
1804 dumper->registered = 0;
Huang Yingfb842b02011-01-12 16:59:43 -08001805 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001806 err = 0;
1807 }
1808 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b02011-01-12 16:59:43 -08001809 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02001810
1811 return err;
1812}
1813EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1814
Simon Kagstrom456b5652009-10-16 14:09:18 +02001815/**
1816 * kmsg_dump - dump kernel log to kernel message dumpers.
1817 * @reason: the reason (oops, panic etc) for dumping
1818 *
1819 * Iterate through each of the dump devices and call the oops/panic
1820 * callbacks with the log buffer.
1821 */
1822void kmsg_dump(enum kmsg_dump_reason reason)
1823{
1824 unsigned long end;
1825 unsigned chars;
1826 struct kmsg_dumper *dumper;
1827 const char *s1, *s2;
1828 unsigned long l1, l2;
1829 unsigned long flags;
1830
Matthew Garrettc22ab332012-03-05 14:59:10 -08001831 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
1832 return;
1833
Simon Kagstrom456b5652009-10-16 14:09:18 +02001834 /* Theoretically, the log could move on after we do this, but
1835 there's not a lot we can do about that. The new messages
1836 will overwrite the start of what we dump. */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001837 raw_spin_lock_irqsave(&logbuf_lock, flags);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001838 end = log_end & LOG_BUF_MASK;
1839 chars = logged_chars;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001840 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001841
Andi Kleen8c4af382010-08-09 17:20:36 -07001842 if (chars > end) {
1843 s1 = log_buf + log_buf_len - chars + end;
1844 l1 = chars - end;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001845
1846 s2 = log_buf;
1847 l2 = end;
1848 } else {
1849 s1 = "";
1850 l1 = 0;
1851
Andi Kleen8c4af382010-08-09 17:20:36 -07001852 s2 = log_buf + end - chars;
1853 l2 = chars;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001854 }
1855
Huang Yingfb842b02011-01-12 16:59:43 -08001856 rcu_read_lock();
1857 list_for_each_entry_rcu(dumper, &dump_list, list)
Simon Kagstrom456b5652009-10-16 14:09:18 +02001858 dumper->dump(dumper, reason, s1, l1, s2, l2);
Huang Yingfb842b02011-01-12 16:59:43 -08001859 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02001860}
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001861#endif