printk.c 38 KB

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