printk.c 35 KB

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