printk.c 40 KB

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