printk.c 38 KB

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