printk.c 39 KB

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