printk.c 65 KB

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