printk.c 35 KB

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