printk.c 40 KB

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