printk.c 35 KB

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