printk.c 35 KB

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