printk.c 38 KB

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