printk.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  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/memblock.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/kexec.h>
  36. #include <linux/kdb.h>
  37. #include <linux/ratelimit.h>
  38. #include <linux/kmsg_dump.h>
  39. #include <linux/syslog.h>
  40. #include <linux/cpu.h>
  41. #include <linux/notifier.h>
  42. #include <linux/rculist.h>
  43. #include <linux/poll.h>
  44. #include <asm/uaccess.h>
  45. #define CREATE_TRACE_POINTS
  46. #include <trace/events/printk.h>
  47. /*
  48. * Architectures can override it:
  49. */
  50. void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
  51. {
  52. }
  53. /* printk's without a loglevel use this.. */
  54. #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
  55. /* We show everything that is MORE important than this.. */
  56. #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
  57. #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
  58. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  59. int console_printk[4] = {
  60. DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
  61. DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
  62. MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
  63. DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
  64. };
  65. /*
  66. * Low level drivers may need that to know if they can schedule in
  67. * their unblank() callback or not. So let's export it.
  68. */
  69. int oops_in_progress;
  70. EXPORT_SYMBOL(oops_in_progress);
  71. /*
  72. * console_sem protects the console_drivers list, and also
  73. * provides serialisation for access to the entire console
  74. * driver system.
  75. */
  76. static DEFINE_SEMAPHORE(console_sem);
  77. struct console *console_drivers;
  78. EXPORT_SYMBOL_GPL(console_drivers);
  79. /*
  80. * This is used for debugging the mess that is the VT code by
  81. * keeping track if we have the console semaphore held. It's
  82. * definitely not the perfect debug tool (we don't know if _WE_
  83. * hold it are racing, but it helps tracking those weird code
  84. * path in the console code where we end up in places I want
  85. * locked without the console sempahore held
  86. */
  87. static int console_locked, console_suspended;
  88. /*
  89. * If exclusive_console is non-NULL then only this console is to be printed to.
  90. */
  91. static struct console *exclusive_console;
  92. /*
  93. * Array of consoles built from command line options (console=)
  94. */
  95. struct console_cmdline
  96. {
  97. char name[8]; /* Name of the driver */
  98. int index; /* Minor dev. to use */
  99. char *options; /* Options for the driver */
  100. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  101. char *brl_options; /* Options for braille driver */
  102. #endif
  103. };
  104. #define MAX_CMDLINECONSOLES 8
  105. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  106. static int selected_console = -1;
  107. static int preferred_console = -1;
  108. int console_set_on_cmdline;
  109. EXPORT_SYMBOL(console_set_on_cmdline);
  110. /* Flag: console code may call schedule() */
  111. static int console_may_schedule;
  112. /*
  113. * The printk log buffer consists of a chain of concatenated variable
  114. * length records. Every record starts with a record header, containing
  115. * the overall length of the record.
  116. *
  117. * The heads to the first and last entry in the buffer, as well as the
  118. * sequence numbers of these both entries are maintained when messages
  119. * are stored..
  120. *
  121. * If the heads indicate available messages, the length in the header
  122. * tells the start next message. A length == 0 for the next message
  123. * indicates a wrap-around to the beginning of the buffer.
  124. *
  125. * Every record carries the monotonic timestamp in microseconds, as well as
  126. * the standard userspace syslog level and syslog facility. The usual
  127. * kernel messages use LOG_KERN; userspace-injected messages always carry
  128. * a matching syslog facility, by default LOG_USER. The origin of every
  129. * message can be reliably determined that way.
  130. *
  131. * The human readable log message directly follows the message header. The
  132. * length of the message text is stored in the header, the stored message
  133. * is not terminated.
  134. *
  135. * Optionally, a message can carry a dictionary of properties (key/value pairs),
  136. * to provide userspace with a machine-readable message context.
  137. *
  138. * Examples for well-defined, commonly used property names are:
  139. * DEVICE=b12:8 device identifier
  140. * b12:8 block dev_t
  141. * c127:3 char dev_t
  142. * n8 netdev ifindex
  143. * +sound:card0 subsystem:devname
  144. * SUBSYSTEM=pci driver-core subsystem name
  145. *
  146. * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
  147. * follows directly after a '=' character. Every property is terminated by
  148. * a '\0' character. The last property is not terminated.
  149. *
  150. * Example of a message structure:
  151. * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
  152. * 0008 34 00 record is 52 bytes long
  153. * 000a 0b 00 text is 11 bytes long
  154. * 000c 1f 00 dictionary is 23 bytes long
  155. * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
  156. * 0010 69 74 27 73 20 61 20 6c "it's a l"
  157. * 69 6e 65 "ine"
  158. * 001b 44 45 56 49 43 "DEVIC"
  159. * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
  160. * 52 49 56 45 52 3d 62 75 "RIVER=bu"
  161. * 67 "g"
  162. * 0032 00 00 00 padding to next message header
  163. *
  164. * The 'struct log' buffer header must never be directly exported to
  165. * userspace, it is a kernel-private implementation detail that might
  166. * need to be changed in the future, when the requirements change.
  167. *
  168. * /dev/kmsg exports the structured data in the following line format:
  169. * "level,sequnum,timestamp;<message text>\n"
  170. *
  171. * The optional key/value pairs are attached as continuation lines starting
  172. * with a space character and terminated by a newline. All possible
  173. * non-prinatable characters are escaped in the "\xff" notation.
  174. *
  175. * Users of the export format should ignore possible additional values
  176. * separated by ',', and find the message after the ';' character.
  177. */
  178. struct log {
  179. u64 ts_nsec; /* timestamp in nanoseconds */
  180. u16 len; /* length of entire record */
  181. u16 text_len; /* length of text buffer */
  182. u16 dict_len; /* length of dictionary buffer */
  183. u16 level; /* syslog level + facility */
  184. };
  185. /*
  186. * The logbuf_lock protects kmsg buffer, indices, counters. It is also
  187. * used in interesting ways to provide interlocking in console_unlock();
  188. */
  189. static DEFINE_RAW_SPINLOCK(logbuf_lock);
  190. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  191. static u64 syslog_seq;
  192. static u32 syslog_idx;
  193. /* index and sequence number of the first record stored in the buffer */
  194. static u64 log_first_seq;
  195. static u32 log_first_idx;
  196. /* index and sequence number of the next record to store in the buffer */
  197. static u64 log_next_seq;
  198. #ifdef CONFIG_PRINTK
  199. static u32 log_next_idx;
  200. /* the next printk record to read after the last 'clear' command */
  201. static u64 clear_seq;
  202. static u32 clear_idx;
  203. #define LOG_LINE_MAX 1024
  204. /* record buffer */
  205. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  206. #define LOG_ALIGN 4
  207. #else
  208. #define LOG_ALIGN __alignof__(struct log)
  209. #endif
  210. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  211. static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
  212. static char *log_buf = __log_buf;
  213. static u32 log_buf_len = __LOG_BUF_LEN;
  214. /* cpu currently holding logbuf_lock */
  215. static volatile unsigned int logbuf_cpu = UINT_MAX;
  216. /* human readable text of the record */
  217. static char *log_text(const struct log *msg)
  218. {
  219. return (char *)msg + sizeof(struct log);
  220. }
  221. /* optional key/value pair dictionary attached to the record */
  222. static char *log_dict(const struct log *msg)
  223. {
  224. return (char *)msg + sizeof(struct log) + msg->text_len;
  225. }
  226. /* get record by index; idx must point to valid msg */
  227. static struct log *log_from_idx(u32 idx)
  228. {
  229. struct log *msg = (struct log *)(log_buf + idx);
  230. /*
  231. * A length == 0 record is the end of buffer marker. Wrap around and
  232. * read the message at the start of the buffer.
  233. */
  234. if (!msg->len)
  235. return (struct log *)log_buf;
  236. return msg;
  237. }
  238. /* get next record; idx must point to valid msg */
  239. static u32 log_next(u32 idx)
  240. {
  241. struct log *msg = (struct log *)(log_buf + idx);
  242. /* length == 0 indicates the end of the buffer; wrap */
  243. /*
  244. * A length == 0 record is the end of buffer marker. Wrap around and
  245. * read the message at the start of the buffer as *this* one, and
  246. * return the one after that.
  247. */
  248. if (!msg->len) {
  249. msg = (struct log *)log_buf;
  250. return msg->len;
  251. }
  252. return idx + msg->len;
  253. }
  254. /* insert record into the buffer, discard old ones, update heads */
  255. static void log_store(int facility, int level,
  256. const char *dict, u16 dict_len,
  257. const char *text, u16 text_len)
  258. {
  259. struct log *msg;
  260. u32 size, pad_len;
  261. /* number of '\0' padding bytes to next message */
  262. size = sizeof(struct log) + text_len + dict_len;
  263. pad_len = (-size) & (LOG_ALIGN - 1);
  264. size += pad_len;
  265. while (log_first_seq < log_next_seq) {
  266. u32 free;
  267. if (log_next_idx > log_first_idx)
  268. free = max(log_buf_len - log_next_idx, log_first_idx);
  269. else
  270. free = log_first_idx - log_next_idx;
  271. if (free > size + sizeof(struct log))
  272. break;
  273. /* drop old messages until we have enough contiuous space */
  274. log_first_idx = log_next(log_first_idx);
  275. log_first_seq++;
  276. }
  277. if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
  278. /*
  279. * This message + an additional empty header does not fit
  280. * at the end of the buffer. Add an empty header with len == 0
  281. * to signify a wrap around.
  282. */
  283. memset(log_buf + log_next_idx, 0, sizeof(struct log));
  284. log_next_idx = 0;
  285. }
  286. /* fill message */
  287. msg = (struct log *)(log_buf + log_next_idx);
  288. memcpy(log_text(msg), text, text_len);
  289. msg->text_len = text_len;
  290. memcpy(log_dict(msg), dict, dict_len);
  291. msg->dict_len = dict_len;
  292. msg->level = (facility << 3) | (level & 7);
  293. msg->ts_nsec = local_clock();
  294. memset(log_dict(msg) + dict_len, 0, pad_len);
  295. msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
  296. /* insert message */
  297. log_next_idx += msg->len;
  298. log_next_seq++;
  299. }
  300. /* /dev/kmsg - userspace message inject/listen interface */
  301. struct devkmsg_user {
  302. u64 seq;
  303. u32 idx;
  304. struct mutex lock;
  305. char buf[8192];
  306. };
  307. static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv,
  308. unsigned long count, loff_t pos)
  309. {
  310. char *buf, *line;
  311. int i;
  312. int level = default_message_loglevel;
  313. int facility = 1; /* LOG_USER */
  314. size_t len = iov_length(iv, count);
  315. ssize_t ret = len;
  316. if (len > LOG_LINE_MAX)
  317. return -EINVAL;
  318. buf = kmalloc(len+1, GFP_KERNEL);
  319. if (buf == NULL)
  320. return -ENOMEM;
  321. line = buf;
  322. for (i = 0; i < count; i++) {
  323. if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len))
  324. goto out;
  325. line += iv[i].iov_len;
  326. }
  327. /*
  328. * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
  329. * the decimal value represents 32bit, the lower 3 bit are the log
  330. * level, the rest are the log facility.
  331. *
  332. * If no prefix or no userspace facility is specified, we
  333. * enforce LOG_USER, to be able to reliably distinguish
  334. * kernel-generated messages from userspace-injected ones.
  335. */
  336. line = buf;
  337. if (line[0] == '<') {
  338. char *endp = NULL;
  339. i = simple_strtoul(line+1, &endp, 10);
  340. if (endp && endp[0] == '>') {
  341. level = i & 7;
  342. if (i >> 3)
  343. facility = i >> 3;
  344. endp++;
  345. len -= endp - line;
  346. line = endp;
  347. }
  348. }
  349. line[len] = '\0';
  350. printk_emit(facility, level, NULL, 0, "%s", line);
  351. out:
  352. kfree(buf);
  353. return ret;
  354. }
  355. static ssize_t devkmsg_read(struct file *file, char __user *buf,
  356. size_t count, loff_t *ppos)
  357. {
  358. struct devkmsg_user *user = file->private_data;
  359. struct log *msg;
  360. u64 ts_usec;
  361. size_t i;
  362. size_t len;
  363. ssize_t ret;
  364. if (!user)
  365. return -EBADF;
  366. ret = mutex_lock_interruptible(&user->lock);
  367. if (ret)
  368. return ret;
  369. raw_spin_lock(&logbuf_lock);
  370. while (user->seq == log_next_seq) {
  371. if (file->f_flags & O_NONBLOCK) {
  372. ret = -EAGAIN;
  373. raw_spin_unlock(&logbuf_lock);
  374. goto out;
  375. }
  376. raw_spin_unlock(&logbuf_lock);
  377. ret = wait_event_interruptible(log_wait,
  378. user->seq != log_next_seq);
  379. if (ret)
  380. goto out;
  381. raw_spin_lock(&logbuf_lock);
  382. }
  383. if (user->seq < log_first_seq) {
  384. /* our last seen message is gone, return error and reset */
  385. user->idx = log_first_idx;
  386. user->seq = log_first_seq;
  387. ret = -EPIPE;
  388. raw_spin_unlock(&logbuf_lock);
  389. goto out;
  390. }
  391. msg = log_from_idx(user->idx);
  392. ts_usec = msg->ts_nsec;
  393. do_div(ts_usec, 1000);
  394. len = sprintf(user->buf, "%u,%llu,%llu;",
  395. msg->level, user->seq, ts_usec);
  396. /* escape non-printable characters */
  397. for (i = 0; i < msg->text_len; i++) {
  398. unsigned char c = log_text(msg)[i];
  399. if (c < ' ' || c >= 128)
  400. len += sprintf(user->buf + len, "\\x%02x", c);
  401. else
  402. user->buf[len++] = c;
  403. }
  404. user->buf[len++] = '\n';
  405. if (msg->dict_len) {
  406. bool line = true;
  407. for (i = 0; i < msg->dict_len; i++) {
  408. unsigned char c = log_dict(msg)[i];
  409. if (line) {
  410. user->buf[len++] = ' ';
  411. line = false;
  412. }
  413. if (c == '\0') {
  414. user->buf[len++] = '\n';
  415. line = true;
  416. continue;
  417. }
  418. if (c < ' ' || c >= 128) {
  419. len += sprintf(user->buf + len, "\\x%02x", c);
  420. continue;
  421. }
  422. user->buf[len++] = c;
  423. }
  424. user->buf[len++] = '\n';
  425. }
  426. user->idx = log_next(user->idx);
  427. user->seq++;
  428. raw_spin_unlock(&logbuf_lock);
  429. if (len > count) {
  430. ret = -EINVAL;
  431. goto out;
  432. }
  433. if (copy_to_user(buf, user->buf, len)) {
  434. ret = -EFAULT;
  435. goto out;
  436. }
  437. ret = len;
  438. out:
  439. mutex_unlock(&user->lock);
  440. return ret;
  441. }
  442. static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
  443. {
  444. struct devkmsg_user *user = file->private_data;
  445. loff_t ret = 0;
  446. if (!user)
  447. return -EBADF;
  448. if (offset)
  449. return -ESPIPE;
  450. raw_spin_lock(&logbuf_lock);
  451. switch (whence) {
  452. case SEEK_SET:
  453. /* the first record */
  454. user->idx = log_first_idx;
  455. user->seq = log_first_seq;
  456. break;
  457. case SEEK_DATA:
  458. /*
  459. * The first record after the last SYSLOG_ACTION_CLEAR,
  460. * like issued by 'dmesg -c'. Reading /dev/kmsg itself
  461. * changes no global state, and does not clear anything.
  462. */
  463. user->idx = clear_idx;
  464. user->seq = clear_seq;
  465. break;
  466. case SEEK_END:
  467. /* after the last record */
  468. user->idx = log_next_idx;
  469. user->seq = log_next_seq;
  470. break;
  471. default:
  472. ret = -EINVAL;
  473. }
  474. raw_spin_unlock(&logbuf_lock);
  475. return ret;
  476. }
  477. static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
  478. {
  479. struct devkmsg_user *user = file->private_data;
  480. int ret = 0;
  481. if (!user)
  482. return POLLERR|POLLNVAL;
  483. poll_wait(file, &log_wait, wait);
  484. raw_spin_lock(&logbuf_lock);
  485. if (user->seq < log_next_seq) {
  486. /* return error when data has vanished underneath us */
  487. if (user->seq < log_first_seq)
  488. ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
  489. ret = POLLIN|POLLRDNORM;
  490. }
  491. raw_spin_unlock(&logbuf_lock);
  492. return ret;
  493. }
  494. static int devkmsg_open(struct inode *inode, struct file *file)
  495. {
  496. struct devkmsg_user *user;
  497. int err;
  498. /* write-only does not need any file context */
  499. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  500. return 0;
  501. err = security_syslog(SYSLOG_ACTION_READ_ALL);
  502. if (err)
  503. return err;
  504. user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
  505. if (!user)
  506. return -ENOMEM;
  507. mutex_init(&user->lock);
  508. raw_spin_lock(&logbuf_lock);
  509. user->idx = log_first_idx;
  510. user->seq = log_first_seq;
  511. raw_spin_unlock(&logbuf_lock);
  512. file->private_data = user;
  513. return 0;
  514. }
  515. static int devkmsg_release(struct inode *inode, struct file *file)
  516. {
  517. struct devkmsg_user *user = file->private_data;
  518. if (!user)
  519. return 0;
  520. mutex_destroy(&user->lock);
  521. kfree(user);
  522. return 0;
  523. }
  524. const struct file_operations kmsg_fops = {
  525. .open = devkmsg_open,
  526. .read = devkmsg_read,
  527. .aio_write = devkmsg_writev,
  528. .llseek = devkmsg_llseek,
  529. .poll = devkmsg_poll,
  530. .release = devkmsg_release,
  531. };
  532. #ifdef CONFIG_KEXEC
  533. /*
  534. * This appends the listed symbols to /proc/vmcoreinfo
  535. *
  536. * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
  537. * obtain access to symbols that are otherwise very difficult to locate. These
  538. * symbols are specifically used so that utilities can access and extract the
  539. * dmesg log from a vmcore file after a crash.
  540. */
  541. void log_buf_kexec_setup(void)
  542. {
  543. VMCOREINFO_SYMBOL(log_buf);
  544. VMCOREINFO_SYMBOL(log_buf_len);
  545. VMCOREINFO_SYMBOL(log_first_idx);
  546. VMCOREINFO_SYMBOL(log_next_idx);
  547. }
  548. #endif
  549. /* requested log_buf_len from kernel cmdline */
  550. static unsigned long __initdata new_log_buf_len;
  551. /* save requested log_buf_len since it's too early to process it */
  552. static int __init log_buf_len_setup(char *str)
  553. {
  554. unsigned size = memparse(str, &str);
  555. if (size)
  556. size = roundup_pow_of_two(size);
  557. if (size > log_buf_len)
  558. new_log_buf_len = size;
  559. return 0;
  560. }
  561. early_param("log_buf_len", log_buf_len_setup);
  562. void __init setup_log_buf(int early)
  563. {
  564. unsigned long flags;
  565. char *new_log_buf;
  566. int free;
  567. if (!new_log_buf_len)
  568. return;
  569. if (early) {
  570. unsigned long mem;
  571. mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
  572. if (!mem)
  573. return;
  574. new_log_buf = __va(mem);
  575. } else {
  576. new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
  577. }
  578. if (unlikely(!new_log_buf)) {
  579. pr_err("log_buf_len: %ld bytes not available\n",
  580. new_log_buf_len);
  581. return;
  582. }
  583. raw_spin_lock_irqsave(&logbuf_lock, flags);
  584. log_buf_len = new_log_buf_len;
  585. log_buf = new_log_buf;
  586. new_log_buf_len = 0;
  587. free = __LOG_BUF_LEN - log_next_idx;
  588. memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
  589. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  590. pr_info("log_buf_len: %d\n", log_buf_len);
  591. pr_info("early log buf free: %d(%d%%)\n",
  592. free, (free * 100) / __LOG_BUF_LEN);
  593. }
  594. #ifdef CONFIG_BOOT_PRINTK_DELAY
  595. static int boot_delay; /* msecs delay after each printk during bootup */
  596. static unsigned long long loops_per_msec; /* based on boot_delay */
  597. static int __init boot_delay_setup(char *str)
  598. {
  599. unsigned long lpj;
  600. lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
  601. loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
  602. get_option(&str, &boot_delay);
  603. if (boot_delay > 10 * 1000)
  604. boot_delay = 0;
  605. pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
  606. "HZ: %d, loops_per_msec: %llu\n",
  607. boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
  608. return 1;
  609. }
  610. __setup("boot_delay=", boot_delay_setup);
  611. static void boot_delay_msec(void)
  612. {
  613. unsigned long long k;
  614. unsigned long timeout;
  615. if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
  616. return;
  617. k = (unsigned long long)loops_per_msec * boot_delay;
  618. timeout = jiffies + msecs_to_jiffies(boot_delay);
  619. while (k) {
  620. k--;
  621. cpu_relax();
  622. /*
  623. * use (volatile) jiffies to prevent
  624. * compiler reduction; loop termination via jiffies
  625. * is secondary and may or may not happen.
  626. */
  627. if (time_after(jiffies, timeout))
  628. break;
  629. touch_nmi_watchdog();
  630. }
  631. }
  632. #else
  633. static inline void boot_delay_msec(void)
  634. {
  635. }
  636. #endif
  637. #ifdef CONFIG_SECURITY_DMESG_RESTRICT
  638. int dmesg_restrict = 1;
  639. #else
  640. int dmesg_restrict;
  641. #endif
  642. static int syslog_action_restricted(int type)
  643. {
  644. if (dmesg_restrict)
  645. return 1;
  646. /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
  647. return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
  648. }
  649. static int check_syslog_permissions(int type, bool from_file)
  650. {
  651. /*
  652. * If this is from /proc/kmsg and we've already opened it, then we've
  653. * already done the capabilities checks at open time.
  654. */
  655. if (from_file && type != SYSLOG_ACTION_OPEN)
  656. return 0;
  657. if (syslog_action_restricted(type)) {
  658. if (capable(CAP_SYSLOG))
  659. return 0;
  660. /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
  661. if (capable(CAP_SYS_ADMIN)) {
  662. printk_once(KERN_WARNING "%s (%d): "
  663. "Attempt to access syslog with CAP_SYS_ADMIN "
  664. "but no CAP_SYSLOG (deprecated).\n",
  665. current->comm, task_pid_nr(current));
  666. return 0;
  667. }
  668. return -EPERM;
  669. }
  670. return 0;
  671. }
  672. #if defined(CONFIG_PRINTK_TIME)
  673. static bool printk_time = 1;
  674. #else
  675. static bool printk_time;
  676. #endif
  677. module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
  678. static size_t print_prefix(const struct log *msg, bool syslog, char *buf)
  679. {
  680. size_t len = 0;
  681. if (syslog) {
  682. if (buf) {
  683. len += sprintf(buf, "<%u>", msg->level);
  684. } else {
  685. len += 3;
  686. if (msg->level > 9)
  687. len++;
  688. if (msg->level > 99)
  689. len++;
  690. }
  691. }
  692. if (printk_time) {
  693. if (buf) {
  694. unsigned long long ts = msg->ts_nsec;
  695. unsigned long rem_nsec = do_div(ts, 1000000000);
  696. len += sprintf(buf + len, "[%5lu.%06lu] ",
  697. (unsigned long) ts, rem_nsec / 1000);
  698. } else {
  699. len += 15;
  700. }
  701. }
  702. return len;
  703. }
  704. static size_t msg_print_text(const struct log *msg, bool syslog,
  705. char *buf, size_t size)
  706. {
  707. const char *text = log_text(msg);
  708. size_t text_size = msg->text_len;
  709. size_t len = 0;
  710. do {
  711. const char *next = memchr(text, '\n', text_size);
  712. size_t text_len;
  713. if (next) {
  714. text_len = next - text;
  715. next++;
  716. text_size -= next - text;
  717. } else {
  718. text_len = text_size;
  719. }
  720. if (buf) {
  721. if (print_prefix(msg, syslog, NULL) +
  722. text_len + 1>= size - len)
  723. break;
  724. len += print_prefix(msg, syslog, buf + len);
  725. memcpy(buf + len, text, text_len);
  726. len += text_len;
  727. buf[len++] = '\n';
  728. } else {
  729. /* SYSLOG_ACTION_* buffer size only calculation */
  730. len += print_prefix(msg, syslog, NULL);
  731. len += text_len + 1;
  732. }
  733. text = next;
  734. } while (text);
  735. return len;
  736. }
  737. static int syslog_print(char __user *buf, int size)
  738. {
  739. char *text;
  740. struct log *msg;
  741. int len;
  742. text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
  743. if (!text)
  744. return -ENOMEM;
  745. raw_spin_lock_irq(&logbuf_lock);
  746. if (syslog_seq < log_first_seq) {
  747. /* messages are gone, move to first one */
  748. syslog_seq = log_first_seq;
  749. syslog_idx = log_first_idx;
  750. }
  751. msg = log_from_idx(syslog_idx);
  752. len = msg_print_text(msg, true, text, LOG_LINE_MAX);
  753. syslog_idx = log_next(syslog_idx);
  754. syslog_seq++;
  755. raw_spin_unlock_irq(&logbuf_lock);
  756. if (len > size)
  757. len = -EINVAL;
  758. else if (len > 0 && copy_to_user(buf, text, len))
  759. len = -EFAULT;
  760. kfree(text);
  761. return len;
  762. }
  763. static int syslog_print_all(char __user *buf, int size, bool clear)
  764. {
  765. char *text;
  766. int len = 0;
  767. text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
  768. if (!text)
  769. return -ENOMEM;
  770. raw_spin_lock_irq(&logbuf_lock);
  771. if (buf) {
  772. u64 next_seq;
  773. u64 seq;
  774. u32 idx;
  775. if (clear_seq < log_first_seq) {
  776. /* messages are gone, move to first available one */
  777. clear_seq = log_first_seq;
  778. clear_idx = log_first_idx;
  779. }
  780. /*
  781. * Find first record that fits, including all following records,
  782. * into the user-provided buffer for this dump.
  783. */
  784. seq = clear_seq;
  785. idx = clear_idx;
  786. while (seq < log_next_seq) {
  787. struct log *msg = log_from_idx(idx);
  788. len += msg_print_text(msg, true, NULL, 0);
  789. idx = log_next(idx);
  790. seq++;
  791. }
  792. /* move first record forward until length fits into the buffer */
  793. seq = clear_seq;
  794. idx = clear_idx;
  795. while (len > size && seq < log_next_seq) {
  796. struct log *msg = log_from_idx(idx);
  797. len -= msg_print_text(msg, true, NULL, 0);
  798. idx = log_next(idx);
  799. seq++;
  800. }
  801. /* last message fitting into this dump */
  802. next_seq = log_next_seq;
  803. len = 0;
  804. while (len >= 0 && seq < next_seq) {
  805. struct log *msg = log_from_idx(idx);
  806. int textlen;
  807. textlen = msg_print_text(msg, true, text, LOG_LINE_MAX);
  808. if (textlen < 0) {
  809. len = textlen;
  810. break;
  811. }
  812. idx = log_next(idx);
  813. seq++;
  814. raw_spin_unlock_irq(&logbuf_lock);
  815. if (copy_to_user(buf + len, text, textlen))
  816. len = -EFAULT;
  817. else
  818. len += textlen;
  819. raw_spin_lock_irq(&logbuf_lock);
  820. if (seq < log_first_seq) {
  821. /* messages are gone, move to next one */
  822. seq = log_first_seq;
  823. idx = log_first_idx;
  824. }
  825. }
  826. }
  827. if (clear) {
  828. clear_seq = log_next_seq;
  829. clear_idx = log_next_idx;
  830. }
  831. raw_spin_unlock_irq(&logbuf_lock);
  832. kfree(text);
  833. return len;
  834. }
  835. int do_syslog(int type, char __user *buf, int len, bool from_file)
  836. {
  837. bool clear = false;
  838. static int saved_console_loglevel = -1;
  839. static DEFINE_MUTEX(syslog_mutex);
  840. int error;
  841. error = check_syslog_permissions(type, from_file);
  842. if (error)
  843. goto out;
  844. error = security_syslog(type);
  845. if (error)
  846. return error;
  847. switch (type) {
  848. case SYSLOG_ACTION_CLOSE: /* Close log */
  849. break;
  850. case SYSLOG_ACTION_OPEN: /* Open log */
  851. break;
  852. case SYSLOG_ACTION_READ: /* Read from log */
  853. error = -EINVAL;
  854. if (!buf || len < 0)
  855. goto out;
  856. error = 0;
  857. if (!len)
  858. goto out;
  859. if (!access_ok(VERIFY_WRITE, buf, len)) {
  860. error = -EFAULT;
  861. goto out;
  862. }
  863. error = mutex_lock_interruptible(&syslog_mutex);
  864. if (error)
  865. goto out;
  866. error = wait_event_interruptible(log_wait,
  867. syslog_seq != log_next_seq);
  868. if (error) {
  869. mutex_unlock(&syslog_mutex);
  870. goto out;
  871. }
  872. error = syslog_print(buf, len);
  873. mutex_unlock(&syslog_mutex);
  874. break;
  875. /* Read/clear last kernel messages */
  876. case SYSLOG_ACTION_READ_CLEAR:
  877. clear = true;
  878. /* FALL THRU */
  879. /* Read last kernel messages */
  880. case SYSLOG_ACTION_READ_ALL:
  881. error = -EINVAL;
  882. if (!buf || len < 0)
  883. goto out;
  884. error = 0;
  885. if (!len)
  886. goto out;
  887. if (!access_ok(VERIFY_WRITE, buf, len)) {
  888. error = -EFAULT;
  889. goto out;
  890. }
  891. error = syslog_print_all(buf, len, clear);
  892. break;
  893. /* Clear ring buffer */
  894. case SYSLOG_ACTION_CLEAR:
  895. syslog_print_all(NULL, 0, true);
  896. /* Disable logging to console */
  897. case SYSLOG_ACTION_CONSOLE_OFF:
  898. if (saved_console_loglevel == -1)
  899. saved_console_loglevel = console_loglevel;
  900. console_loglevel = minimum_console_loglevel;
  901. break;
  902. /* Enable logging to console */
  903. case SYSLOG_ACTION_CONSOLE_ON:
  904. if (saved_console_loglevel != -1) {
  905. console_loglevel = saved_console_loglevel;
  906. saved_console_loglevel = -1;
  907. }
  908. break;
  909. /* Set level of messages printed to console */
  910. case SYSLOG_ACTION_CONSOLE_LEVEL:
  911. error = -EINVAL;
  912. if (len < 1 || len > 8)
  913. goto out;
  914. if (len < minimum_console_loglevel)
  915. len = minimum_console_loglevel;
  916. console_loglevel = len;
  917. /* Implicitly re-enable logging to console */
  918. saved_console_loglevel = -1;
  919. error = 0;
  920. break;
  921. /* Number of chars in the log buffer */
  922. case SYSLOG_ACTION_SIZE_UNREAD:
  923. raw_spin_lock_irq(&logbuf_lock);
  924. if (syslog_seq < log_first_seq) {
  925. /* messages are gone, move to first one */
  926. syslog_seq = log_first_seq;
  927. syslog_idx = log_first_idx;
  928. }
  929. if (from_file) {
  930. /*
  931. * Short-cut for poll(/"proc/kmsg") which simply checks
  932. * for pending data, not the size; return the count of
  933. * records, not the length.
  934. */
  935. error = log_next_idx - syslog_idx;
  936. } else {
  937. u64 seq;
  938. u32 idx;
  939. error = 0;
  940. seq = syslog_seq;
  941. idx = syslog_idx;
  942. while (seq < log_next_seq) {
  943. struct log *msg = log_from_idx(idx);
  944. error += msg_print_text(msg, true, NULL, 0);
  945. idx = log_next(idx);
  946. seq++;
  947. }
  948. }
  949. raw_spin_unlock_irq(&logbuf_lock);
  950. break;
  951. /* Size of the log buffer */
  952. case SYSLOG_ACTION_SIZE_BUFFER:
  953. error = log_buf_len;
  954. break;
  955. default:
  956. error = -EINVAL;
  957. break;
  958. }
  959. out:
  960. return error;
  961. }
  962. SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
  963. {
  964. return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
  965. }
  966. #ifdef CONFIG_KGDB_KDB
  967. /* kdb dmesg command needs access to the syslog buffer. do_syslog()
  968. * uses locks so it cannot be used during debugging. Just tell kdb
  969. * where the start and end of the physical and logical logs are. This
  970. * is equivalent to do_syslog(3).
  971. */
  972. void kdb_syslog_data(char *syslog_data[4])
  973. {
  974. syslog_data[0] = log_buf;
  975. syslog_data[1] = log_buf + log_buf_len;
  976. syslog_data[2] = log_buf + log_first_idx;
  977. syslog_data[3] = log_buf + log_next_idx;
  978. }
  979. #endif /* CONFIG_KGDB_KDB */
  980. static bool __read_mostly ignore_loglevel;
  981. static int __init ignore_loglevel_setup(char *str)
  982. {
  983. ignore_loglevel = 1;
  984. printk(KERN_INFO "debug: ignoring loglevel setting.\n");
  985. return 0;
  986. }
  987. early_param("ignore_loglevel", ignore_loglevel_setup);
  988. module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
  989. MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
  990. "print all kernel messages to the console.");
  991. /*
  992. * Call the console drivers, asking them to write out
  993. * log_buf[start] to log_buf[end - 1].
  994. * The console_lock must be held.
  995. */
  996. static void call_console_drivers(int level, const char *text, size_t len)
  997. {
  998. struct console *con;
  999. trace_console(text, 0, len, len);
  1000. if (level >= console_loglevel && !ignore_loglevel)
  1001. return;
  1002. if (!console_drivers)
  1003. return;
  1004. for_each_console(con) {
  1005. if (exclusive_console && con != exclusive_console)
  1006. continue;
  1007. if (!(con->flags & CON_ENABLED))
  1008. continue;
  1009. if (!con->write)
  1010. continue;
  1011. if (!cpu_online(smp_processor_id()) &&
  1012. !(con->flags & CON_ANYTIME))
  1013. continue;
  1014. con->write(con, text, len);
  1015. }
  1016. }
  1017. /*
  1018. * Zap console related locks when oopsing. Only zap at most once
  1019. * every 10 seconds, to leave time for slow consoles to print a
  1020. * full oops.
  1021. */
  1022. static void zap_locks(void)
  1023. {
  1024. static unsigned long oops_timestamp;
  1025. if (time_after_eq(jiffies, oops_timestamp) &&
  1026. !time_after(jiffies, oops_timestamp + 30 * HZ))
  1027. return;
  1028. oops_timestamp = jiffies;
  1029. debug_locks_off();
  1030. /* If a crash is occurring, make sure we can't deadlock */
  1031. raw_spin_lock_init(&logbuf_lock);
  1032. /* And make sure that we print immediately */
  1033. sema_init(&console_sem, 1);
  1034. }
  1035. /* Check if we have any console registered that can be called early in boot. */
  1036. static int have_callable_console(void)
  1037. {
  1038. struct console *con;
  1039. for_each_console(con)
  1040. if (con->flags & CON_ANYTIME)
  1041. return 1;
  1042. return 0;
  1043. }
  1044. /*
  1045. * Can we actually use the console at this time on this cpu?
  1046. *
  1047. * Console drivers may assume that per-cpu resources have
  1048. * been allocated. So unless they're explicitly marked as
  1049. * being able to cope (CON_ANYTIME) don't call them until
  1050. * this CPU is officially up.
  1051. */
  1052. static inline int can_use_console(unsigned int cpu)
  1053. {
  1054. return cpu_online(cpu) || have_callable_console();
  1055. }
  1056. /*
  1057. * Try to get console ownership to actually show the kernel
  1058. * messages from a 'printk'. Return true (and with the
  1059. * console_lock held, and 'console_locked' set) if it
  1060. * is successful, false otherwise.
  1061. *
  1062. * This gets called with the 'logbuf_lock' spinlock held and
  1063. * interrupts disabled. It should return with 'lockbuf_lock'
  1064. * released but interrupts still disabled.
  1065. */
  1066. static int console_trylock_for_printk(unsigned int cpu)
  1067. __releases(&logbuf_lock)
  1068. {
  1069. int retval = 0, wake = 0;
  1070. if (console_trylock()) {
  1071. retval = 1;
  1072. /*
  1073. * If we can't use the console, we need to release
  1074. * the console semaphore by hand to avoid flushing
  1075. * the buffer. We need to hold the console semaphore
  1076. * in order to do this test safely.
  1077. */
  1078. if (!can_use_console(cpu)) {
  1079. console_locked = 0;
  1080. wake = 1;
  1081. retval = 0;
  1082. }
  1083. }
  1084. logbuf_cpu = UINT_MAX;
  1085. if (wake)
  1086. up(&console_sem);
  1087. raw_spin_unlock(&logbuf_lock);
  1088. return retval;
  1089. }
  1090. int printk_delay_msec __read_mostly;
  1091. static inline void printk_delay(void)
  1092. {
  1093. if (unlikely(printk_delay_msec)) {
  1094. int m = printk_delay_msec;
  1095. while (m--) {
  1096. mdelay(1);
  1097. touch_nmi_watchdog();
  1098. }
  1099. }
  1100. }
  1101. asmlinkage int vprintk_emit(int facility, int level,
  1102. const char *dict, size_t dictlen,
  1103. const char *fmt, va_list args)
  1104. {
  1105. static int recursion_bug;
  1106. static char cont_buf[LOG_LINE_MAX];
  1107. static size_t cont_len;
  1108. static int cont_level;
  1109. static struct task_struct *cont_task;
  1110. static char textbuf[LOG_LINE_MAX];
  1111. char *text = textbuf;
  1112. size_t text_len;
  1113. unsigned long flags;
  1114. int this_cpu;
  1115. bool newline = false;
  1116. bool prefix = false;
  1117. int printed_len = 0;
  1118. boot_delay_msec();
  1119. printk_delay();
  1120. /* This stops the holder of console_sem just where we want him */
  1121. local_irq_save(flags);
  1122. this_cpu = smp_processor_id();
  1123. /*
  1124. * Ouch, printk recursed into itself!
  1125. */
  1126. if (unlikely(logbuf_cpu == this_cpu)) {
  1127. /*
  1128. * If a crash is occurring during printk() on this CPU,
  1129. * then try to get the crash message out but make sure
  1130. * we can't deadlock. Otherwise just return to avoid the
  1131. * recursion and return - but flag the recursion so that
  1132. * it can be printed at the next appropriate moment:
  1133. */
  1134. if (!oops_in_progress && !lockdep_recursing(current)) {
  1135. recursion_bug = 1;
  1136. goto out_restore_irqs;
  1137. }
  1138. zap_locks();
  1139. }
  1140. lockdep_off();
  1141. raw_spin_lock(&logbuf_lock);
  1142. logbuf_cpu = this_cpu;
  1143. if (recursion_bug) {
  1144. static const char recursion_msg[] =
  1145. "BUG: recent printk recursion!";
  1146. recursion_bug = 0;
  1147. printed_len += strlen(recursion_msg);
  1148. /* emit KERN_CRIT message */
  1149. log_store(0, 2, NULL, 0, recursion_msg, printed_len);
  1150. }
  1151. /*
  1152. * The printf needs to come first; we need the syslog
  1153. * prefix which might be passed-in as a parameter.
  1154. */
  1155. text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
  1156. /* mark and strip a trailing newline */
  1157. if (text_len && text[text_len-1] == '\n') {
  1158. text_len--;
  1159. newline = true;
  1160. }
  1161. /* strip syslog prefix and extract log level or control flags */
  1162. if (text[0] == '<' && text[1] && text[2] == '>') {
  1163. switch (text[1]) {
  1164. case '0' ... '7':
  1165. if (level == -1)
  1166. level = text[1] - '0';
  1167. case 'd': /* KERN_DEFAULT */
  1168. prefix = true;
  1169. case 'c': /* KERN_CONT */
  1170. text += 3;
  1171. text_len -= 3;
  1172. }
  1173. }
  1174. if (level == -1)
  1175. level = default_message_loglevel;
  1176. if (dict) {
  1177. prefix = true;
  1178. newline = true;
  1179. }
  1180. if (!newline) {
  1181. if (cont_len && (prefix || cont_task != current)) {
  1182. /*
  1183. * Flush earlier buffer, which is either from a
  1184. * different thread, or when we got a new prefix.
  1185. */
  1186. log_store(facility, cont_level, NULL, 0, cont_buf, cont_len);
  1187. cont_len = 0;
  1188. }
  1189. if (!cont_len) {
  1190. cont_level = level;
  1191. cont_task = current;
  1192. }
  1193. /* buffer or append to earlier buffer from the same thread */
  1194. if (cont_len + text_len > sizeof(cont_buf))
  1195. text_len = sizeof(cont_buf) - cont_len;
  1196. memcpy(cont_buf + cont_len, text, text_len);
  1197. cont_len += text_len;
  1198. } else {
  1199. if (cont_len && cont_task == current) {
  1200. if (prefix) {
  1201. /*
  1202. * New prefix from the same thread; flush. We
  1203. * either got no earlier newline, or we race
  1204. * with an interrupt.
  1205. */
  1206. log_store(facility, cont_level,
  1207. NULL, 0, cont_buf, cont_len);
  1208. cont_len = 0;
  1209. }
  1210. /* append to the earlier buffer and flush */
  1211. if (cont_len + text_len > sizeof(cont_buf))
  1212. text_len = sizeof(cont_buf) - cont_len;
  1213. memcpy(cont_buf + cont_len, text, text_len);
  1214. cont_len += text_len;
  1215. log_store(facility, cont_level,
  1216. NULL, 0, cont_buf, cont_len);
  1217. cont_len = 0;
  1218. cont_task = NULL;
  1219. printed_len = cont_len;
  1220. } else {
  1221. /* ordinary single and terminated line */
  1222. log_store(facility, level,
  1223. dict, dictlen, text, text_len);
  1224. printed_len = text_len;
  1225. }
  1226. }
  1227. /*
  1228. * Try to acquire and then immediately release the console semaphore.
  1229. * The release will print out buffers and wake up /dev/kmsg and syslog()
  1230. * users.
  1231. *
  1232. * The console_trylock_for_printk() function will release 'logbuf_lock'
  1233. * regardless of whether it actually gets the console semaphore or not.
  1234. */
  1235. if (console_trylock_for_printk(this_cpu))
  1236. console_unlock();
  1237. lockdep_on();
  1238. out_restore_irqs:
  1239. local_irq_restore(flags);
  1240. return printed_len;
  1241. }
  1242. EXPORT_SYMBOL(vprintk_emit);
  1243. asmlinkage int vprintk(const char *fmt, va_list args)
  1244. {
  1245. return vprintk_emit(0, -1, NULL, 0, fmt, args);
  1246. }
  1247. EXPORT_SYMBOL(vprintk);
  1248. asmlinkage int printk_emit(int facility, int level,
  1249. const char *dict, size_t dictlen,
  1250. const char *fmt, ...)
  1251. {
  1252. va_list args;
  1253. int r;
  1254. va_start(args, fmt);
  1255. r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
  1256. va_end(args);
  1257. return r;
  1258. }
  1259. EXPORT_SYMBOL(printk_emit);
  1260. /**
  1261. * printk - print a kernel message
  1262. * @fmt: format string
  1263. *
  1264. * This is printk(). It can be called from any context. We want it to work.
  1265. *
  1266. * We try to grab the console_lock. If we succeed, it's easy - we log the
  1267. * output and call the console drivers. If we fail to get the semaphore, we
  1268. * place the output into the log buffer and return. The current holder of
  1269. * the console_sem will notice the new output in console_unlock(); and will
  1270. * send it to the consoles before releasing the lock.
  1271. *
  1272. * One effect of this deferred printing is that code which calls printk() and
  1273. * then changes console_loglevel may break. This is because console_loglevel
  1274. * is inspected when the actual printing occurs.
  1275. *
  1276. * See also:
  1277. * printf(3)
  1278. *
  1279. * See the vsnprintf() documentation for format string extensions over C99.
  1280. */
  1281. asmlinkage int printk(const char *fmt, ...)
  1282. {
  1283. va_list args;
  1284. int r;
  1285. #ifdef CONFIG_KGDB_KDB
  1286. if (unlikely(kdb_trap_printk)) {
  1287. va_start(args, fmt);
  1288. r = vkdb_printf(fmt, args);
  1289. va_end(args);
  1290. return r;
  1291. }
  1292. #endif
  1293. va_start(args, fmt);
  1294. r = vprintk_emit(0, -1, NULL, 0, fmt, args);
  1295. va_end(args);
  1296. return r;
  1297. }
  1298. EXPORT_SYMBOL(printk);
  1299. #else
  1300. #define LOG_LINE_MAX 0
  1301. static struct log *log_from_idx(u32 idx) { return NULL; }
  1302. static u32 log_next(u32 idx) { return 0; }
  1303. static void call_console_drivers(int level, const char *text, size_t len) {}
  1304. static size_t msg_print_text(const struct log *msg, bool syslog,
  1305. char *buf, size_t size) { return 0; }
  1306. #endif /* CONFIG_PRINTK */
  1307. static int __add_preferred_console(char *name, int idx, char *options,
  1308. char *brl_options)
  1309. {
  1310. struct console_cmdline *c;
  1311. int i;
  1312. /*
  1313. * See if this tty is not yet registered, and
  1314. * if we have a slot free.
  1315. */
  1316. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  1317. if (strcmp(console_cmdline[i].name, name) == 0 &&
  1318. console_cmdline[i].index == idx) {
  1319. if (!brl_options)
  1320. selected_console = i;
  1321. return 0;
  1322. }
  1323. if (i == MAX_CMDLINECONSOLES)
  1324. return -E2BIG;
  1325. if (!brl_options)
  1326. selected_console = i;
  1327. c = &console_cmdline[i];
  1328. strlcpy(c->name, name, sizeof(c->name));
  1329. c->options = options;
  1330. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1331. c->brl_options = brl_options;
  1332. #endif
  1333. c->index = idx;
  1334. return 0;
  1335. }
  1336. /*
  1337. * Set up a list of consoles. Called from init/main.c
  1338. */
  1339. static int __init console_setup(char *str)
  1340. {
  1341. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
  1342. char *s, *options, *brl_options = NULL;
  1343. int idx;
  1344. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1345. if (!memcmp(str, "brl,", 4)) {
  1346. brl_options = "";
  1347. str += 4;
  1348. } else if (!memcmp(str, "brl=", 4)) {
  1349. brl_options = str + 4;
  1350. str = strchr(brl_options, ',');
  1351. if (!str) {
  1352. printk(KERN_ERR "need port name after brl=\n");
  1353. return 1;
  1354. }
  1355. *(str++) = 0;
  1356. }
  1357. #endif
  1358. /*
  1359. * Decode str into name, index, options.
  1360. */
  1361. if (str[0] >= '0' && str[0] <= '9') {
  1362. strcpy(buf, "ttyS");
  1363. strncpy(buf + 4, str, sizeof(buf) - 5);
  1364. } else {
  1365. strncpy(buf, str, sizeof(buf) - 1);
  1366. }
  1367. buf[sizeof(buf) - 1] = 0;
  1368. if ((options = strchr(str, ',')) != NULL)
  1369. *(options++) = 0;
  1370. #ifdef __sparc__
  1371. if (!strcmp(str, "ttya"))
  1372. strcpy(buf, "ttyS0");
  1373. if (!strcmp(str, "ttyb"))
  1374. strcpy(buf, "ttyS1");
  1375. #endif
  1376. for (s = buf; *s; s++)
  1377. if ((*s >= '0' && *s <= '9') || *s == ',')
  1378. break;
  1379. idx = simple_strtoul(s, NULL, 10);
  1380. *s = 0;
  1381. __add_preferred_console(buf, idx, options, brl_options);
  1382. console_set_on_cmdline = 1;
  1383. return 1;
  1384. }
  1385. __setup("console=", console_setup);
  1386. /**
  1387. * add_preferred_console - add a device to the list of preferred consoles.
  1388. * @name: device name
  1389. * @idx: device index
  1390. * @options: options for this console
  1391. *
  1392. * The last preferred console added will be used for kernel messages
  1393. * and stdin/out/err for init. Normally this is used by console_setup
  1394. * above to handle user-supplied console arguments; however it can also
  1395. * be used by arch-specific code either to override the user or more
  1396. * commonly to provide a default console (ie from PROM variables) when
  1397. * the user has not supplied one.
  1398. */
  1399. int add_preferred_console(char *name, int idx, char *options)
  1400. {
  1401. return __add_preferred_console(name, idx, options, NULL);
  1402. }
  1403. int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
  1404. {
  1405. struct console_cmdline *c;
  1406. int i;
  1407. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  1408. if (strcmp(console_cmdline[i].name, name) == 0 &&
  1409. console_cmdline[i].index == idx) {
  1410. c = &console_cmdline[i];
  1411. strlcpy(c->name, name_new, sizeof(c->name));
  1412. c->name[sizeof(c->name) - 1] = 0;
  1413. c->options = options;
  1414. c->index = idx_new;
  1415. return i;
  1416. }
  1417. /* not found */
  1418. return -1;
  1419. }
  1420. bool console_suspend_enabled = 1;
  1421. EXPORT_SYMBOL(console_suspend_enabled);
  1422. static int __init console_suspend_disable(char *str)
  1423. {
  1424. console_suspend_enabled = 0;
  1425. return 1;
  1426. }
  1427. __setup("no_console_suspend", console_suspend_disable);
  1428. module_param_named(console_suspend, console_suspend_enabled,
  1429. bool, S_IRUGO | S_IWUSR);
  1430. MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
  1431. " and hibernate operations");
  1432. /**
  1433. * suspend_console - suspend the console subsystem
  1434. *
  1435. * This disables printk() while we go into suspend states
  1436. */
  1437. void suspend_console(void)
  1438. {
  1439. if (!console_suspend_enabled)
  1440. return;
  1441. printk("Suspending console(s) (use no_console_suspend to debug)\n");
  1442. console_lock();
  1443. console_suspended = 1;
  1444. up(&console_sem);
  1445. }
  1446. void resume_console(void)
  1447. {
  1448. if (!console_suspend_enabled)
  1449. return;
  1450. down(&console_sem);
  1451. console_suspended = 0;
  1452. console_unlock();
  1453. }
  1454. /**
  1455. * console_cpu_notify - print deferred console messages after CPU hotplug
  1456. * @self: notifier struct
  1457. * @action: CPU hotplug event
  1458. * @hcpu: unused
  1459. *
  1460. * If printk() is called from a CPU that is not online yet, the messages
  1461. * will be spooled but will not show up on the console. This function is
  1462. * called when a new CPU comes online (or fails to come up), and ensures
  1463. * that any such output gets printed.
  1464. */
  1465. static int __cpuinit console_cpu_notify(struct notifier_block *self,
  1466. unsigned long action, void *hcpu)
  1467. {
  1468. switch (action) {
  1469. case CPU_ONLINE:
  1470. case CPU_DEAD:
  1471. case CPU_DYING:
  1472. case CPU_DOWN_FAILED:
  1473. case CPU_UP_CANCELED:
  1474. console_lock();
  1475. console_unlock();
  1476. }
  1477. return NOTIFY_OK;
  1478. }
  1479. /**
  1480. * console_lock - lock the console system for exclusive use.
  1481. *
  1482. * Acquires a lock which guarantees that the caller has
  1483. * exclusive access to the console system and the console_drivers list.
  1484. *
  1485. * Can sleep, returns nothing.
  1486. */
  1487. void console_lock(void)
  1488. {
  1489. BUG_ON(in_interrupt());
  1490. down(&console_sem);
  1491. if (console_suspended)
  1492. return;
  1493. console_locked = 1;
  1494. console_may_schedule = 1;
  1495. }
  1496. EXPORT_SYMBOL(console_lock);
  1497. /**
  1498. * console_trylock - try to lock the console system for exclusive use.
  1499. *
  1500. * Tried to acquire a lock which guarantees that the caller has
  1501. * exclusive access to the console system and the console_drivers list.
  1502. *
  1503. * returns 1 on success, and 0 on failure to acquire the lock.
  1504. */
  1505. int console_trylock(void)
  1506. {
  1507. if (down_trylock(&console_sem))
  1508. return 0;
  1509. if (console_suspended) {
  1510. up(&console_sem);
  1511. return 0;
  1512. }
  1513. console_locked = 1;
  1514. console_may_schedule = 0;
  1515. return 1;
  1516. }
  1517. EXPORT_SYMBOL(console_trylock);
  1518. int is_console_locked(void)
  1519. {
  1520. return console_locked;
  1521. }
  1522. /*
  1523. * Delayed printk version, for scheduler-internal messages:
  1524. */
  1525. #define PRINTK_BUF_SIZE 512
  1526. #define PRINTK_PENDING_WAKEUP 0x01
  1527. #define PRINTK_PENDING_SCHED 0x02
  1528. static DEFINE_PER_CPU(int, printk_pending);
  1529. static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
  1530. void printk_tick(void)
  1531. {
  1532. if (__this_cpu_read(printk_pending)) {
  1533. int pending = __this_cpu_xchg(printk_pending, 0);
  1534. if (pending & PRINTK_PENDING_SCHED) {
  1535. char *buf = __get_cpu_var(printk_sched_buf);
  1536. printk(KERN_WARNING "[sched_delayed] %s", buf);
  1537. }
  1538. if (pending & PRINTK_PENDING_WAKEUP)
  1539. wake_up_interruptible(&log_wait);
  1540. }
  1541. }
  1542. int printk_needs_cpu(int cpu)
  1543. {
  1544. if (cpu_is_offline(cpu))
  1545. printk_tick();
  1546. return __this_cpu_read(printk_pending);
  1547. }
  1548. void wake_up_klogd(void)
  1549. {
  1550. if (waitqueue_active(&log_wait))
  1551. this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
  1552. }
  1553. /* the next printk record to write to the console */
  1554. static u64 console_seq;
  1555. static u32 console_idx;
  1556. /**
  1557. * console_unlock - unlock the console system
  1558. *
  1559. * Releases the console_lock which the caller holds on the console system
  1560. * and the console driver list.
  1561. *
  1562. * While the console_lock was held, console output may have been buffered
  1563. * by printk(). If this is the case, console_unlock(); emits
  1564. * the output prior to releasing the lock.
  1565. *
  1566. * If there is output waiting, we wake /dev/kmsg and syslog() users.
  1567. *
  1568. * console_unlock(); may be called from any context.
  1569. */
  1570. void console_unlock(void)
  1571. {
  1572. static u64 seen_seq;
  1573. unsigned long flags;
  1574. bool wake_klogd = false;
  1575. bool retry;
  1576. if (console_suspended) {
  1577. up(&console_sem);
  1578. return;
  1579. }
  1580. console_may_schedule = 0;
  1581. again:
  1582. for (;;) {
  1583. struct log *msg;
  1584. static char text[LOG_LINE_MAX];
  1585. size_t len;
  1586. int level;
  1587. raw_spin_lock_irqsave(&logbuf_lock, flags);
  1588. if (seen_seq != log_next_seq) {
  1589. wake_klogd = true;
  1590. seen_seq = log_next_seq;
  1591. }
  1592. if (console_seq < log_first_seq) {
  1593. /* messages are gone, move to first one */
  1594. console_seq = log_first_seq;
  1595. console_idx = log_first_idx;
  1596. }
  1597. if (console_seq == log_next_seq)
  1598. break;
  1599. msg = log_from_idx(console_idx);
  1600. level = msg->level & 7;
  1601. len = msg_print_text(msg, false, text, sizeof(text));
  1602. console_idx = log_next(console_idx);
  1603. console_seq++;
  1604. raw_spin_unlock(&logbuf_lock);
  1605. stop_critical_timings(); /* don't trace print latency */
  1606. call_console_drivers(level, text, len);
  1607. start_critical_timings();
  1608. local_irq_restore(flags);
  1609. }
  1610. console_locked = 0;
  1611. /* Release the exclusive_console once it is used */
  1612. if (unlikely(exclusive_console))
  1613. exclusive_console = NULL;
  1614. raw_spin_unlock(&logbuf_lock);
  1615. up(&console_sem);
  1616. /*
  1617. * Someone could have filled up the buffer again, so re-check if there's
  1618. * something to flush. In case we cannot trylock the console_sem again,
  1619. * there's a new owner and the console_unlock() from them will do the
  1620. * flush, no worries.
  1621. */
  1622. raw_spin_lock(&logbuf_lock);
  1623. retry = console_seq != log_next_seq;
  1624. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  1625. if (retry && console_trylock())
  1626. goto again;
  1627. if (wake_klogd)
  1628. wake_up_klogd();
  1629. }
  1630. EXPORT_SYMBOL(console_unlock);
  1631. /**
  1632. * console_conditional_schedule - yield the CPU if required
  1633. *
  1634. * If the console code is currently allowed to sleep, and
  1635. * if this CPU should yield the CPU to another task, do
  1636. * so here.
  1637. *
  1638. * Must be called within console_lock();.
  1639. */
  1640. void __sched console_conditional_schedule(void)
  1641. {
  1642. if (console_may_schedule)
  1643. cond_resched();
  1644. }
  1645. EXPORT_SYMBOL(console_conditional_schedule);
  1646. void console_unblank(void)
  1647. {
  1648. struct console *c;
  1649. /*
  1650. * console_unblank can no longer be called in interrupt context unless
  1651. * oops_in_progress is set to 1..
  1652. */
  1653. if (oops_in_progress) {
  1654. if (down_trylock(&console_sem) != 0)
  1655. return;
  1656. } else
  1657. console_lock();
  1658. console_locked = 1;
  1659. console_may_schedule = 0;
  1660. for_each_console(c)
  1661. if ((c->flags & CON_ENABLED) && c->unblank)
  1662. c->unblank();
  1663. console_unlock();
  1664. }
  1665. /*
  1666. * Return the console tty driver structure and its associated index
  1667. */
  1668. struct tty_driver *console_device(int *index)
  1669. {
  1670. struct console *c;
  1671. struct tty_driver *driver = NULL;
  1672. console_lock();
  1673. for_each_console(c) {
  1674. if (!c->device)
  1675. continue;
  1676. driver = c->device(c, index);
  1677. if (driver)
  1678. break;
  1679. }
  1680. console_unlock();
  1681. return driver;
  1682. }
  1683. /*
  1684. * Prevent further output on the passed console device so that (for example)
  1685. * serial drivers can disable console output before suspending a port, and can
  1686. * re-enable output afterwards.
  1687. */
  1688. void console_stop(struct console *console)
  1689. {
  1690. console_lock();
  1691. console->flags &= ~CON_ENABLED;
  1692. console_unlock();
  1693. }
  1694. EXPORT_SYMBOL(console_stop);
  1695. void console_start(struct console *console)
  1696. {
  1697. console_lock();
  1698. console->flags |= CON_ENABLED;
  1699. console_unlock();
  1700. }
  1701. EXPORT_SYMBOL(console_start);
  1702. static int __read_mostly keep_bootcon;
  1703. static int __init keep_bootcon_setup(char *str)
  1704. {
  1705. keep_bootcon = 1;
  1706. printk(KERN_INFO "debug: skip boot console de-registration.\n");
  1707. return 0;
  1708. }
  1709. early_param("keep_bootcon", keep_bootcon_setup);
  1710. /*
  1711. * The console driver calls this routine during kernel initialization
  1712. * to register the console printing procedure with printk() and to
  1713. * print any messages that were printed by the kernel before the
  1714. * console driver was initialized.
  1715. *
  1716. * This can happen pretty early during the boot process (because of
  1717. * early_printk) - sometimes before setup_arch() completes - be careful
  1718. * of what kernel features are used - they may not be initialised yet.
  1719. *
  1720. * There are two types of consoles - bootconsoles (early_printk) and
  1721. * "real" consoles (everything which is not a bootconsole) which are
  1722. * handled differently.
  1723. * - Any number of bootconsoles can be registered at any time.
  1724. * - As soon as a "real" console is registered, all bootconsoles
  1725. * will be unregistered automatically.
  1726. * - Once a "real" console is registered, any attempt to register a
  1727. * bootconsoles will be rejected
  1728. */
  1729. void register_console(struct console *newcon)
  1730. {
  1731. int i;
  1732. unsigned long flags;
  1733. struct console *bcon = NULL;
  1734. /*
  1735. * before we register a new CON_BOOT console, make sure we don't
  1736. * already have a valid console
  1737. */
  1738. if (console_drivers && newcon->flags & CON_BOOT) {
  1739. /* find the last or real console */
  1740. for_each_console(bcon) {
  1741. if (!(bcon->flags & CON_BOOT)) {
  1742. printk(KERN_INFO "Too late to register bootconsole %s%d\n",
  1743. newcon->name, newcon->index);
  1744. return;
  1745. }
  1746. }
  1747. }
  1748. if (console_drivers && console_drivers->flags & CON_BOOT)
  1749. bcon = console_drivers;
  1750. if (preferred_console < 0 || bcon || !console_drivers)
  1751. preferred_console = selected_console;
  1752. if (newcon->early_setup)
  1753. newcon->early_setup();
  1754. /*
  1755. * See if we want to use this console driver. If we
  1756. * didn't select a console we take the first one
  1757. * that registers here.
  1758. */
  1759. if (preferred_console < 0) {
  1760. if (newcon->index < 0)
  1761. newcon->index = 0;
  1762. if (newcon->setup == NULL ||
  1763. newcon->setup(newcon, NULL) == 0) {
  1764. newcon->flags |= CON_ENABLED;
  1765. if (newcon->device) {
  1766. newcon->flags |= CON_CONSDEV;
  1767. preferred_console = 0;
  1768. }
  1769. }
  1770. }
  1771. /*
  1772. * See if this console matches one we selected on
  1773. * the command line.
  1774. */
  1775. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  1776. i++) {
  1777. if (strcmp(console_cmdline[i].name, newcon->name) != 0)
  1778. continue;
  1779. if (newcon->index >= 0 &&
  1780. newcon->index != console_cmdline[i].index)
  1781. continue;
  1782. if (newcon->index < 0)
  1783. newcon->index = console_cmdline[i].index;
  1784. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1785. if (console_cmdline[i].brl_options) {
  1786. newcon->flags |= CON_BRL;
  1787. braille_register_console(newcon,
  1788. console_cmdline[i].index,
  1789. console_cmdline[i].options,
  1790. console_cmdline[i].brl_options);
  1791. return;
  1792. }
  1793. #endif
  1794. if (newcon->setup &&
  1795. newcon->setup(newcon, console_cmdline[i].options) != 0)
  1796. break;
  1797. newcon->flags |= CON_ENABLED;
  1798. newcon->index = console_cmdline[i].index;
  1799. if (i == selected_console) {
  1800. newcon->flags |= CON_CONSDEV;
  1801. preferred_console = selected_console;
  1802. }
  1803. break;
  1804. }
  1805. if (!(newcon->flags & CON_ENABLED))
  1806. return;
  1807. /*
  1808. * If we have a bootconsole, and are switching to a real console,
  1809. * don't print everything out again, since when the boot console, and
  1810. * the real console are the same physical device, it's annoying to
  1811. * see the beginning boot messages twice
  1812. */
  1813. if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
  1814. newcon->flags &= ~CON_PRINTBUFFER;
  1815. /*
  1816. * Put this console in the list - keep the
  1817. * preferred driver at the head of the list.
  1818. */
  1819. console_lock();
  1820. if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
  1821. newcon->next = console_drivers;
  1822. console_drivers = newcon;
  1823. if (newcon->next)
  1824. newcon->next->flags &= ~CON_CONSDEV;
  1825. } else {
  1826. newcon->next = console_drivers->next;
  1827. console_drivers->next = newcon;
  1828. }
  1829. if (newcon->flags & CON_PRINTBUFFER) {
  1830. /*
  1831. * console_unlock(); will print out the buffered messages
  1832. * for us.
  1833. */
  1834. raw_spin_lock_irqsave(&logbuf_lock, flags);
  1835. console_seq = syslog_seq;
  1836. console_idx = syslog_idx;
  1837. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  1838. /*
  1839. * We're about to replay the log buffer. Only do this to the
  1840. * just-registered console to avoid excessive message spam to
  1841. * the already-registered consoles.
  1842. */
  1843. exclusive_console = newcon;
  1844. }
  1845. console_unlock();
  1846. console_sysfs_notify();
  1847. /*
  1848. * By unregistering the bootconsoles after we enable the real console
  1849. * we get the "console xxx enabled" message on all the consoles -
  1850. * boot consoles, real consoles, etc - this is to ensure that end
  1851. * users know there might be something in the kernel's log buffer that
  1852. * went to the bootconsole (that they do not see on the real console)
  1853. */
  1854. if (bcon &&
  1855. ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
  1856. !keep_bootcon) {
  1857. /* we need to iterate through twice, to make sure we print
  1858. * everything out, before we unregister the console(s)
  1859. */
  1860. printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
  1861. newcon->name, newcon->index);
  1862. for_each_console(bcon)
  1863. if (bcon->flags & CON_BOOT)
  1864. unregister_console(bcon);
  1865. } else {
  1866. printk(KERN_INFO "%sconsole [%s%d] enabled\n",
  1867. (newcon->flags & CON_BOOT) ? "boot" : "" ,
  1868. newcon->name, newcon->index);
  1869. }
  1870. }
  1871. EXPORT_SYMBOL(register_console);
  1872. int unregister_console(struct console *console)
  1873. {
  1874. struct console *a, *b;
  1875. int res = 1;
  1876. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1877. if (console->flags & CON_BRL)
  1878. return braille_unregister_console(console);
  1879. #endif
  1880. console_lock();
  1881. if (console_drivers == console) {
  1882. console_drivers=console->next;
  1883. res = 0;
  1884. } else if (console_drivers) {
  1885. for (a=console_drivers->next, b=console_drivers ;
  1886. a; b=a, a=b->next) {
  1887. if (a == console) {
  1888. b->next = a->next;
  1889. res = 0;
  1890. break;
  1891. }
  1892. }
  1893. }
  1894. /*
  1895. * If this isn't the last console and it has CON_CONSDEV set, we
  1896. * need to set it on the next preferred console.
  1897. */
  1898. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  1899. console_drivers->flags |= CON_CONSDEV;
  1900. console_unlock();
  1901. console_sysfs_notify();
  1902. return res;
  1903. }
  1904. EXPORT_SYMBOL(unregister_console);
  1905. static int __init printk_late_init(void)
  1906. {
  1907. struct console *con;
  1908. for_each_console(con) {
  1909. if (!keep_bootcon && con->flags & CON_BOOT) {
  1910. printk(KERN_INFO "turn off boot console %s%d\n",
  1911. con->name, con->index);
  1912. unregister_console(con);
  1913. }
  1914. }
  1915. hotcpu_notifier(console_cpu_notify, 0);
  1916. return 0;
  1917. }
  1918. late_initcall(printk_late_init);
  1919. #if defined CONFIG_PRINTK
  1920. int printk_sched(const char *fmt, ...)
  1921. {
  1922. unsigned long flags;
  1923. va_list args;
  1924. char *buf;
  1925. int r;
  1926. local_irq_save(flags);
  1927. buf = __get_cpu_var(printk_sched_buf);
  1928. va_start(args, fmt);
  1929. r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
  1930. va_end(args);
  1931. __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
  1932. local_irq_restore(flags);
  1933. return r;
  1934. }
  1935. /*
  1936. * printk rate limiting, lifted from the networking subsystem.
  1937. *
  1938. * This enforces a rate limit: not more than 10 kernel messages
  1939. * every 5s to make a denial-of-service attack impossible.
  1940. */
  1941. DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
  1942. int __printk_ratelimit(const char *func)
  1943. {
  1944. return ___ratelimit(&printk_ratelimit_state, func);
  1945. }
  1946. EXPORT_SYMBOL(__printk_ratelimit);
  1947. /**
  1948. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  1949. * @caller_jiffies: pointer to caller's state
  1950. * @interval_msecs: minimum interval between prints
  1951. *
  1952. * printk_timed_ratelimit() returns true if more than @interval_msecs
  1953. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  1954. * returned true.
  1955. */
  1956. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  1957. unsigned int interval_msecs)
  1958. {
  1959. if (*caller_jiffies == 0
  1960. || !time_in_range(jiffies, *caller_jiffies,
  1961. *caller_jiffies
  1962. + msecs_to_jiffies(interval_msecs))) {
  1963. *caller_jiffies = jiffies;
  1964. return true;
  1965. }
  1966. return false;
  1967. }
  1968. EXPORT_SYMBOL(printk_timed_ratelimit);
  1969. static DEFINE_SPINLOCK(dump_list_lock);
  1970. static LIST_HEAD(dump_list);
  1971. /**
  1972. * kmsg_dump_register - register a kernel log dumper.
  1973. * @dumper: pointer to the kmsg_dumper structure
  1974. *
  1975. * Adds a kernel log dumper to the system. The dump callback in the
  1976. * structure will be called when the kernel oopses or panics and must be
  1977. * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
  1978. */
  1979. int kmsg_dump_register(struct kmsg_dumper *dumper)
  1980. {
  1981. unsigned long flags;
  1982. int err = -EBUSY;
  1983. /* The dump callback needs to be set */
  1984. if (!dumper->dump)
  1985. return -EINVAL;
  1986. spin_lock_irqsave(&dump_list_lock, flags);
  1987. /* Don't allow registering multiple times */
  1988. if (!dumper->registered) {
  1989. dumper->registered = 1;
  1990. list_add_tail_rcu(&dumper->list, &dump_list);
  1991. err = 0;
  1992. }
  1993. spin_unlock_irqrestore(&dump_list_lock, flags);
  1994. return err;
  1995. }
  1996. EXPORT_SYMBOL_GPL(kmsg_dump_register);
  1997. /**
  1998. * kmsg_dump_unregister - unregister a kmsg dumper.
  1999. * @dumper: pointer to the kmsg_dumper structure
  2000. *
  2001. * Removes a dump device from the system. Returns zero on success and
  2002. * %-EINVAL otherwise.
  2003. */
  2004. int kmsg_dump_unregister(struct kmsg_dumper *dumper)
  2005. {
  2006. unsigned long flags;
  2007. int err = -EINVAL;
  2008. spin_lock_irqsave(&dump_list_lock, flags);
  2009. if (dumper->registered) {
  2010. dumper->registered = 0;
  2011. list_del_rcu(&dumper->list);
  2012. err = 0;
  2013. }
  2014. spin_unlock_irqrestore(&dump_list_lock, flags);
  2015. synchronize_rcu();
  2016. return err;
  2017. }
  2018. EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
  2019. static bool always_kmsg_dump;
  2020. module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
  2021. /**
  2022. * kmsg_dump - dump kernel log to kernel message dumpers.
  2023. * @reason: the reason (oops, panic etc) for dumping
  2024. *
  2025. * Call each of the registered dumper's dump() callback, which can
  2026. * retrieve the kmsg records with kmsg_dump_get_line() or
  2027. * kmsg_dump_get_buffer().
  2028. */
  2029. void kmsg_dump(enum kmsg_dump_reason reason)
  2030. {
  2031. struct kmsg_dumper *dumper;
  2032. unsigned long flags;
  2033. if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
  2034. return;
  2035. rcu_read_lock();
  2036. list_for_each_entry_rcu(dumper, &dump_list, list) {
  2037. if (dumper->max_reason && reason > dumper->max_reason)
  2038. continue;
  2039. /* initialize iterator with data about the stored records */
  2040. dumper->active = true;
  2041. raw_spin_lock_irqsave(&logbuf_lock, flags);
  2042. dumper->cur_seq = clear_seq;
  2043. dumper->cur_idx = clear_idx;
  2044. dumper->next_seq = log_next_seq;
  2045. dumper->next_idx = log_next_idx;
  2046. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2047. /* invoke dumper which will iterate over records */
  2048. dumper->dump(dumper, reason);
  2049. /* reset iterator */
  2050. dumper->active = false;
  2051. }
  2052. rcu_read_unlock();
  2053. }
  2054. /**
  2055. * kmsg_dump_get_line - retrieve one kmsg log line
  2056. * @dumper: registered kmsg dumper
  2057. * @syslog: include the "<4>" prefixes
  2058. * @line: buffer to copy the line to
  2059. * @size: maximum size of the buffer
  2060. * @len: length of line placed into buffer
  2061. *
  2062. * Start at the beginning of the kmsg buffer, with the oldest kmsg
  2063. * record, and copy one record into the provided buffer.
  2064. *
  2065. * Consecutive calls will return the next available record moving
  2066. * towards the end of the buffer with the youngest messages.
  2067. *
  2068. * A return value of FALSE indicates that there are no more records to
  2069. * read.
  2070. */
  2071. bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
  2072. char *line, size_t size, size_t *len)
  2073. {
  2074. unsigned long flags;
  2075. struct log *msg;
  2076. size_t l = 0;
  2077. bool ret = false;
  2078. if (!dumper->active)
  2079. goto out;
  2080. raw_spin_lock_irqsave(&logbuf_lock, flags);
  2081. if (dumper->cur_seq < log_first_seq) {
  2082. /* messages are gone, move to first available one */
  2083. dumper->cur_seq = log_first_seq;
  2084. dumper->cur_idx = log_first_idx;
  2085. }
  2086. /* last entry */
  2087. if (dumper->cur_seq >= log_next_seq) {
  2088. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2089. goto out;
  2090. }
  2091. msg = log_from_idx(dumper->cur_idx);
  2092. l = msg_print_text(msg, syslog,
  2093. line, size);
  2094. dumper->cur_idx = log_next(dumper->cur_idx);
  2095. dumper->cur_seq++;
  2096. ret = true;
  2097. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2098. out:
  2099. if (len)
  2100. *len = l;
  2101. return ret;
  2102. }
  2103. EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
  2104. /**
  2105. * kmsg_dump_get_buffer - copy kmsg log lines
  2106. * @dumper: registered kmsg dumper
  2107. * @syslog: include the "<4>" prefixes
  2108. * @line: buffer to copy the line to
  2109. * @size: maximum size of the buffer
  2110. * @len: length of line placed into buffer
  2111. *
  2112. * Start at the end of the kmsg buffer and fill the provided buffer
  2113. * with as many of the the *youngest* kmsg records that fit into it.
  2114. * If the buffer is large enough, all available kmsg records will be
  2115. * copied with a single call.
  2116. *
  2117. * Consecutive calls will fill the buffer with the next block of
  2118. * available older records, not including the earlier retrieved ones.
  2119. *
  2120. * A return value of FALSE indicates that there are no more records to
  2121. * read.
  2122. */
  2123. bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
  2124. char *buf, size_t size, size_t *len)
  2125. {
  2126. unsigned long flags;
  2127. u64 seq;
  2128. u32 idx;
  2129. u64 next_seq;
  2130. u32 next_idx;
  2131. size_t l = 0;
  2132. bool ret = false;
  2133. if (!dumper->active)
  2134. goto out;
  2135. raw_spin_lock_irqsave(&logbuf_lock, flags);
  2136. if (dumper->cur_seq < log_first_seq) {
  2137. /* messages are gone, move to first available one */
  2138. dumper->cur_seq = log_first_seq;
  2139. dumper->cur_idx = log_first_idx;
  2140. }
  2141. /* last entry */
  2142. if (dumper->cur_seq >= dumper->next_seq) {
  2143. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2144. goto out;
  2145. }
  2146. /* calculate length of entire buffer */
  2147. seq = dumper->cur_seq;
  2148. idx = dumper->cur_idx;
  2149. while (seq < dumper->next_seq) {
  2150. struct log *msg = log_from_idx(idx);
  2151. l += msg_print_text(msg, true, NULL, 0);
  2152. idx = log_next(idx);
  2153. seq++;
  2154. }
  2155. /* move first record forward until length fits into the buffer */
  2156. seq = dumper->cur_seq;
  2157. idx = dumper->cur_idx;
  2158. while (l > size && seq < dumper->next_seq) {
  2159. struct log *msg = log_from_idx(idx);
  2160. l -= msg_print_text(msg, true, NULL, 0);
  2161. idx = log_next(idx);
  2162. seq++;
  2163. }
  2164. /* last message in next interation */
  2165. next_seq = seq;
  2166. next_idx = idx;
  2167. l = 0;
  2168. while (seq < dumper->next_seq) {
  2169. struct log *msg = log_from_idx(idx);
  2170. l += msg_print_text(msg, syslog,
  2171. buf + l, size - l);
  2172. idx = log_next(idx);
  2173. seq++;
  2174. }
  2175. dumper->next_seq = next_seq;
  2176. dumper->next_idx = next_idx;
  2177. ret = true;
  2178. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2179. out:
  2180. if (len)
  2181. *len = l;
  2182. return ret;
  2183. }
  2184. EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
  2185. /**
  2186. * kmsg_dump_rewind - reset the interator
  2187. * @dumper: registered kmsg dumper
  2188. *
  2189. * Reset the dumper's iterator so that kmsg_dump_get_line() and
  2190. * kmsg_dump_get_buffer() can be called again and used multiple
  2191. * times within the same dumper.dump() callback.
  2192. */
  2193. void kmsg_dump_rewind(struct kmsg_dumper *dumper)
  2194. {
  2195. unsigned long flags;
  2196. raw_spin_lock_irqsave(&logbuf_lock, flags);
  2197. dumper->cur_seq = clear_seq;
  2198. dumper->cur_idx = clear_idx;
  2199. dumper->next_seq = log_next_seq;
  2200. dumper->next_idx = log_next_idx;
  2201. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2202. }
  2203. EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
  2204. #endif