printk.c 38 KB

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