printk.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/console.h>
  23. #include <linux/init.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/nmi.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/interrupt.h> /* For in_interrupt() */
  29. #include <linux/delay.h>
  30. #include <linux/smp.h>
  31. #include <linux/security.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/kexec.h>
  35. #include <linux/kdb.h>
  36. #include <linux/ratelimit.h>
  37. #include <linux/kmsg_dump.h>
  38. #include <linux/syslog.h>
  39. #include <linux/cpu.h>
  40. #include <linux/notifier.h>
  41. #include <asm/uaccess.h>
  42. /*
  43. * for_each_console() allows you to iterate on each console
  44. */
  45. #define for_each_console(con) \
  46. for (con = console_drivers; con != NULL; con = con->next)
  47. /*
  48. * Architectures can override it:
  49. */
  50. void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
  51. {
  52. }
  53. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  54. /* printk's without a loglevel use this.. */
  55. #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
  56. /* We show everything that is MORE important than this.. */
  57. #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
  58. #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
  59. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  60. int console_printk[4] = {
  61. DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
  62. DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
  63. MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
  64. DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
  65. };
  66. /*
  67. * Low level drivers may need that to know if they can schedule in
  68. * their unblank() callback or not. So let's export it.
  69. */
  70. int oops_in_progress;
  71. EXPORT_SYMBOL(oops_in_progress);
  72. /*
  73. * console_sem protects the console_drivers list, and also
  74. * provides serialisation for access to the entire console
  75. * driver system.
  76. */
  77. static DEFINE_SEMAPHORE(console_sem);
  78. struct console *console_drivers;
  79. EXPORT_SYMBOL_GPL(console_drivers);
  80. /*
  81. * This is used for debugging the mess that is the VT code by
  82. * keeping track if we have the console semaphore held. It's
  83. * definitely not the perfect debug tool (we don't know if _WE_
  84. * hold it are racing, but it helps tracking those weird code
  85. * path in the console code where we end up in places I want
  86. * locked without the console sempahore held
  87. */
  88. static int console_locked, console_suspended;
  89. /*
  90. * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
  91. * It is also used in interesting ways to provide interlocking in
  92. * release_console_sem().
  93. */
  94. static DEFINE_SPINLOCK(logbuf_lock);
  95. #define LOG_BUF_MASK (log_buf_len-1)
  96. #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
  97. /*
  98. * The indices into log_buf are not constrained to log_buf_len - they
  99. * must be masked before subscripting
  100. */
  101. static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
  102. static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
  103. static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
  104. /*
  105. * Array of consoles built from command line options (console=)
  106. */
  107. struct console_cmdline
  108. {
  109. char name[8]; /* Name of the driver */
  110. int index; /* Minor dev. to use */
  111. char *options; /* Options for the driver */
  112. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  113. char *brl_options; /* Options for braille driver */
  114. #endif
  115. };
  116. #define MAX_CMDLINECONSOLES 8
  117. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  118. static int selected_console = -1;
  119. static int preferred_console = -1;
  120. int console_set_on_cmdline;
  121. EXPORT_SYMBOL(console_set_on_cmdline);
  122. /* Flag: console code may call schedule() */
  123. static int console_may_schedule;
  124. #ifdef CONFIG_PRINTK
  125. static char __log_buf[__LOG_BUF_LEN];
  126. static char *log_buf = __log_buf;
  127. static int log_buf_len = __LOG_BUF_LEN;
  128. static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
  129. static int saved_console_loglevel = -1;
  130. #ifdef CONFIG_KEXEC
  131. /*
  132. * This appends the listed symbols to /proc/vmcoreinfo
  133. *
  134. * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
  135. * obtain access to symbols that are otherwise very difficult to locate. These
  136. * symbols are specifically used so that utilities can access and extract the
  137. * dmesg log from a vmcore file after a crash.
  138. */
  139. void log_buf_kexec_setup(void)
  140. {
  141. VMCOREINFO_SYMBOL(log_buf);
  142. VMCOREINFO_SYMBOL(log_end);
  143. VMCOREINFO_SYMBOL(log_buf_len);
  144. VMCOREINFO_SYMBOL(logged_chars);
  145. }
  146. #endif
  147. static int __init log_buf_len_setup(char *str)
  148. {
  149. unsigned size = memparse(str, &str);
  150. unsigned long flags;
  151. if (size)
  152. size = roundup_pow_of_two(size);
  153. if (size > log_buf_len) {
  154. unsigned start, dest_idx, offset;
  155. char *new_log_buf;
  156. new_log_buf = alloc_bootmem(size);
  157. if (!new_log_buf) {
  158. printk(KERN_WARNING "log_buf_len: allocation failed\n");
  159. goto out;
  160. }
  161. spin_lock_irqsave(&logbuf_lock, flags);
  162. log_buf_len = size;
  163. log_buf = new_log_buf;
  164. offset = start = min(con_start, log_start);
  165. dest_idx = 0;
  166. while (start != log_end) {
  167. log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
  168. start++;
  169. dest_idx++;
  170. }
  171. log_start -= offset;
  172. con_start -= offset;
  173. log_end -= offset;
  174. spin_unlock_irqrestore(&logbuf_lock, flags);
  175. printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
  176. }
  177. out:
  178. return 1;
  179. }
  180. __setup("log_buf_len=", log_buf_len_setup);
  181. #ifdef CONFIG_BOOT_PRINTK_DELAY
  182. static int boot_delay; /* msecs delay after each printk during bootup */
  183. static unsigned long long loops_per_msec; /* based on boot_delay */
  184. static int __init boot_delay_setup(char *str)
  185. {
  186. unsigned long lpj;
  187. lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
  188. loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
  189. get_option(&str, &boot_delay);
  190. if (boot_delay > 10 * 1000)
  191. boot_delay = 0;
  192. pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
  193. "HZ: %d, loops_per_msec: %llu\n",
  194. boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
  195. return 1;
  196. }
  197. __setup("boot_delay=", boot_delay_setup);
  198. static void boot_delay_msec(void)
  199. {
  200. unsigned long long k;
  201. unsigned long timeout;
  202. if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
  203. return;
  204. k = (unsigned long long)loops_per_msec * boot_delay;
  205. timeout = jiffies + msecs_to_jiffies(boot_delay);
  206. while (k) {
  207. k--;
  208. cpu_relax();
  209. /*
  210. * use (volatile) jiffies to prevent
  211. * compiler reduction; loop termination via jiffies
  212. * is secondary and may or may not happen.
  213. */
  214. if (time_after(jiffies, timeout))
  215. break;
  216. touch_nmi_watchdog();
  217. }
  218. }
  219. #else
  220. static inline void boot_delay_msec(void)
  221. {
  222. }
  223. #endif
  224. int do_syslog(int type, char __user *buf, int len, bool from_file)
  225. {
  226. unsigned i, j, limit, count;
  227. int do_clear = 0;
  228. char c;
  229. int error = 0;
  230. error = security_syslog(type, from_file);
  231. if (error)
  232. return error;
  233. switch (type) {
  234. case SYSLOG_ACTION_CLOSE: /* Close log */
  235. break;
  236. case SYSLOG_ACTION_OPEN: /* Open log */
  237. break;
  238. case SYSLOG_ACTION_READ: /* Read from log */
  239. error = -EINVAL;
  240. if (!buf || len < 0)
  241. goto out;
  242. error = 0;
  243. if (!len)
  244. goto out;
  245. if (!access_ok(VERIFY_WRITE, buf, len)) {
  246. error = -EFAULT;
  247. goto out;
  248. }
  249. error = wait_event_interruptible(log_wait,
  250. (log_start - log_end));
  251. if (error)
  252. goto out;
  253. i = 0;
  254. spin_lock_irq(&logbuf_lock);
  255. while (!error && (log_start != log_end) && i < len) {
  256. c = LOG_BUF(log_start);
  257. log_start++;
  258. spin_unlock_irq(&logbuf_lock);
  259. error = __put_user(c,buf);
  260. buf++;
  261. i++;
  262. cond_resched();
  263. spin_lock_irq(&logbuf_lock);
  264. }
  265. spin_unlock_irq(&logbuf_lock);
  266. if (!error)
  267. error = i;
  268. break;
  269. /* Read/clear last kernel messages */
  270. case SYSLOG_ACTION_READ_CLEAR:
  271. do_clear = 1;
  272. /* FALL THRU */
  273. /* Read last kernel messages */
  274. case SYSLOG_ACTION_READ_ALL:
  275. error = -EINVAL;
  276. if (!buf || len < 0)
  277. goto out;
  278. error = 0;
  279. if (!len)
  280. goto out;
  281. if (!access_ok(VERIFY_WRITE, buf, len)) {
  282. error = -EFAULT;
  283. goto out;
  284. }
  285. count = len;
  286. if (count > log_buf_len)
  287. count = log_buf_len;
  288. spin_lock_irq(&logbuf_lock);
  289. if (count > logged_chars)
  290. count = logged_chars;
  291. if (do_clear)
  292. logged_chars = 0;
  293. limit = log_end;
  294. /*
  295. * __put_user() could sleep, and while we sleep
  296. * printk() could overwrite the messages
  297. * we try to copy to user space. Therefore
  298. * the messages are copied in reverse. <manfreds>
  299. */
  300. for (i = 0; i < count && !error; i++) {
  301. j = limit-1-i;
  302. if (j + log_buf_len < log_end)
  303. break;
  304. c = LOG_BUF(j);
  305. spin_unlock_irq(&logbuf_lock);
  306. error = __put_user(c,&buf[count-1-i]);
  307. cond_resched();
  308. spin_lock_irq(&logbuf_lock);
  309. }
  310. spin_unlock_irq(&logbuf_lock);
  311. if (error)
  312. break;
  313. error = i;
  314. if (i != count) {
  315. int offset = count-error;
  316. /* buffer overflow during copy, correct user buffer. */
  317. for (i = 0; i < error; i++) {
  318. if (__get_user(c,&buf[i+offset]) ||
  319. __put_user(c,&buf[i])) {
  320. error = -EFAULT;
  321. break;
  322. }
  323. cond_resched();
  324. }
  325. }
  326. break;
  327. /* Clear ring buffer */
  328. case SYSLOG_ACTION_CLEAR:
  329. logged_chars = 0;
  330. break;
  331. /* Disable logging to console */
  332. case SYSLOG_ACTION_CONSOLE_OFF:
  333. if (saved_console_loglevel == -1)
  334. saved_console_loglevel = console_loglevel;
  335. console_loglevel = minimum_console_loglevel;
  336. break;
  337. /* Enable logging to console */
  338. case SYSLOG_ACTION_CONSOLE_ON:
  339. if (saved_console_loglevel != -1) {
  340. console_loglevel = saved_console_loglevel;
  341. saved_console_loglevel = -1;
  342. }
  343. break;
  344. /* Set level of messages printed to console */
  345. case SYSLOG_ACTION_CONSOLE_LEVEL:
  346. error = -EINVAL;
  347. if (len < 1 || len > 8)
  348. goto out;
  349. if (len < minimum_console_loglevel)
  350. len = minimum_console_loglevel;
  351. console_loglevel = len;
  352. /* Implicitly re-enable logging to console */
  353. saved_console_loglevel = -1;
  354. error = 0;
  355. break;
  356. /* Number of chars in the log buffer */
  357. case SYSLOG_ACTION_SIZE_UNREAD:
  358. error = log_end - log_start;
  359. break;
  360. /* Size of the log buffer */
  361. case SYSLOG_ACTION_SIZE_BUFFER:
  362. error = log_buf_len;
  363. break;
  364. default:
  365. error = -EINVAL;
  366. break;
  367. }
  368. out:
  369. return error;
  370. }
  371. SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
  372. {
  373. return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
  374. }
  375. #ifdef CONFIG_KGDB_KDB
  376. /* kdb dmesg command needs access to the syslog buffer. do_syslog()
  377. * uses locks so it cannot be used during debugging. Just tell kdb
  378. * where the start and end of the physical and logical logs are. This
  379. * is equivalent to do_syslog(3).
  380. */
  381. void kdb_syslog_data(char *syslog_data[4])
  382. {
  383. syslog_data[0] = log_buf;
  384. syslog_data[1] = log_buf + log_buf_len;
  385. syslog_data[2] = log_buf + log_end -
  386. (logged_chars < log_buf_len ? logged_chars : log_buf_len);
  387. syslog_data[3] = log_buf + log_end;
  388. }
  389. #endif /* CONFIG_KGDB_KDB */
  390. /*
  391. * Call the console drivers on a range of log_buf
  392. */
  393. static void __call_console_drivers(unsigned start, unsigned end)
  394. {
  395. struct console *con;
  396. for_each_console(con) {
  397. if ((con->flags & CON_ENABLED) && con->write &&
  398. (cpu_online(smp_processor_id()) ||
  399. (con->flags & CON_ANYTIME)))
  400. con->write(con, &LOG_BUF(start), end - start);
  401. }
  402. }
  403. static int __read_mostly ignore_loglevel;
  404. static int __init ignore_loglevel_setup(char *str)
  405. {
  406. ignore_loglevel = 1;
  407. printk(KERN_INFO "debug: ignoring loglevel setting.\n");
  408. return 0;
  409. }
  410. early_param("ignore_loglevel", ignore_loglevel_setup);
  411. /*
  412. * Write out chars from start to end - 1 inclusive
  413. */
  414. static void _call_console_drivers(unsigned start,
  415. unsigned end, int msg_log_level)
  416. {
  417. if ((msg_log_level < console_loglevel || ignore_loglevel) &&
  418. console_drivers && start != end) {
  419. if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
  420. /* wrapped write */
  421. __call_console_drivers(start & LOG_BUF_MASK,
  422. log_buf_len);
  423. __call_console_drivers(0, end & LOG_BUF_MASK);
  424. } else {
  425. __call_console_drivers(start, end);
  426. }
  427. }
  428. }
  429. /*
  430. * Call the console drivers, asking them to write out
  431. * log_buf[start] to log_buf[end - 1].
  432. * The console_sem must be held.
  433. */
  434. static void call_console_drivers(unsigned start, unsigned end)
  435. {
  436. unsigned cur_index, start_print;
  437. static int msg_level = -1;
  438. BUG_ON(((int)(start - end)) > 0);
  439. cur_index = start;
  440. start_print = start;
  441. while (cur_index != end) {
  442. if (msg_level < 0 && ((end - cur_index) > 2) &&
  443. LOG_BUF(cur_index + 0) == '<' &&
  444. LOG_BUF(cur_index + 1) >= '0' &&
  445. LOG_BUF(cur_index + 1) <= '7' &&
  446. LOG_BUF(cur_index + 2) == '>') {
  447. msg_level = LOG_BUF(cur_index + 1) - '0';
  448. cur_index += 3;
  449. start_print = cur_index;
  450. }
  451. while (cur_index != end) {
  452. char c = LOG_BUF(cur_index);
  453. cur_index++;
  454. if (c == '\n') {
  455. if (msg_level < 0) {
  456. /*
  457. * printk() has already given us loglevel tags in
  458. * the buffer. This code is here in case the
  459. * log buffer has wrapped right round and scribbled
  460. * on those tags
  461. */
  462. msg_level = default_message_loglevel;
  463. }
  464. _call_console_drivers(start_print, cur_index, msg_level);
  465. msg_level = -1;
  466. start_print = cur_index;
  467. break;
  468. }
  469. }
  470. }
  471. _call_console_drivers(start_print, end, msg_level);
  472. }
  473. static void emit_log_char(char c)
  474. {
  475. LOG_BUF(log_end) = c;
  476. log_end++;
  477. if (log_end - log_start > log_buf_len)
  478. log_start = log_end - log_buf_len;
  479. if (log_end - con_start > log_buf_len)
  480. con_start = log_end - log_buf_len;
  481. if (logged_chars < log_buf_len)
  482. logged_chars++;
  483. }
  484. /*
  485. * Zap console related locks when oopsing. Only zap at most once
  486. * every 10 seconds, to leave time for slow consoles to print a
  487. * full oops.
  488. */
  489. static void zap_locks(void)
  490. {
  491. static unsigned long oops_timestamp;
  492. if (time_after_eq(jiffies, oops_timestamp) &&
  493. !time_after(jiffies, oops_timestamp + 30 * HZ))
  494. return;
  495. oops_timestamp = jiffies;
  496. /* If a crash is occurring, make sure we can't deadlock */
  497. spin_lock_init(&logbuf_lock);
  498. /* And make sure that we print immediately */
  499. sema_init(&console_sem, 1);
  500. }
  501. #if defined(CONFIG_PRINTK_TIME)
  502. static int printk_time = 1;
  503. #else
  504. static int printk_time = 0;
  505. #endif
  506. module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
  507. /* Check if we have any console registered that can be called early in boot. */
  508. static int have_callable_console(void)
  509. {
  510. struct console *con;
  511. for_each_console(con)
  512. if (con->flags & CON_ANYTIME)
  513. return 1;
  514. return 0;
  515. }
  516. /**
  517. * printk - print a kernel message
  518. * @fmt: format string
  519. *
  520. * This is printk(). It can be called from any context. We want it to work.
  521. *
  522. * We try to grab the console_sem. If we succeed, it's easy - we log the output and
  523. * call the console drivers. If we fail to get the semaphore we place the output
  524. * into the log buffer and return. The current holder of the console_sem will
  525. * notice the new output in release_console_sem() and will send it to the
  526. * consoles before releasing the semaphore.
  527. *
  528. * One effect of this deferred printing is that code which calls printk() and
  529. * then changes console_loglevel may break. This is because console_loglevel
  530. * is inspected when the actual printing occurs.
  531. *
  532. * See also:
  533. * printf(3)
  534. *
  535. * See the vsnprintf() documentation for format string extensions over C99.
  536. */
  537. asmlinkage int printk(const char *fmt, ...)
  538. {
  539. va_list args;
  540. int r;
  541. #ifdef CONFIG_KGDB_KDB
  542. if (unlikely(kdb_trap_printk)) {
  543. va_start(args, fmt);
  544. r = vkdb_printf(fmt, args);
  545. va_end(args);
  546. return r;
  547. }
  548. #endif
  549. va_start(args, fmt);
  550. r = vprintk(fmt, args);
  551. va_end(args);
  552. return r;
  553. }
  554. /* cpu currently holding logbuf_lock */
  555. static volatile unsigned int printk_cpu = UINT_MAX;
  556. /*
  557. * Can we actually use the console at this time on this cpu?
  558. *
  559. * Console drivers may assume that per-cpu resources have
  560. * been allocated. So unless they're explicitly marked as
  561. * being able to cope (CON_ANYTIME) don't call them until
  562. * this CPU is officially up.
  563. */
  564. static inline int can_use_console(unsigned int cpu)
  565. {
  566. return cpu_online(cpu) || have_callable_console();
  567. }
  568. /*
  569. * Try to get console ownership to actually show the kernel
  570. * messages from a 'printk'. Return true (and with the
  571. * console_semaphore held, and 'console_locked' set) if it
  572. * is successful, false otherwise.
  573. *
  574. * This gets called with the 'logbuf_lock' spinlock held and
  575. * interrupts disabled. It should return with 'lockbuf_lock'
  576. * released but interrupts still disabled.
  577. */
  578. static int acquire_console_semaphore_for_printk(unsigned int cpu)
  579. __releases(&logbuf_lock)
  580. {
  581. int retval = 0;
  582. if (!try_acquire_console_sem()) {
  583. retval = 1;
  584. /*
  585. * If we can't use the console, we need to release
  586. * the console semaphore by hand to avoid flushing
  587. * the buffer. We need to hold the console semaphore
  588. * in order to do this test safely.
  589. */
  590. if (!can_use_console(cpu)) {
  591. console_locked = 0;
  592. up(&console_sem);
  593. retval = 0;
  594. }
  595. }
  596. printk_cpu = UINT_MAX;
  597. spin_unlock(&logbuf_lock);
  598. return retval;
  599. }
  600. static const char recursion_bug_msg [] =
  601. KERN_CRIT "BUG: recent printk recursion!\n";
  602. static int recursion_bug;
  603. static int new_text_line = 1;
  604. static char printk_buf[1024];
  605. int printk_delay_msec __read_mostly;
  606. static inline void printk_delay(void)
  607. {
  608. if (unlikely(printk_delay_msec)) {
  609. int m = printk_delay_msec;
  610. while (m--) {
  611. mdelay(1);
  612. touch_nmi_watchdog();
  613. }
  614. }
  615. }
  616. asmlinkage int vprintk(const char *fmt, va_list args)
  617. {
  618. int printed_len = 0;
  619. int current_log_level = default_message_loglevel;
  620. unsigned long flags;
  621. int this_cpu;
  622. char *p;
  623. boot_delay_msec();
  624. printk_delay();
  625. preempt_disable();
  626. /* This stops the holder of console_sem just where we want him */
  627. raw_local_irq_save(flags);
  628. this_cpu = smp_processor_id();
  629. /*
  630. * Ouch, printk recursed into itself!
  631. */
  632. if (unlikely(printk_cpu == this_cpu)) {
  633. /*
  634. * If a crash is occurring during printk() on this CPU,
  635. * then try to get the crash message out but make sure
  636. * we can't deadlock. Otherwise just return to avoid the
  637. * recursion and return - but flag the recursion so that
  638. * it can be printed at the next appropriate moment:
  639. */
  640. if (!oops_in_progress) {
  641. recursion_bug = 1;
  642. goto out_restore_irqs;
  643. }
  644. zap_locks();
  645. }
  646. lockdep_off();
  647. spin_lock(&logbuf_lock);
  648. printk_cpu = this_cpu;
  649. if (recursion_bug) {
  650. recursion_bug = 0;
  651. strcpy(printk_buf, recursion_bug_msg);
  652. printed_len = strlen(recursion_bug_msg);
  653. }
  654. /* Emit the output into the temporary buffer */
  655. printed_len += vscnprintf(printk_buf + printed_len,
  656. sizeof(printk_buf) - printed_len, fmt, args);
  657. p = printk_buf;
  658. /* Do we have a loglevel in the string? */
  659. if (p[0] == '<') {
  660. unsigned char c = p[1];
  661. if (c && p[2] == '>') {
  662. switch (c) {
  663. case '0' ... '7': /* loglevel */
  664. current_log_level = c - '0';
  665. /* Fallthrough - make sure we're on a new line */
  666. case 'd': /* KERN_DEFAULT */
  667. if (!new_text_line) {
  668. emit_log_char('\n');
  669. new_text_line = 1;
  670. }
  671. /* Fallthrough - skip the loglevel */
  672. case 'c': /* KERN_CONT */
  673. p += 3;
  674. break;
  675. }
  676. }
  677. }
  678. /*
  679. * Copy the output into log_buf. If the caller didn't provide
  680. * appropriate log level tags, we insert them here
  681. */
  682. for ( ; *p; p++) {
  683. if (new_text_line) {
  684. /* Always output the token */
  685. emit_log_char('<');
  686. emit_log_char(current_log_level + '0');
  687. emit_log_char('>');
  688. printed_len += 3;
  689. new_text_line = 0;
  690. if (printk_time) {
  691. /* Follow the token with the time */
  692. char tbuf[50], *tp;
  693. unsigned tlen;
  694. unsigned long long t;
  695. unsigned long nanosec_rem;
  696. t = cpu_clock(printk_cpu);
  697. nanosec_rem = do_div(t, 1000000000);
  698. tlen = sprintf(tbuf, "[%5lu.%06lu] ",
  699. (unsigned long) t,
  700. nanosec_rem / 1000);
  701. for (tp = tbuf; tp < tbuf + tlen; tp++)
  702. emit_log_char(*tp);
  703. printed_len += tlen;
  704. }
  705. if (!*p)
  706. break;
  707. }
  708. emit_log_char(*p);
  709. if (*p == '\n')
  710. new_text_line = 1;
  711. }
  712. /*
  713. * Try to acquire and then immediately release the
  714. * console semaphore. The release will do all the
  715. * actual magic (print out buffers, wake up klogd,
  716. * etc).
  717. *
  718. * The acquire_console_semaphore_for_printk() function
  719. * will release 'logbuf_lock' regardless of whether it
  720. * actually gets the semaphore or not.
  721. */
  722. if (acquire_console_semaphore_for_printk(this_cpu))
  723. release_console_sem();
  724. lockdep_on();
  725. out_restore_irqs:
  726. raw_local_irq_restore(flags);
  727. preempt_enable();
  728. return printed_len;
  729. }
  730. EXPORT_SYMBOL(printk);
  731. EXPORT_SYMBOL(vprintk);
  732. #else
  733. static void call_console_drivers(unsigned start, unsigned end)
  734. {
  735. }
  736. #endif
  737. static int __add_preferred_console(char *name, int idx, char *options,
  738. char *brl_options)
  739. {
  740. struct console_cmdline *c;
  741. int i;
  742. /*
  743. * See if this tty is not yet registered, and
  744. * if we have a slot free.
  745. */
  746. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  747. if (strcmp(console_cmdline[i].name, name) == 0 &&
  748. console_cmdline[i].index == idx) {
  749. if (!brl_options)
  750. selected_console = i;
  751. return 0;
  752. }
  753. if (i == MAX_CMDLINECONSOLES)
  754. return -E2BIG;
  755. if (!brl_options)
  756. selected_console = i;
  757. c = &console_cmdline[i];
  758. strlcpy(c->name, name, sizeof(c->name));
  759. c->options = options;
  760. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  761. c->brl_options = brl_options;
  762. #endif
  763. c->index = idx;
  764. return 0;
  765. }
  766. /*
  767. * Set up a list of consoles. Called from init/main.c
  768. */
  769. static int __init console_setup(char *str)
  770. {
  771. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
  772. char *s, *options, *brl_options = NULL;
  773. int idx;
  774. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  775. if (!memcmp(str, "brl,", 4)) {
  776. brl_options = "";
  777. str += 4;
  778. } else if (!memcmp(str, "brl=", 4)) {
  779. brl_options = str + 4;
  780. str = strchr(brl_options, ',');
  781. if (!str) {
  782. printk(KERN_ERR "need port name after brl=\n");
  783. return 1;
  784. }
  785. *(str++) = 0;
  786. }
  787. #endif
  788. /*
  789. * Decode str into name, index, options.
  790. */
  791. if (str[0] >= '0' && str[0] <= '9') {
  792. strcpy(buf, "ttyS");
  793. strncpy(buf + 4, str, sizeof(buf) - 5);
  794. } else {
  795. strncpy(buf, str, sizeof(buf) - 1);
  796. }
  797. buf[sizeof(buf) - 1] = 0;
  798. if ((options = strchr(str, ',')) != NULL)
  799. *(options++) = 0;
  800. #ifdef __sparc__
  801. if (!strcmp(str, "ttya"))
  802. strcpy(buf, "ttyS0");
  803. if (!strcmp(str, "ttyb"))
  804. strcpy(buf, "ttyS1");
  805. #endif
  806. for (s = buf; *s; s++)
  807. if ((*s >= '0' && *s <= '9') || *s == ',')
  808. break;
  809. idx = simple_strtoul(s, NULL, 10);
  810. *s = 0;
  811. __add_preferred_console(buf, idx, options, brl_options);
  812. console_set_on_cmdline = 1;
  813. return 1;
  814. }
  815. __setup("console=", console_setup);
  816. /**
  817. * add_preferred_console - add a device to the list of preferred consoles.
  818. * @name: device name
  819. * @idx: device index
  820. * @options: options for this console
  821. *
  822. * The last preferred console added will be used for kernel messages
  823. * and stdin/out/err for init. Normally this is used by console_setup
  824. * above to handle user-supplied console arguments; however it can also
  825. * be used by arch-specific code either to override the user or more
  826. * commonly to provide a default console (ie from PROM variables) when
  827. * the user has not supplied one.
  828. */
  829. int add_preferred_console(char *name, int idx, char *options)
  830. {
  831. return __add_preferred_console(name, idx, options, NULL);
  832. }
  833. int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
  834. {
  835. struct console_cmdline *c;
  836. int i;
  837. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  838. if (strcmp(console_cmdline[i].name, name) == 0 &&
  839. console_cmdline[i].index == idx) {
  840. c = &console_cmdline[i];
  841. strlcpy(c->name, name_new, sizeof(c->name));
  842. c->name[sizeof(c->name) - 1] = 0;
  843. c->options = options;
  844. c->index = idx_new;
  845. return i;
  846. }
  847. /* not found */
  848. return -1;
  849. }
  850. int console_suspend_enabled = 1;
  851. EXPORT_SYMBOL(console_suspend_enabled);
  852. static int __init console_suspend_disable(char *str)
  853. {
  854. console_suspend_enabled = 0;
  855. return 1;
  856. }
  857. __setup("no_console_suspend", console_suspend_disable);
  858. /**
  859. * suspend_console - suspend the console subsystem
  860. *
  861. * This disables printk() while we go into suspend states
  862. */
  863. void suspend_console(void)
  864. {
  865. if (!console_suspend_enabled)
  866. return;
  867. printk("Suspending console(s) (use no_console_suspend to debug)\n");
  868. acquire_console_sem();
  869. console_suspended = 1;
  870. up(&console_sem);
  871. }
  872. void resume_console(void)
  873. {
  874. if (!console_suspend_enabled)
  875. return;
  876. down(&console_sem);
  877. console_suspended = 0;
  878. release_console_sem();
  879. }
  880. /**
  881. * console_cpu_notify - print deferred console messages after CPU hotplug
  882. * @self: notifier struct
  883. * @action: CPU hotplug event
  884. * @hcpu: unused
  885. *
  886. * If printk() is called from a CPU that is not online yet, the messages
  887. * will be spooled but will not show up on the console. This function is
  888. * called when a new CPU comes online (or fails to come up), and ensures
  889. * that any such output gets printed.
  890. */
  891. static int __cpuinit console_cpu_notify(struct notifier_block *self,
  892. unsigned long action, void *hcpu)
  893. {
  894. switch (action) {
  895. case CPU_ONLINE:
  896. case CPU_DEAD:
  897. case CPU_DYING:
  898. case CPU_DOWN_FAILED:
  899. case CPU_UP_CANCELED:
  900. acquire_console_sem();
  901. release_console_sem();
  902. }
  903. return NOTIFY_OK;
  904. }
  905. /**
  906. * acquire_console_sem - lock the console system for exclusive use.
  907. *
  908. * Acquires a semaphore which guarantees that the caller has
  909. * exclusive access to the console system and the console_drivers list.
  910. *
  911. * Can sleep, returns nothing.
  912. */
  913. void acquire_console_sem(void)
  914. {
  915. BUG_ON(in_interrupt());
  916. down(&console_sem);
  917. if (console_suspended)
  918. return;
  919. console_locked = 1;
  920. console_may_schedule = 1;
  921. }
  922. EXPORT_SYMBOL(acquire_console_sem);
  923. int try_acquire_console_sem(void)
  924. {
  925. if (down_trylock(&console_sem))
  926. return -1;
  927. if (console_suspended) {
  928. up(&console_sem);
  929. return -1;
  930. }
  931. console_locked = 1;
  932. console_may_schedule = 0;
  933. return 0;
  934. }
  935. EXPORT_SYMBOL(try_acquire_console_sem);
  936. int is_console_locked(void)
  937. {
  938. return console_locked;
  939. }
  940. static DEFINE_PER_CPU(int, printk_pending);
  941. void printk_tick(void)
  942. {
  943. if (__get_cpu_var(printk_pending)) {
  944. __get_cpu_var(printk_pending) = 0;
  945. wake_up_interruptible(&log_wait);
  946. }
  947. }
  948. int printk_needs_cpu(int cpu)
  949. {
  950. return per_cpu(printk_pending, cpu);
  951. }
  952. void wake_up_klogd(void)
  953. {
  954. if (waitqueue_active(&log_wait))
  955. __raw_get_cpu_var(printk_pending) = 1;
  956. }
  957. /**
  958. * release_console_sem - unlock the console system
  959. *
  960. * Releases the semaphore which the caller holds on the console system
  961. * and the console driver list.
  962. *
  963. * While the semaphore was held, console output may have been buffered
  964. * by printk(). If this is the case, release_console_sem() emits
  965. * the output prior to releasing the semaphore.
  966. *
  967. * If there is output waiting for klogd, we wake it up.
  968. *
  969. * release_console_sem() may be called from any context.
  970. */
  971. void release_console_sem(void)
  972. {
  973. unsigned long flags;
  974. unsigned _con_start, _log_end;
  975. unsigned wake_klogd = 0;
  976. if (console_suspended) {
  977. up(&console_sem);
  978. return;
  979. }
  980. console_may_schedule = 0;
  981. for ( ; ; ) {
  982. spin_lock_irqsave(&logbuf_lock, flags);
  983. wake_klogd |= log_start - log_end;
  984. if (con_start == log_end)
  985. break; /* Nothing to print */
  986. _con_start = con_start;
  987. _log_end = log_end;
  988. con_start = log_end; /* Flush */
  989. spin_unlock(&logbuf_lock);
  990. stop_critical_timings(); /* don't trace print latency */
  991. call_console_drivers(_con_start, _log_end);
  992. start_critical_timings();
  993. local_irq_restore(flags);
  994. }
  995. console_locked = 0;
  996. up(&console_sem);
  997. spin_unlock_irqrestore(&logbuf_lock, flags);
  998. if (wake_klogd)
  999. wake_up_klogd();
  1000. }
  1001. EXPORT_SYMBOL(release_console_sem);
  1002. /**
  1003. * console_conditional_schedule - yield the CPU if required
  1004. *
  1005. * If the console code is currently allowed to sleep, and
  1006. * if this CPU should yield the CPU to another task, do
  1007. * so here.
  1008. *
  1009. * Must be called within acquire_console_sem().
  1010. */
  1011. void __sched console_conditional_schedule(void)
  1012. {
  1013. if (console_may_schedule)
  1014. cond_resched();
  1015. }
  1016. EXPORT_SYMBOL(console_conditional_schedule);
  1017. void console_unblank(void)
  1018. {
  1019. struct console *c;
  1020. /*
  1021. * console_unblank can no longer be called in interrupt context unless
  1022. * oops_in_progress is set to 1..
  1023. */
  1024. if (oops_in_progress) {
  1025. if (down_trylock(&console_sem) != 0)
  1026. return;
  1027. } else
  1028. acquire_console_sem();
  1029. console_locked = 1;
  1030. console_may_schedule = 0;
  1031. for_each_console(c)
  1032. if ((c->flags & CON_ENABLED) && c->unblank)
  1033. c->unblank();
  1034. release_console_sem();
  1035. }
  1036. /*
  1037. * Return the console tty driver structure and its associated index
  1038. */
  1039. struct tty_driver *console_device(int *index)
  1040. {
  1041. struct console *c;
  1042. struct tty_driver *driver = NULL;
  1043. acquire_console_sem();
  1044. for_each_console(c) {
  1045. if (!c->device)
  1046. continue;
  1047. driver = c->device(c, index);
  1048. if (driver)
  1049. break;
  1050. }
  1051. release_console_sem();
  1052. return driver;
  1053. }
  1054. /*
  1055. * Prevent further output on the passed console device so that (for example)
  1056. * serial drivers can disable console output before suspending a port, and can
  1057. * re-enable output afterwards.
  1058. */
  1059. void console_stop(struct console *console)
  1060. {
  1061. acquire_console_sem();
  1062. console->flags &= ~CON_ENABLED;
  1063. release_console_sem();
  1064. }
  1065. EXPORT_SYMBOL(console_stop);
  1066. void console_start(struct console *console)
  1067. {
  1068. acquire_console_sem();
  1069. console->flags |= CON_ENABLED;
  1070. release_console_sem();
  1071. }
  1072. EXPORT_SYMBOL(console_start);
  1073. /*
  1074. * The console driver calls this routine during kernel initialization
  1075. * to register the console printing procedure with printk() and to
  1076. * print any messages that were printed by the kernel before the
  1077. * console driver was initialized.
  1078. *
  1079. * This can happen pretty early during the boot process (because of
  1080. * early_printk) - sometimes before setup_arch() completes - be careful
  1081. * of what kernel features are used - they may not be initialised yet.
  1082. *
  1083. * There are two types of consoles - bootconsoles (early_printk) and
  1084. * "real" consoles (everything which is not a bootconsole) which are
  1085. * handled differently.
  1086. * - Any number of bootconsoles can be registered at any time.
  1087. * - As soon as a "real" console is registered, all bootconsoles
  1088. * will be unregistered automatically.
  1089. * - Once a "real" console is registered, any attempt to register a
  1090. * bootconsoles will be rejected
  1091. */
  1092. void register_console(struct console *newcon)
  1093. {
  1094. int i;
  1095. unsigned long flags;
  1096. struct console *bcon = NULL;
  1097. /*
  1098. * before we register a new CON_BOOT console, make sure we don't
  1099. * already have a valid console
  1100. */
  1101. if (console_drivers && newcon->flags & CON_BOOT) {
  1102. /* find the last or real console */
  1103. for_each_console(bcon) {
  1104. if (!(bcon->flags & CON_BOOT)) {
  1105. printk(KERN_INFO "Too late to register bootconsole %s%d\n",
  1106. newcon->name, newcon->index);
  1107. return;
  1108. }
  1109. }
  1110. }
  1111. if (console_drivers && console_drivers->flags & CON_BOOT)
  1112. bcon = console_drivers;
  1113. if (preferred_console < 0 || bcon || !console_drivers)
  1114. preferred_console = selected_console;
  1115. if (newcon->early_setup)
  1116. newcon->early_setup();
  1117. /*
  1118. * See if we want to use this console driver. If we
  1119. * didn't select a console we take the first one
  1120. * that registers here.
  1121. */
  1122. if (preferred_console < 0) {
  1123. if (newcon->index < 0)
  1124. newcon->index = 0;
  1125. if (newcon->setup == NULL ||
  1126. newcon->setup(newcon, NULL) == 0) {
  1127. newcon->flags |= CON_ENABLED;
  1128. if (newcon->device) {
  1129. newcon->flags |= CON_CONSDEV;
  1130. preferred_console = 0;
  1131. }
  1132. }
  1133. }
  1134. /*
  1135. * See if this console matches one we selected on
  1136. * the command line.
  1137. */
  1138. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  1139. i++) {
  1140. if (strcmp(console_cmdline[i].name, newcon->name) != 0)
  1141. continue;
  1142. if (newcon->index >= 0 &&
  1143. newcon->index != console_cmdline[i].index)
  1144. continue;
  1145. if (newcon->index < 0)
  1146. newcon->index = console_cmdline[i].index;
  1147. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1148. if (console_cmdline[i].brl_options) {
  1149. newcon->flags |= CON_BRL;
  1150. braille_register_console(newcon,
  1151. console_cmdline[i].index,
  1152. console_cmdline[i].options,
  1153. console_cmdline[i].brl_options);
  1154. return;
  1155. }
  1156. #endif
  1157. if (newcon->setup &&
  1158. newcon->setup(newcon, console_cmdline[i].options) != 0)
  1159. break;
  1160. newcon->flags |= CON_ENABLED;
  1161. newcon->index = console_cmdline[i].index;
  1162. if (i == selected_console) {
  1163. newcon->flags |= CON_CONSDEV;
  1164. preferred_console = selected_console;
  1165. }
  1166. break;
  1167. }
  1168. if (!(newcon->flags & CON_ENABLED))
  1169. return;
  1170. /*
  1171. * If we have a bootconsole, and are switching to a real console,
  1172. * don't print everything out again, since when the boot console, and
  1173. * the real console are the same physical device, it's annoying to
  1174. * see the beginning boot messages twice
  1175. */
  1176. if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
  1177. newcon->flags &= ~CON_PRINTBUFFER;
  1178. /*
  1179. * Put this console in the list - keep the
  1180. * preferred driver at the head of the list.
  1181. */
  1182. acquire_console_sem();
  1183. if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
  1184. newcon->next = console_drivers;
  1185. console_drivers = newcon;
  1186. if (newcon->next)
  1187. newcon->next->flags &= ~CON_CONSDEV;
  1188. } else {
  1189. newcon->next = console_drivers->next;
  1190. console_drivers->next = newcon;
  1191. }
  1192. if (newcon->flags & CON_PRINTBUFFER) {
  1193. /*
  1194. * release_console_sem() will print out the buffered messages
  1195. * for us.
  1196. */
  1197. spin_lock_irqsave(&logbuf_lock, flags);
  1198. con_start = log_start;
  1199. spin_unlock_irqrestore(&logbuf_lock, flags);
  1200. }
  1201. release_console_sem();
  1202. /*
  1203. * By unregistering the bootconsoles after we enable the real console
  1204. * we get the "console xxx enabled" message on all the consoles -
  1205. * boot consoles, real consoles, etc - this is to ensure that end
  1206. * users know there might be something in the kernel's log buffer that
  1207. * went to the bootconsole (that they do not see on the real console)
  1208. */
  1209. if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
  1210. /* we need to iterate through twice, to make sure we print
  1211. * everything out, before we unregister the console(s)
  1212. */
  1213. printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
  1214. newcon->name, newcon->index);
  1215. for_each_console(bcon)
  1216. if (bcon->flags & CON_BOOT)
  1217. unregister_console(bcon);
  1218. } else {
  1219. printk(KERN_INFO "%sconsole [%s%d] enabled\n",
  1220. (newcon->flags & CON_BOOT) ? "boot" : "" ,
  1221. newcon->name, newcon->index);
  1222. }
  1223. }
  1224. EXPORT_SYMBOL(register_console);
  1225. int unregister_console(struct console *console)
  1226. {
  1227. struct console *a, *b;
  1228. int res = 1;
  1229. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1230. if (console->flags & CON_BRL)
  1231. return braille_unregister_console(console);
  1232. #endif
  1233. acquire_console_sem();
  1234. if (console_drivers == console) {
  1235. console_drivers=console->next;
  1236. res = 0;
  1237. } else if (console_drivers) {
  1238. for (a=console_drivers->next, b=console_drivers ;
  1239. a; b=a, a=b->next) {
  1240. if (a == console) {
  1241. b->next = a->next;
  1242. res = 0;
  1243. break;
  1244. }
  1245. }
  1246. }
  1247. /*
  1248. * If this isn't the last console and it has CON_CONSDEV set, we
  1249. * need to set it on the next preferred console.
  1250. */
  1251. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  1252. console_drivers->flags |= CON_CONSDEV;
  1253. release_console_sem();
  1254. return res;
  1255. }
  1256. EXPORT_SYMBOL(unregister_console);
  1257. static int __init printk_late_init(void)
  1258. {
  1259. struct console *con;
  1260. for_each_console(con) {
  1261. if (con->flags & CON_BOOT) {
  1262. printk(KERN_INFO "turn off boot console %s%d\n",
  1263. con->name, con->index);
  1264. unregister_console(con);
  1265. }
  1266. }
  1267. hotcpu_notifier(console_cpu_notify, 0);
  1268. return 0;
  1269. }
  1270. late_initcall(printk_late_init);
  1271. #if defined CONFIG_PRINTK
  1272. /*
  1273. * printk rate limiting, lifted from the networking subsystem.
  1274. *
  1275. * This enforces a rate limit: not more than 10 kernel messages
  1276. * every 5s to make a denial-of-service attack impossible.
  1277. */
  1278. DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
  1279. int __printk_ratelimit(const char *func)
  1280. {
  1281. return ___ratelimit(&printk_ratelimit_state, func);
  1282. }
  1283. EXPORT_SYMBOL(__printk_ratelimit);
  1284. /**
  1285. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  1286. * @caller_jiffies: pointer to caller's state
  1287. * @interval_msecs: minimum interval between prints
  1288. *
  1289. * printk_timed_ratelimit() returns true if more than @interval_msecs
  1290. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  1291. * returned true.
  1292. */
  1293. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  1294. unsigned int interval_msecs)
  1295. {
  1296. if (*caller_jiffies == 0
  1297. || !time_in_range(jiffies, *caller_jiffies,
  1298. *caller_jiffies
  1299. + msecs_to_jiffies(interval_msecs))) {
  1300. *caller_jiffies = jiffies;
  1301. return true;
  1302. }
  1303. return false;
  1304. }
  1305. EXPORT_SYMBOL(printk_timed_ratelimit);
  1306. static DEFINE_SPINLOCK(dump_list_lock);
  1307. static LIST_HEAD(dump_list);
  1308. /**
  1309. * kmsg_dump_register - register a kernel log dumper.
  1310. * @dumper: pointer to the kmsg_dumper structure
  1311. *
  1312. * Adds a kernel log dumper to the system. The dump callback in the
  1313. * structure will be called when the kernel oopses or panics and must be
  1314. * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
  1315. */
  1316. int kmsg_dump_register(struct kmsg_dumper *dumper)
  1317. {
  1318. unsigned long flags;
  1319. int err = -EBUSY;
  1320. /* The dump callback needs to be set */
  1321. if (!dumper->dump)
  1322. return -EINVAL;
  1323. spin_lock_irqsave(&dump_list_lock, flags);
  1324. /* Don't allow registering multiple times */
  1325. if (!dumper->registered) {
  1326. dumper->registered = 1;
  1327. list_add_tail(&dumper->list, &dump_list);
  1328. err = 0;
  1329. }
  1330. spin_unlock_irqrestore(&dump_list_lock, flags);
  1331. return err;
  1332. }
  1333. EXPORT_SYMBOL_GPL(kmsg_dump_register);
  1334. /**
  1335. * kmsg_dump_unregister - unregister a kmsg dumper.
  1336. * @dumper: pointer to the kmsg_dumper structure
  1337. *
  1338. * Removes a dump device from the system. Returns zero on success and
  1339. * %-EINVAL otherwise.
  1340. */
  1341. int kmsg_dump_unregister(struct kmsg_dumper *dumper)
  1342. {
  1343. unsigned long flags;
  1344. int err = -EINVAL;
  1345. spin_lock_irqsave(&dump_list_lock, flags);
  1346. if (dumper->registered) {
  1347. dumper->registered = 0;
  1348. list_del(&dumper->list);
  1349. err = 0;
  1350. }
  1351. spin_unlock_irqrestore(&dump_list_lock, flags);
  1352. return err;
  1353. }
  1354. EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
  1355. static const char * const kmsg_reasons[] = {
  1356. [KMSG_DUMP_OOPS] = "oops",
  1357. [KMSG_DUMP_PANIC] = "panic",
  1358. [KMSG_DUMP_KEXEC] = "kexec",
  1359. };
  1360. static const char *kmsg_to_str(enum kmsg_dump_reason reason)
  1361. {
  1362. if (reason >= ARRAY_SIZE(kmsg_reasons) || reason < 0)
  1363. return "unknown";
  1364. return kmsg_reasons[reason];
  1365. }
  1366. /**
  1367. * kmsg_dump - dump kernel log to kernel message dumpers.
  1368. * @reason: the reason (oops, panic etc) for dumping
  1369. *
  1370. * Iterate through each of the dump devices and call the oops/panic
  1371. * callbacks with the log buffer.
  1372. */
  1373. void kmsg_dump(enum kmsg_dump_reason reason)
  1374. {
  1375. unsigned long end;
  1376. unsigned chars;
  1377. struct kmsg_dumper *dumper;
  1378. const char *s1, *s2;
  1379. unsigned long l1, l2;
  1380. unsigned long flags;
  1381. /* Theoretically, the log could move on after we do this, but
  1382. there's not a lot we can do about that. The new messages
  1383. will overwrite the start of what we dump. */
  1384. spin_lock_irqsave(&logbuf_lock, flags);
  1385. end = log_end & LOG_BUF_MASK;
  1386. chars = logged_chars;
  1387. spin_unlock_irqrestore(&logbuf_lock, flags);
  1388. if (chars > end) {
  1389. s1 = log_buf + log_buf_len - chars + end;
  1390. l1 = chars - end;
  1391. s2 = log_buf;
  1392. l2 = end;
  1393. } else {
  1394. s1 = "";
  1395. l1 = 0;
  1396. s2 = log_buf + end - chars;
  1397. l2 = chars;
  1398. }
  1399. if (!spin_trylock_irqsave(&dump_list_lock, flags)) {
  1400. printk(KERN_ERR "dump_kmsg: dump list lock is held during %s, skipping dump\n",
  1401. kmsg_to_str(reason));
  1402. return;
  1403. }
  1404. list_for_each_entry(dumper, &dump_list, list)
  1405. dumper->dump(dumper, reason, s1, l1, s2, l2);
  1406. spin_unlock_irqrestore(&dump_list_lock, flags);
  1407. }
  1408. #endif