printk.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  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_irq(&logbuf_lock);
  382. while (user->seq == log_next_seq) {
  383. if (file->f_flags & O_NONBLOCK) {
  384. ret = -EAGAIN;
  385. raw_spin_unlock_irq(&logbuf_lock);
  386. goto out;
  387. }
  388. raw_spin_unlock_irq(&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_irq(&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_irq(&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 >= 127 || c == '\\')
  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 >= 127 || c == '\\') {
  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_irq(&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_irq(&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_irq(&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_irq(&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_irq(&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_irq(&logbuf_lock);
  521. user->idx = log_first_idx;
  522. user->seq = log_first_seq;
  523. raw_spin_unlock_irq(&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. unsigned int prefix = (msg->facility << 3) | msg->level;
  705. if (syslog) {
  706. if (buf) {
  707. len += sprintf(buf, "<%u>", prefix);
  708. } else {
  709. len += 3;
  710. if (prefix > 999)
  711. len += 3;
  712. else if (prefix > 99)
  713. len += 2;
  714. else if (prefix > 9)
  715. len++;
  716. }
  717. }
  718. len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
  719. return len;
  720. }
  721. static size_t msg_print_text(const struct log *msg, bool syslog,
  722. char *buf, size_t size)
  723. {
  724. const char *text = log_text(msg);
  725. size_t text_size = msg->text_len;
  726. size_t len = 0;
  727. do {
  728. const char *next = memchr(text, '\n', text_size);
  729. size_t text_len;
  730. if (next) {
  731. text_len = next - text;
  732. next++;
  733. text_size -= next - text;
  734. } else {
  735. text_len = text_size;
  736. }
  737. if (buf) {
  738. if (print_prefix(msg, syslog, NULL) +
  739. text_len + 1>= size - len)
  740. break;
  741. len += print_prefix(msg, syslog, buf + len);
  742. memcpy(buf + len, text, text_len);
  743. len += text_len;
  744. buf[len++] = '\n';
  745. } else {
  746. /* SYSLOG_ACTION_* buffer size only calculation */
  747. len += print_prefix(msg, syslog, NULL);
  748. len += text_len + 1;
  749. }
  750. text = next;
  751. } while (text);
  752. return len;
  753. }
  754. static int syslog_print(char __user *buf, int size)
  755. {
  756. char *text;
  757. struct log *msg;
  758. int len = 0;
  759. text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
  760. if (!text)
  761. return -ENOMEM;
  762. while (size > 0) {
  763. size_t n;
  764. raw_spin_lock_irq(&logbuf_lock);
  765. if (syslog_seq < log_first_seq) {
  766. /* messages are gone, move to first one */
  767. syslog_seq = log_first_seq;
  768. syslog_idx = log_first_idx;
  769. }
  770. if (syslog_seq == log_next_seq) {
  771. raw_spin_unlock_irq(&logbuf_lock);
  772. break;
  773. }
  774. msg = log_from_idx(syslog_idx);
  775. n = msg_print_text(msg, true, text, LOG_LINE_MAX);
  776. if (n <= size) {
  777. syslog_idx = log_next(syslog_idx);
  778. syslog_seq++;
  779. } else
  780. n = 0;
  781. raw_spin_unlock_irq(&logbuf_lock);
  782. if (!n)
  783. break;
  784. len += n;
  785. size -= n;
  786. buf += n;
  787. n = copy_to_user(buf - n, text, n);
  788. if (n) {
  789. len -= n;
  790. if (!len)
  791. len = -EFAULT;
  792. break;
  793. }
  794. }
  795. kfree(text);
  796. return len;
  797. }
  798. static int syslog_print_all(char __user *buf, int size, bool clear)
  799. {
  800. char *text;
  801. int len = 0;
  802. text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
  803. if (!text)
  804. return -ENOMEM;
  805. raw_spin_lock_irq(&logbuf_lock);
  806. if (buf) {
  807. u64 next_seq;
  808. u64 seq;
  809. u32 idx;
  810. if (clear_seq < log_first_seq) {
  811. /* messages are gone, move to first available one */
  812. clear_seq = log_first_seq;
  813. clear_idx = log_first_idx;
  814. }
  815. /*
  816. * Find first record that fits, including all following records,
  817. * into the user-provided buffer for this dump.
  818. */
  819. seq = clear_seq;
  820. idx = clear_idx;
  821. while (seq < log_next_seq) {
  822. struct log *msg = log_from_idx(idx);
  823. len += msg_print_text(msg, true, NULL, 0);
  824. idx = log_next(idx);
  825. seq++;
  826. }
  827. /* move first record forward until length fits into the buffer */
  828. seq = clear_seq;
  829. idx = clear_idx;
  830. while (len > size && seq < log_next_seq) {
  831. struct log *msg = log_from_idx(idx);
  832. len -= msg_print_text(msg, true, NULL, 0);
  833. idx = log_next(idx);
  834. seq++;
  835. }
  836. /* last message fitting into this dump */
  837. next_seq = log_next_seq;
  838. len = 0;
  839. while (len >= 0 && seq < next_seq) {
  840. struct log *msg = log_from_idx(idx);
  841. int textlen;
  842. textlen = msg_print_text(msg, true, text, LOG_LINE_MAX);
  843. if (textlen < 0) {
  844. len = textlen;
  845. break;
  846. }
  847. idx = log_next(idx);
  848. seq++;
  849. raw_spin_unlock_irq(&logbuf_lock);
  850. if (copy_to_user(buf + len, text, textlen))
  851. len = -EFAULT;
  852. else
  853. len += textlen;
  854. raw_spin_lock_irq(&logbuf_lock);
  855. if (seq < log_first_seq) {
  856. /* messages are gone, move to next one */
  857. seq = log_first_seq;
  858. idx = log_first_idx;
  859. }
  860. }
  861. }
  862. if (clear) {
  863. clear_seq = log_next_seq;
  864. clear_idx = log_next_idx;
  865. }
  866. raw_spin_unlock_irq(&logbuf_lock);
  867. kfree(text);
  868. return len;
  869. }
  870. int do_syslog(int type, char __user *buf, int len, bool from_file)
  871. {
  872. bool clear = false;
  873. static int saved_console_loglevel = -1;
  874. static DEFINE_MUTEX(syslog_mutex);
  875. int error;
  876. error = check_syslog_permissions(type, from_file);
  877. if (error)
  878. goto out;
  879. error = security_syslog(type);
  880. if (error)
  881. return error;
  882. switch (type) {
  883. case SYSLOG_ACTION_CLOSE: /* Close log */
  884. break;
  885. case SYSLOG_ACTION_OPEN: /* Open log */
  886. break;
  887. case SYSLOG_ACTION_READ: /* Read from log */
  888. error = -EINVAL;
  889. if (!buf || len < 0)
  890. goto out;
  891. error = 0;
  892. if (!len)
  893. goto out;
  894. if (!access_ok(VERIFY_WRITE, buf, len)) {
  895. error = -EFAULT;
  896. goto out;
  897. }
  898. error = mutex_lock_interruptible(&syslog_mutex);
  899. if (error)
  900. goto out;
  901. error = wait_event_interruptible(log_wait,
  902. syslog_seq != log_next_seq);
  903. if (error) {
  904. mutex_unlock(&syslog_mutex);
  905. goto out;
  906. }
  907. error = syslog_print(buf, len);
  908. mutex_unlock(&syslog_mutex);
  909. break;
  910. /* Read/clear last kernel messages */
  911. case SYSLOG_ACTION_READ_CLEAR:
  912. clear = true;
  913. /* FALL THRU */
  914. /* Read last kernel messages */
  915. case SYSLOG_ACTION_READ_ALL:
  916. error = -EINVAL;
  917. if (!buf || len < 0)
  918. goto out;
  919. error = 0;
  920. if (!len)
  921. goto out;
  922. if (!access_ok(VERIFY_WRITE, buf, len)) {
  923. error = -EFAULT;
  924. goto out;
  925. }
  926. error = syslog_print_all(buf, len, clear);
  927. break;
  928. /* Clear ring buffer */
  929. case SYSLOG_ACTION_CLEAR:
  930. syslog_print_all(NULL, 0, true);
  931. break;
  932. /* Disable logging to console */
  933. case SYSLOG_ACTION_CONSOLE_OFF:
  934. if (saved_console_loglevel == -1)
  935. saved_console_loglevel = console_loglevel;
  936. console_loglevel = minimum_console_loglevel;
  937. break;
  938. /* Enable logging to console */
  939. case SYSLOG_ACTION_CONSOLE_ON:
  940. if (saved_console_loglevel != -1) {
  941. console_loglevel = saved_console_loglevel;
  942. saved_console_loglevel = -1;
  943. }
  944. break;
  945. /* Set level of messages printed to console */
  946. case SYSLOG_ACTION_CONSOLE_LEVEL:
  947. error = -EINVAL;
  948. if (len < 1 || len > 8)
  949. goto out;
  950. if (len < minimum_console_loglevel)
  951. len = minimum_console_loglevel;
  952. console_loglevel = len;
  953. /* Implicitly re-enable logging to console */
  954. saved_console_loglevel = -1;
  955. error = 0;
  956. break;
  957. /* Number of chars in the log buffer */
  958. case SYSLOG_ACTION_SIZE_UNREAD:
  959. raw_spin_lock_irq(&logbuf_lock);
  960. if (syslog_seq < log_first_seq) {
  961. /* messages are gone, move to first one */
  962. syslog_seq = log_first_seq;
  963. syslog_idx = log_first_idx;
  964. }
  965. if (from_file) {
  966. /*
  967. * Short-cut for poll(/"proc/kmsg") which simply checks
  968. * for pending data, not the size; return the count of
  969. * records, not the length.
  970. */
  971. error = log_next_idx - syslog_idx;
  972. } else {
  973. u64 seq;
  974. u32 idx;
  975. error = 0;
  976. seq = syslog_seq;
  977. idx = syslog_idx;
  978. while (seq < log_next_seq) {
  979. struct log *msg = log_from_idx(idx);
  980. error += msg_print_text(msg, true, NULL, 0);
  981. idx = log_next(idx);
  982. seq++;
  983. }
  984. }
  985. raw_spin_unlock_irq(&logbuf_lock);
  986. break;
  987. /* Size of the log buffer */
  988. case SYSLOG_ACTION_SIZE_BUFFER:
  989. error = log_buf_len;
  990. break;
  991. default:
  992. error = -EINVAL;
  993. break;
  994. }
  995. out:
  996. return error;
  997. }
  998. SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
  999. {
  1000. return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
  1001. }
  1002. #ifdef CONFIG_KGDB_KDB
  1003. /* kdb dmesg command needs access to the syslog buffer. do_syslog()
  1004. * uses locks so it cannot be used during debugging. Just tell kdb
  1005. * where the start and end of the physical and logical logs are. This
  1006. * is equivalent to do_syslog(3).
  1007. */
  1008. void kdb_syslog_data(char *syslog_data[4])
  1009. {
  1010. syslog_data[0] = log_buf;
  1011. syslog_data[1] = log_buf + log_buf_len;
  1012. syslog_data[2] = log_buf + log_first_idx;
  1013. syslog_data[3] = log_buf + log_next_idx;
  1014. }
  1015. #endif /* CONFIG_KGDB_KDB */
  1016. static bool __read_mostly ignore_loglevel;
  1017. static int __init ignore_loglevel_setup(char *str)
  1018. {
  1019. ignore_loglevel = 1;
  1020. printk(KERN_INFO "debug: ignoring loglevel setting.\n");
  1021. return 0;
  1022. }
  1023. early_param("ignore_loglevel", ignore_loglevel_setup);
  1024. module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
  1025. MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
  1026. "print all kernel messages to the console.");
  1027. /*
  1028. * Call the console drivers, asking them to write out
  1029. * log_buf[start] to log_buf[end - 1].
  1030. * The console_lock must be held.
  1031. */
  1032. static void call_console_drivers(int level, const char *text, size_t len)
  1033. {
  1034. struct console *con;
  1035. trace_console(text, 0, len, len);
  1036. if (level >= console_loglevel && !ignore_loglevel)
  1037. return;
  1038. if (!console_drivers)
  1039. return;
  1040. for_each_console(con) {
  1041. if (exclusive_console && con != exclusive_console)
  1042. continue;
  1043. if (!(con->flags & CON_ENABLED))
  1044. continue;
  1045. if (!con->write)
  1046. continue;
  1047. if (!cpu_online(smp_processor_id()) &&
  1048. !(con->flags & CON_ANYTIME))
  1049. continue;
  1050. con->write(con, text, len);
  1051. }
  1052. }
  1053. /*
  1054. * Zap console related locks when oopsing. Only zap at most once
  1055. * every 10 seconds, to leave time for slow consoles to print a
  1056. * full oops.
  1057. */
  1058. static void zap_locks(void)
  1059. {
  1060. static unsigned long oops_timestamp;
  1061. if (time_after_eq(jiffies, oops_timestamp) &&
  1062. !time_after(jiffies, oops_timestamp + 30 * HZ))
  1063. return;
  1064. oops_timestamp = jiffies;
  1065. debug_locks_off();
  1066. /* If a crash is occurring, make sure we can't deadlock */
  1067. raw_spin_lock_init(&logbuf_lock);
  1068. /* And make sure that we print immediately */
  1069. sema_init(&console_sem, 1);
  1070. }
  1071. /* Check if we have any console registered that can be called early in boot. */
  1072. static int have_callable_console(void)
  1073. {
  1074. struct console *con;
  1075. for_each_console(con)
  1076. if (con->flags & CON_ANYTIME)
  1077. return 1;
  1078. return 0;
  1079. }
  1080. /*
  1081. * Can we actually use the console at this time on this cpu?
  1082. *
  1083. * Console drivers may assume that per-cpu resources have
  1084. * been allocated. So unless they're explicitly marked as
  1085. * being able to cope (CON_ANYTIME) don't call them until
  1086. * this CPU is officially up.
  1087. */
  1088. static inline int can_use_console(unsigned int cpu)
  1089. {
  1090. return cpu_online(cpu) || have_callable_console();
  1091. }
  1092. /*
  1093. * Try to get console ownership to actually show the kernel
  1094. * messages from a 'printk'. Return true (and with the
  1095. * console_lock held, and 'console_locked' set) if it
  1096. * is successful, false otherwise.
  1097. *
  1098. * This gets called with the 'logbuf_lock' spinlock held and
  1099. * interrupts disabled. It should return with 'lockbuf_lock'
  1100. * released but interrupts still disabled.
  1101. */
  1102. static int console_trylock_for_printk(unsigned int cpu)
  1103. __releases(&logbuf_lock)
  1104. {
  1105. int retval = 0, wake = 0;
  1106. if (console_trylock()) {
  1107. retval = 1;
  1108. /*
  1109. * If we can't use the console, we need to release
  1110. * the console semaphore by hand to avoid flushing
  1111. * the buffer. We need to hold the console semaphore
  1112. * in order to do this test safely.
  1113. */
  1114. if (!can_use_console(cpu)) {
  1115. console_locked = 0;
  1116. wake = 1;
  1117. retval = 0;
  1118. }
  1119. }
  1120. logbuf_cpu = UINT_MAX;
  1121. if (wake)
  1122. up(&console_sem);
  1123. raw_spin_unlock(&logbuf_lock);
  1124. return retval;
  1125. }
  1126. int printk_delay_msec __read_mostly;
  1127. static inline void printk_delay(void)
  1128. {
  1129. if (unlikely(printk_delay_msec)) {
  1130. int m = printk_delay_msec;
  1131. while (m--) {
  1132. mdelay(1);
  1133. touch_nmi_watchdog();
  1134. }
  1135. }
  1136. }
  1137. /*
  1138. * Continuation lines are buffered, and not committed to the record buffer
  1139. * until the line is complete, or a race forces it. The line fragments
  1140. * though, are printed immediately to the consoles to ensure everything has
  1141. * reached the console in case of a kernel crash.
  1142. */
  1143. static struct cont {
  1144. char buf[LOG_LINE_MAX];
  1145. size_t len; /* length == 0 means unused buffer */
  1146. size_t cons; /* bytes written to console */
  1147. struct task_struct *owner; /* task of first print*/
  1148. u64 ts_nsec; /* time of first print */
  1149. u8 level; /* log level of first message */
  1150. u8 facility; /* log level of first message */
  1151. bool flushed:1; /* buffer sealed and committed */
  1152. } cont;
  1153. static void cont_flush(void)
  1154. {
  1155. if (cont.flushed)
  1156. return;
  1157. if (cont.len == 0)
  1158. return;
  1159. log_store(cont.facility, cont.level, LOG_NOCONS, cont.ts_nsec,
  1160. NULL, 0, cont.buf, cont.len);
  1161. cont.flushed = true;
  1162. }
  1163. static bool cont_add(int facility, int level, const char *text, size_t len)
  1164. {
  1165. if (cont.len && cont.flushed)
  1166. return false;
  1167. if (cont.len + len > sizeof(cont.buf)) {
  1168. cont_flush();
  1169. return false;
  1170. }
  1171. if (!cont.len) {
  1172. cont.facility = facility;
  1173. cont.level = level;
  1174. cont.owner = current;
  1175. cont.ts_nsec = local_clock();
  1176. cont.cons = 0;
  1177. cont.flushed = false;
  1178. }
  1179. memcpy(cont.buf + cont.len, text, len);
  1180. cont.len += len;
  1181. return true;
  1182. }
  1183. static size_t cont_print_text(char *text, size_t size)
  1184. {
  1185. size_t textlen = 0;
  1186. size_t len;
  1187. if (cont.cons == 0) {
  1188. textlen += print_time(cont.ts_nsec, text);
  1189. size -= textlen;
  1190. }
  1191. len = cont.len - cont.cons;
  1192. if (len > 0) {
  1193. if (len+1 > size)
  1194. len = size-1;
  1195. memcpy(text + textlen, cont.buf + cont.cons, len);
  1196. textlen += len;
  1197. cont.cons = cont.len;
  1198. }
  1199. if (cont.flushed) {
  1200. text[textlen++] = '\n';
  1201. /* got everything, release buffer */
  1202. cont.len = 0;
  1203. }
  1204. return textlen;
  1205. }
  1206. asmlinkage int vprintk_emit(int facility, int level,
  1207. const char *dict, size_t dictlen,
  1208. const char *fmt, va_list args)
  1209. {
  1210. static int recursion_bug;
  1211. static char textbuf[LOG_LINE_MAX];
  1212. char *text = textbuf;
  1213. size_t text_len;
  1214. unsigned long flags;
  1215. int this_cpu;
  1216. bool newline = false;
  1217. bool prefix = false;
  1218. int printed_len = 0;
  1219. boot_delay_msec();
  1220. printk_delay();
  1221. /* This stops the holder of console_sem just where we want him */
  1222. local_irq_save(flags);
  1223. this_cpu = smp_processor_id();
  1224. /*
  1225. * Ouch, printk recursed into itself!
  1226. */
  1227. if (unlikely(logbuf_cpu == this_cpu)) {
  1228. /*
  1229. * If a crash is occurring during printk() on this CPU,
  1230. * then try to get the crash message out but make sure
  1231. * we can't deadlock. Otherwise just return to avoid the
  1232. * recursion and return - but flag the recursion so that
  1233. * it can be printed at the next appropriate moment:
  1234. */
  1235. if (!oops_in_progress && !lockdep_recursing(current)) {
  1236. recursion_bug = 1;
  1237. goto out_restore_irqs;
  1238. }
  1239. zap_locks();
  1240. }
  1241. lockdep_off();
  1242. raw_spin_lock(&logbuf_lock);
  1243. logbuf_cpu = this_cpu;
  1244. if (recursion_bug) {
  1245. static const char recursion_msg[] =
  1246. "BUG: recent printk recursion!";
  1247. recursion_bug = 0;
  1248. printed_len += strlen(recursion_msg);
  1249. /* emit KERN_CRIT message */
  1250. log_store(0, 2, LOG_DEFAULT, 0,
  1251. NULL, 0, recursion_msg, printed_len);
  1252. }
  1253. /*
  1254. * The printf needs to come first; we need the syslog
  1255. * prefix which might be passed-in as a parameter.
  1256. */
  1257. text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
  1258. /* mark and strip a trailing newline */
  1259. if (text_len && text[text_len-1] == '\n') {
  1260. text_len--;
  1261. newline = true;
  1262. }
  1263. /* strip syslog prefix and extract log level or control flags */
  1264. if (text[0] == '<' && text[1] && text[2] == '>') {
  1265. switch (text[1]) {
  1266. case '0' ... '7':
  1267. if (level == -1)
  1268. level = text[1] - '0';
  1269. case 'd': /* KERN_DEFAULT */
  1270. prefix = true;
  1271. case 'c': /* KERN_CONT */
  1272. text += 3;
  1273. text_len -= 3;
  1274. }
  1275. }
  1276. if (level == -1)
  1277. level = default_message_loglevel;
  1278. if (dict) {
  1279. prefix = true;
  1280. newline = true;
  1281. }
  1282. if (!newline) {
  1283. /*
  1284. * Flush the conflicting buffer. An earlier newline was missing,
  1285. * or another task also prints continuation lines.
  1286. */
  1287. if (cont.len && (prefix || cont.owner != current))
  1288. cont_flush();
  1289. /* buffer line if possible, otherwise store it right away */
  1290. if (!cont_add(facility, level, text, text_len))
  1291. log_store(facility, level, LOG_DEFAULT, 0,
  1292. dict, dictlen, text, text_len);
  1293. } else {
  1294. bool stored = false;
  1295. /*
  1296. * If an earlier newline was missing and it was the same task,
  1297. * either merge it with the current buffer and flush, or if
  1298. * there was a race with interrupts (prefix == true) then just
  1299. * flush it out and store this line separately.
  1300. */
  1301. if (cont.len && cont.owner == current) {
  1302. if (!prefix)
  1303. stored = cont_add(facility, level, text, text_len);
  1304. cont_flush();
  1305. }
  1306. if (!stored)
  1307. log_store(facility, level, LOG_DEFAULT, 0,
  1308. dict, dictlen, text, text_len);
  1309. }
  1310. printed_len += text_len;
  1311. /*
  1312. * Try to acquire and then immediately release the console semaphore.
  1313. * The release will print out buffers and wake up /dev/kmsg and syslog()
  1314. * users.
  1315. *
  1316. * The console_trylock_for_printk() function will release 'logbuf_lock'
  1317. * regardless of whether it actually gets the console semaphore or not.
  1318. */
  1319. if (console_trylock_for_printk(this_cpu))
  1320. console_unlock();
  1321. lockdep_on();
  1322. out_restore_irqs:
  1323. local_irq_restore(flags);
  1324. return printed_len;
  1325. }
  1326. EXPORT_SYMBOL(vprintk_emit);
  1327. asmlinkage int vprintk(const char *fmt, va_list args)
  1328. {
  1329. return vprintk_emit(0, -1, NULL, 0, fmt, args);
  1330. }
  1331. EXPORT_SYMBOL(vprintk);
  1332. asmlinkage int printk_emit(int facility, int level,
  1333. const char *dict, size_t dictlen,
  1334. const char *fmt, ...)
  1335. {
  1336. va_list args;
  1337. int r;
  1338. va_start(args, fmt);
  1339. r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
  1340. va_end(args);
  1341. return r;
  1342. }
  1343. EXPORT_SYMBOL(printk_emit);
  1344. /**
  1345. * printk - print a kernel message
  1346. * @fmt: format string
  1347. *
  1348. * This is printk(). It can be called from any context. We want it to work.
  1349. *
  1350. * We try to grab the console_lock. If we succeed, it's easy - we log the
  1351. * output and call the console drivers. If we fail to get the semaphore, we
  1352. * place the output into the log buffer and return. The current holder of
  1353. * the console_sem will notice the new output in console_unlock(); and will
  1354. * send it to the consoles before releasing the lock.
  1355. *
  1356. * One effect of this deferred printing is that code which calls printk() and
  1357. * then changes console_loglevel may break. This is because console_loglevel
  1358. * is inspected when the actual printing occurs.
  1359. *
  1360. * See also:
  1361. * printf(3)
  1362. *
  1363. * See the vsnprintf() documentation for format string extensions over C99.
  1364. */
  1365. asmlinkage int printk(const char *fmt, ...)
  1366. {
  1367. va_list args;
  1368. int r;
  1369. #ifdef CONFIG_KGDB_KDB
  1370. if (unlikely(kdb_trap_printk)) {
  1371. va_start(args, fmt);
  1372. r = vkdb_printf(fmt, args);
  1373. va_end(args);
  1374. return r;
  1375. }
  1376. #endif
  1377. va_start(args, fmt);
  1378. r = vprintk_emit(0, -1, NULL, 0, fmt, args);
  1379. va_end(args);
  1380. return r;
  1381. }
  1382. EXPORT_SYMBOL(printk);
  1383. #else
  1384. #define LOG_LINE_MAX 0
  1385. static struct cont {
  1386. size_t len;
  1387. size_t cons;
  1388. u8 level;
  1389. bool flushed:1;
  1390. } cont;
  1391. static struct log *log_from_idx(u32 idx) { return NULL; }
  1392. static u32 log_next(u32 idx) { return 0; }
  1393. static void call_console_drivers(int level, const char *text, size_t len) {}
  1394. static size_t msg_print_text(const struct log *msg, bool syslog,
  1395. char *buf, size_t size) { return 0; }
  1396. static size_t cont_print_text(char *text, size_t size) { return 0; }
  1397. #endif /* CONFIG_PRINTK */
  1398. static int __add_preferred_console(char *name, int idx, char *options,
  1399. char *brl_options)
  1400. {
  1401. struct console_cmdline *c;
  1402. int i;
  1403. /*
  1404. * See if this tty is not yet registered, and
  1405. * if we have a slot free.
  1406. */
  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. if (!brl_options)
  1411. selected_console = i;
  1412. return 0;
  1413. }
  1414. if (i == MAX_CMDLINECONSOLES)
  1415. return -E2BIG;
  1416. if (!brl_options)
  1417. selected_console = i;
  1418. c = &console_cmdline[i];
  1419. strlcpy(c->name, name, sizeof(c->name));
  1420. c->options = options;
  1421. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1422. c->brl_options = brl_options;
  1423. #endif
  1424. c->index = idx;
  1425. return 0;
  1426. }
  1427. /*
  1428. * Set up a list of consoles. Called from init/main.c
  1429. */
  1430. static int __init console_setup(char *str)
  1431. {
  1432. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
  1433. char *s, *options, *brl_options = NULL;
  1434. int idx;
  1435. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1436. if (!memcmp(str, "brl,", 4)) {
  1437. brl_options = "";
  1438. str += 4;
  1439. } else if (!memcmp(str, "brl=", 4)) {
  1440. brl_options = str + 4;
  1441. str = strchr(brl_options, ',');
  1442. if (!str) {
  1443. printk(KERN_ERR "need port name after brl=\n");
  1444. return 1;
  1445. }
  1446. *(str++) = 0;
  1447. }
  1448. #endif
  1449. /*
  1450. * Decode str into name, index, options.
  1451. */
  1452. if (str[0] >= '0' && str[0] <= '9') {
  1453. strcpy(buf, "ttyS");
  1454. strncpy(buf + 4, str, sizeof(buf) - 5);
  1455. } else {
  1456. strncpy(buf, str, sizeof(buf) - 1);
  1457. }
  1458. buf[sizeof(buf) - 1] = 0;
  1459. if ((options = strchr(str, ',')) != NULL)
  1460. *(options++) = 0;
  1461. #ifdef __sparc__
  1462. if (!strcmp(str, "ttya"))
  1463. strcpy(buf, "ttyS0");
  1464. if (!strcmp(str, "ttyb"))
  1465. strcpy(buf, "ttyS1");
  1466. #endif
  1467. for (s = buf; *s; s++)
  1468. if ((*s >= '0' && *s <= '9') || *s == ',')
  1469. break;
  1470. idx = simple_strtoul(s, NULL, 10);
  1471. *s = 0;
  1472. __add_preferred_console(buf, idx, options, brl_options);
  1473. console_set_on_cmdline = 1;
  1474. return 1;
  1475. }
  1476. __setup("console=", console_setup);
  1477. /**
  1478. * add_preferred_console - add a device to the list of preferred consoles.
  1479. * @name: device name
  1480. * @idx: device index
  1481. * @options: options for this console
  1482. *
  1483. * The last preferred console added will be used for kernel messages
  1484. * and stdin/out/err for init. Normally this is used by console_setup
  1485. * above to handle user-supplied console arguments; however it can also
  1486. * be used by arch-specific code either to override the user or more
  1487. * commonly to provide a default console (ie from PROM variables) when
  1488. * the user has not supplied one.
  1489. */
  1490. int add_preferred_console(char *name, int idx, char *options)
  1491. {
  1492. return __add_preferred_console(name, idx, options, NULL);
  1493. }
  1494. int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
  1495. {
  1496. struct console_cmdline *c;
  1497. int i;
  1498. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  1499. if (strcmp(console_cmdline[i].name, name) == 0 &&
  1500. console_cmdline[i].index == idx) {
  1501. c = &console_cmdline[i];
  1502. strlcpy(c->name, name_new, sizeof(c->name));
  1503. c->name[sizeof(c->name) - 1] = 0;
  1504. c->options = options;
  1505. c->index = idx_new;
  1506. return i;
  1507. }
  1508. /* not found */
  1509. return -1;
  1510. }
  1511. bool console_suspend_enabled = 1;
  1512. EXPORT_SYMBOL(console_suspend_enabled);
  1513. static int __init console_suspend_disable(char *str)
  1514. {
  1515. console_suspend_enabled = 0;
  1516. return 1;
  1517. }
  1518. __setup("no_console_suspend", console_suspend_disable);
  1519. module_param_named(console_suspend, console_suspend_enabled,
  1520. bool, S_IRUGO | S_IWUSR);
  1521. MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
  1522. " and hibernate operations");
  1523. /**
  1524. * suspend_console - suspend the console subsystem
  1525. *
  1526. * This disables printk() while we go into suspend states
  1527. */
  1528. void suspend_console(void)
  1529. {
  1530. if (!console_suspend_enabled)
  1531. return;
  1532. printk("Suspending console(s) (use no_console_suspend to debug)\n");
  1533. console_lock();
  1534. console_suspended = 1;
  1535. up(&console_sem);
  1536. }
  1537. void resume_console(void)
  1538. {
  1539. if (!console_suspend_enabled)
  1540. return;
  1541. down(&console_sem);
  1542. console_suspended = 0;
  1543. console_unlock();
  1544. }
  1545. /**
  1546. * console_cpu_notify - print deferred console messages after CPU hotplug
  1547. * @self: notifier struct
  1548. * @action: CPU hotplug event
  1549. * @hcpu: unused
  1550. *
  1551. * If printk() is called from a CPU that is not online yet, the messages
  1552. * will be spooled but will not show up on the console. This function is
  1553. * called when a new CPU comes online (or fails to come up), and ensures
  1554. * that any such output gets printed.
  1555. */
  1556. static int __cpuinit console_cpu_notify(struct notifier_block *self,
  1557. unsigned long action, void *hcpu)
  1558. {
  1559. switch (action) {
  1560. case CPU_ONLINE:
  1561. case CPU_DEAD:
  1562. case CPU_DYING:
  1563. case CPU_DOWN_FAILED:
  1564. case CPU_UP_CANCELED:
  1565. console_lock();
  1566. console_unlock();
  1567. }
  1568. return NOTIFY_OK;
  1569. }
  1570. /**
  1571. * console_lock - lock the console system for exclusive use.
  1572. *
  1573. * Acquires a lock which guarantees that the caller has
  1574. * exclusive access to the console system and the console_drivers list.
  1575. *
  1576. * Can sleep, returns nothing.
  1577. */
  1578. void console_lock(void)
  1579. {
  1580. BUG_ON(in_interrupt());
  1581. down(&console_sem);
  1582. if (console_suspended)
  1583. return;
  1584. console_locked = 1;
  1585. console_may_schedule = 1;
  1586. }
  1587. EXPORT_SYMBOL(console_lock);
  1588. /**
  1589. * console_trylock - try to lock the console system for exclusive use.
  1590. *
  1591. * Tried to acquire a lock which guarantees that the caller has
  1592. * exclusive access to the console system and the console_drivers list.
  1593. *
  1594. * returns 1 on success, and 0 on failure to acquire the lock.
  1595. */
  1596. int console_trylock(void)
  1597. {
  1598. if (down_trylock(&console_sem))
  1599. return 0;
  1600. if (console_suspended) {
  1601. up(&console_sem);
  1602. return 0;
  1603. }
  1604. console_locked = 1;
  1605. console_may_schedule = 0;
  1606. return 1;
  1607. }
  1608. EXPORT_SYMBOL(console_trylock);
  1609. int is_console_locked(void)
  1610. {
  1611. return console_locked;
  1612. }
  1613. /*
  1614. * Delayed printk version, for scheduler-internal messages:
  1615. */
  1616. #define PRINTK_BUF_SIZE 512
  1617. #define PRINTK_PENDING_WAKEUP 0x01
  1618. #define PRINTK_PENDING_SCHED 0x02
  1619. static DEFINE_PER_CPU(int, printk_pending);
  1620. static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
  1621. void printk_tick(void)
  1622. {
  1623. if (__this_cpu_read(printk_pending)) {
  1624. int pending = __this_cpu_xchg(printk_pending, 0);
  1625. if (pending & PRINTK_PENDING_SCHED) {
  1626. char *buf = __get_cpu_var(printk_sched_buf);
  1627. printk(KERN_WARNING "[sched_delayed] %s", buf);
  1628. }
  1629. if (pending & PRINTK_PENDING_WAKEUP)
  1630. wake_up_interruptible(&log_wait);
  1631. }
  1632. }
  1633. int printk_needs_cpu(int cpu)
  1634. {
  1635. if (cpu_is_offline(cpu))
  1636. printk_tick();
  1637. return __this_cpu_read(printk_pending);
  1638. }
  1639. void wake_up_klogd(void)
  1640. {
  1641. if (waitqueue_active(&log_wait))
  1642. this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
  1643. }
  1644. /* the next printk record to write to the console */
  1645. static u64 console_seq;
  1646. static u32 console_idx;
  1647. /**
  1648. * console_unlock - unlock the console system
  1649. *
  1650. * Releases the console_lock which the caller holds on the console system
  1651. * and the console driver list.
  1652. *
  1653. * While the console_lock was held, console output may have been buffered
  1654. * by printk(). If this is the case, console_unlock(); emits
  1655. * the output prior to releasing the lock.
  1656. *
  1657. * If there is output waiting, we wake /dev/kmsg and syslog() users.
  1658. *
  1659. * console_unlock(); may be called from any context.
  1660. */
  1661. void console_unlock(void)
  1662. {
  1663. static char text[LOG_LINE_MAX];
  1664. static u64 seen_seq;
  1665. unsigned long flags;
  1666. bool wake_klogd = false;
  1667. bool retry;
  1668. if (console_suspended) {
  1669. up(&console_sem);
  1670. return;
  1671. }
  1672. console_may_schedule = 0;
  1673. /* flush buffered message fragment immediately to console */
  1674. raw_spin_lock_irqsave(&logbuf_lock, flags);
  1675. if (cont.len && (cont.cons < cont.len || cont.flushed)) {
  1676. size_t len;
  1677. len = cont_print_text(text, sizeof(text));
  1678. raw_spin_unlock(&logbuf_lock);
  1679. stop_critical_timings();
  1680. call_console_drivers(cont.level, text, len);
  1681. start_critical_timings();
  1682. local_irq_restore(flags);
  1683. } else
  1684. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  1685. again:
  1686. for (;;) {
  1687. struct log *msg;
  1688. size_t len;
  1689. int level;
  1690. raw_spin_lock_irqsave(&logbuf_lock, flags);
  1691. if (seen_seq != log_next_seq) {
  1692. wake_klogd = true;
  1693. seen_seq = log_next_seq;
  1694. }
  1695. if (console_seq < log_first_seq) {
  1696. /* messages are gone, move to first one */
  1697. console_seq = log_first_seq;
  1698. console_idx = log_first_idx;
  1699. }
  1700. skip:
  1701. if (console_seq == log_next_seq)
  1702. break;
  1703. msg = log_from_idx(console_idx);
  1704. if (msg->flags & LOG_NOCONS) {
  1705. /*
  1706. * Skip record we have buffered and already printed
  1707. * directly to the console when we received it.
  1708. */
  1709. console_idx = log_next(console_idx);
  1710. console_seq++;
  1711. goto skip;
  1712. }
  1713. level = msg->level;
  1714. len = msg_print_text(msg, false, text, sizeof(text));
  1715. console_idx = log_next(console_idx);
  1716. console_seq++;
  1717. raw_spin_unlock(&logbuf_lock);
  1718. stop_critical_timings(); /* don't trace print latency */
  1719. call_console_drivers(level, text, len);
  1720. start_critical_timings();
  1721. local_irq_restore(flags);
  1722. }
  1723. console_locked = 0;
  1724. /* Release the exclusive_console once it is used */
  1725. if (unlikely(exclusive_console))
  1726. exclusive_console = NULL;
  1727. raw_spin_unlock(&logbuf_lock);
  1728. up(&console_sem);
  1729. /*
  1730. * Someone could have filled up the buffer again, so re-check if there's
  1731. * something to flush. In case we cannot trylock the console_sem again,
  1732. * there's a new owner and the console_unlock() from them will do the
  1733. * flush, no worries.
  1734. */
  1735. raw_spin_lock(&logbuf_lock);
  1736. retry = console_seq != log_next_seq;
  1737. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  1738. if (retry && console_trylock())
  1739. goto again;
  1740. if (wake_klogd)
  1741. wake_up_klogd();
  1742. }
  1743. EXPORT_SYMBOL(console_unlock);
  1744. /**
  1745. * console_conditional_schedule - yield the CPU if required
  1746. *
  1747. * If the console code is currently allowed to sleep, and
  1748. * if this CPU should yield the CPU to another task, do
  1749. * so here.
  1750. *
  1751. * Must be called within console_lock();.
  1752. */
  1753. void __sched console_conditional_schedule(void)
  1754. {
  1755. if (console_may_schedule)
  1756. cond_resched();
  1757. }
  1758. EXPORT_SYMBOL(console_conditional_schedule);
  1759. void console_unblank(void)
  1760. {
  1761. struct console *c;
  1762. /*
  1763. * console_unblank can no longer be called in interrupt context unless
  1764. * oops_in_progress is set to 1..
  1765. */
  1766. if (oops_in_progress) {
  1767. if (down_trylock(&console_sem) != 0)
  1768. return;
  1769. } else
  1770. console_lock();
  1771. console_locked = 1;
  1772. console_may_schedule = 0;
  1773. for_each_console(c)
  1774. if ((c->flags & CON_ENABLED) && c->unblank)
  1775. c->unblank();
  1776. console_unlock();
  1777. }
  1778. /*
  1779. * Return the console tty driver structure and its associated index
  1780. */
  1781. struct tty_driver *console_device(int *index)
  1782. {
  1783. struct console *c;
  1784. struct tty_driver *driver = NULL;
  1785. console_lock();
  1786. for_each_console(c) {
  1787. if (!c->device)
  1788. continue;
  1789. driver = c->device(c, index);
  1790. if (driver)
  1791. break;
  1792. }
  1793. console_unlock();
  1794. return driver;
  1795. }
  1796. /*
  1797. * Prevent further output on the passed console device so that (for example)
  1798. * serial drivers can disable console output before suspending a port, and can
  1799. * re-enable output afterwards.
  1800. */
  1801. void console_stop(struct console *console)
  1802. {
  1803. console_lock();
  1804. console->flags &= ~CON_ENABLED;
  1805. console_unlock();
  1806. }
  1807. EXPORT_SYMBOL(console_stop);
  1808. void console_start(struct console *console)
  1809. {
  1810. console_lock();
  1811. console->flags |= CON_ENABLED;
  1812. console_unlock();
  1813. }
  1814. EXPORT_SYMBOL(console_start);
  1815. static int __read_mostly keep_bootcon;
  1816. static int __init keep_bootcon_setup(char *str)
  1817. {
  1818. keep_bootcon = 1;
  1819. printk(KERN_INFO "debug: skip boot console de-registration.\n");
  1820. return 0;
  1821. }
  1822. early_param("keep_bootcon", keep_bootcon_setup);
  1823. /*
  1824. * The console driver calls this routine during kernel initialization
  1825. * to register the console printing procedure with printk() and to
  1826. * print any messages that were printed by the kernel before the
  1827. * console driver was initialized.
  1828. *
  1829. * This can happen pretty early during the boot process (because of
  1830. * early_printk) - sometimes before setup_arch() completes - be careful
  1831. * of what kernel features are used - they may not be initialised yet.
  1832. *
  1833. * There are two types of consoles - bootconsoles (early_printk) and
  1834. * "real" consoles (everything which is not a bootconsole) which are
  1835. * handled differently.
  1836. * - Any number of bootconsoles can be registered at any time.
  1837. * - As soon as a "real" console is registered, all bootconsoles
  1838. * will be unregistered automatically.
  1839. * - Once a "real" console is registered, any attempt to register a
  1840. * bootconsoles will be rejected
  1841. */
  1842. void register_console(struct console *newcon)
  1843. {
  1844. int i;
  1845. unsigned long flags;
  1846. struct console *bcon = NULL;
  1847. /*
  1848. * before we register a new CON_BOOT console, make sure we don't
  1849. * already have a valid console
  1850. */
  1851. if (console_drivers && newcon->flags & CON_BOOT) {
  1852. /* find the last or real console */
  1853. for_each_console(bcon) {
  1854. if (!(bcon->flags & CON_BOOT)) {
  1855. printk(KERN_INFO "Too late to register bootconsole %s%d\n",
  1856. newcon->name, newcon->index);
  1857. return;
  1858. }
  1859. }
  1860. }
  1861. if (console_drivers && console_drivers->flags & CON_BOOT)
  1862. bcon = console_drivers;
  1863. if (preferred_console < 0 || bcon || !console_drivers)
  1864. preferred_console = selected_console;
  1865. if (newcon->early_setup)
  1866. newcon->early_setup();
  1867. /*
  1868. * See if we want to use this console driver. If we
  1869. * didn't select a console we take the first one
  1870. * that registers here.
  1871. */
  1872. if (preferred_console < 0) {
  1873. if (newcon->index < 0)
  1874. newcon->index = 0;
  1875. if (newcon->setup == NULL ||
  1876. newcon->setup(newcon, NULL) == 0) {
  1877. newcon->flags |= CON_ENABLED;
  1878. if (newcon->device) {
  1879. newcon->flags |= CON_CONSDEV;
  1880. preferred_console = 0;
  1881. }
  1882. }
  1883. }
  1884. /*
  1885. * See if this console matches one we selected on
  1886. * the command line.
  1887. */
  1888. for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
  1889. i++) {
  1890. if (strcmp(console_cmdline[i].name, newcon->name) != 0)
  1891. continue;
  1892. if (newcon->index >= 0 &&
  1893. newcon->index != console_cmdline[i].index)
  1894. continue;
  1895. if (newcon->index < 0)
  1896. newcon->index = console_cmdline[i].index;
  1897. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1898. if (console_cmdline[i].brl_options) {
  1899. newcon->flags |= CON_BRL;
  1900. braille_register_console(newcon,
  1901. console_cmdline[i].index,
  1902. console_cmdline[i].options,
  1903. console_cmdline[i].brl_options);
  1904. return;
  1905. }
  1906. #endif
  1907. if (newcon->setup &&
  1908. newcon->setup(newcon, console_cmdline[i].options) != 0)
  1909. break;
  1910. newcon->flags |= CON_ENABLED;
  1911. newcon->index = console_cmdline[i].index;
  1912. if (i == selected_console) {
  1913. newcon->flags |= CON_CONSDEV;
  1914. preferred_console = selected_console;
  1915. }
  1916. break;
  1917. }
  1918. if (!(newcon->flags & CON_ENABLED))
  1919. return;
  1920. /*
  1921. * If we have a bootconsole, and are switching to a real console,
  1922. * don't print everything out again, since when the boot console, and
  1923. * the real console are the same physical device, it's annoying to
  1924. * see the beginning boot messages twice
  1925. */
  1926. if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
  1927. newcon->flags &= ~CON_PRINTBUFFER;
  1928. /*
  1929. * Put this console in the list - keep the
  1930. * preferred driver at the head of the list.
  1931. */
  1932. console_lock();
  1933. if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
  1934. newcon->next = console_drivers;
  1935. console_drivers = newcon;
  1936. if (newcon->next)
  1937. newcon->next->flags &= ~CON_CONSDEV;
  1938. } else {
  1939. newcon->next = console_drivers->next;
  1940. console_drivers->next = newcon;
  1941. }
  1942. if (newcon->flags & CON_PRINTBUFFER) {
  1943. /*
  1944. * console_unlock(); will print out the buffered messages
  1945. * for us.
  1946. */
  1947. raw_spin_lock_irqsave(&logbuf_lock, flags);
  1948. console_seq = syslog_seq;
  1949. console_idx = syslog_idx;
  1950. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  1951. /*
  1952. * We're about to replay the log buffer. Only do this to the
  1953. * just-registered console to avoid excessive message spam to
  1954. * the already-registered consoles.
  1955. */
  1956. exclusive_console = newcon;
  1957. }
  1958. console_unlock();
  1959. console_sysfs_notify();
  1960. /*
  1961. * By unregistering the bootconsoles after we enable the real console
  1962. * we get the "console xxx enabled" message on all the consoles -
  1963. * boot consoles, real consoles, etc - this is to ensure that end
  1964. * users know there might be something in the kernel's log buffer that
  1965. * went to the bootconsole (that they do not see on the real console)
  1966. */
  1967. if (bcon &&
  1968. ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
  1969. !keep_bootcon) {
  1970. /* we need to iterate through twice, to make sure we print
  1971. * everything out, before we unregister the console(s)
  1972. */
  1973. printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
  1974. newcon->name, newcon->index);
  1975. for_each_console(bcon)
  1976. if (bcon->flags & CON_BOOT)
  1977. unregister_console(bcon);
  1978. } else {
  1979. printk(KERN_INFO "%sconsole [%s%d] enabled\n",
  1980. (newcon->flags & CON_BOOT) ? "boot" : "" ,
  1981. newcon->name, newcon->index);
  1982. }
  1983. }
  1984. EXPORT_SYMBOL(register_console);
  1985. int unregister_console(struct console *console)
  1986. {
  1987. struct console *a, *b;
  1988. int res = 1;
  1989. #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
  1990. if (console->flags & CON_BRL)
  1991. return braille_unregister_console(console);
  1992. #endif
  1993. console_lock();
  1994. if (console_drivers == console) {
  1995. console_drivers=console->next;
  1996. res = 0;
  1997. } else if (console_drivers) {
  1998. for (a=console_drivers->next, b=console_drivers ;
  1999. a; b=a, a=b->next) {
  2000. if (a == console) {
  2001. b->next = a->next;
  2002. res = 0;
  2003. break;
  2004. }
  2005. }
  2006. }
  2007. /*
  2008. * If this isn't the last console and it has CON_CONSDEV set, we
  2009. * need to set it on the next preferred console.
  2010. */
  2011. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  2012. console_drivers->flags |= CON_CONSDEV;
  2013. console_unlock();
  2014. console_sysfs_notify();
  2015. return res;
  2016. }
  2017. EXPORT_SYMBOL(unregister_console);
  2018. static int __init printk_late_init(void)
  2019. {
  2020. struct console *con;
  2021. for_each_console(con) {
  2022. if (!keep_bootcon && con->flags & CON_BOOT) {
  2023. printk(KERN_INFO "turn off boot console %s%d\n",
  2024. con->name, con->index);
  2025. unregister_console(con);
  2026. }
  2027. }
  2028. hotcpu_notifier(console_cpu_notify, 0);
  2029. return 0;
  2030. }
  2031. late_initcall(printk_late_init);
  2032. #if defined CONFIG_PRINTK
  2033. int printk_sched(const char *fmt, ...)
  2034. {
  2035. unsigned long flags;
  2036. va_list args;
  2037. char *buf;
  2038. int r;
  2039. local_irq_save(flags);
  2040. buf = __get_cpu_var(printk_sched_buf);
  2041. va_start(args, fmt);
  2042. r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
  2043. va_end(args);
  2044. __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
  2045. local_irq_restore(flags);
  2046. return r;
  2047. }
  2048. /*
  2049. * printk rate limiting, lifted from the networking subsystem.
  2050. *
  2051. * This enforces a rate limit: not more than 10 kernel messages
  2052. * every 5s to make a denial-of-service attack impossible.
  2053. */
  2054. DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
  2055. int __printk_ratelimit(const char *func)
  2056. {
  2057. return ___ratelimit(&printk_ratelimit_state, func);
  2058. }
  2059. EXPORT_SYMBOL(__printk_ratelimit);
  2060. /**
  2061. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  2062. * @caller_jiffies: pointer to caller's state
  2063. * @interval_msecs: minimum interval between prints
  2064. *
  2065. * printk_timed_ratelimit() returns true if more than @interval_msecs
  2066. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  2067. * returned true.
  2068. */
  2069. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  2070. unsigned int interval_msecs)
  2071. {
  2072. if (*caller_jiffies == 0
  2073. || !time_in_range(jiffies, *caller_jiffies,
  2074. *caller_jiffies
  2075. + msecs_to_jiffies(interval_msecs))) {
  2076. *caller_jiffies = jiffies;
  2077. return true;
  2078. }
  2079. return false;
  2080. }
  2081. EXPORT_SYMBOL(printk_timed_ratelimit);
  2082. static DEFINE_SPINLOCK(dump_list_lock);
  2083. static LIST_HEAD(dump_list);
  2084. /**
  2085. * kmsg_dump_register - register a kernel log dumper.
  2086. * @dumper: pointer to the kmsg_dumper structure
  2087. *
  2088. * Adds a kernel log dumper to the system. The dump callback in the
  2089. * structure will be called when the kernel oopses or panics and must be
  2090. * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
  2091. */
  2092. int kmsg_dump_register(struct kmsg_dumper *dumper)
  2093. {
  2094. unsigned long flags;
  2095. int err = -EBUSY;
  2096. /* The dump callback needs to be set */
  2097. if (!dumper->dump)
  2098. return -EINVAL;
  2099. spin_lock_irqsave(&dump_list_lock, flags);
  2100. /* Don't allow registering multiple times */
  2101. if (!dumper->registered) {
  2102. dumper->registered = 1;
  2103. list_add_tail_rcu(&dumper->list, &dump_list);
  2104. err = 0;
  2105. }
  2106. spin_unlock_irqrestore(&dump_list_lock, flags);
  2107. return err;
  2108. }
  2109. EXPORT_SYMBOL_GPL(kmsg_dump_register);
  2110. /**
  2111. * kmsg_dump_unregister - unregister a kmsg dumper.
  2112. * @dumper: pointer to the kmsg_dumper structure
  2113. *
  2114. * Removes a dump device from the system. Returns zero on success and
  2115. * %-EINVAL otherwise.
  2116. */
  2117. int kmsg_dump_unregister(struct kmsg_dumper *dumper)
  2118. {
  2119. unsigned long flags;
  2120. int err = -EINVAL;
  2121. spin_lock_irqsave(&dump_list_lock, flags);
  2122. if (dumper->registered) {
  2123. dumper->registered = 0;
  2124. list_del_rcu(&dumper->list);
  2125. err = 0;
  2126. }
  2127. spin_unlock_irqrestore(&dump_list_lock, flags);
  2128. synchronize_rcu();
  2129. return err;
  2130. }
  2131. EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
  2132. static bool always_kmsg_dump;
  2133. module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
  2134. /**
  2135. * kmsg_dump - dump kernel log to kernel message dumpers.
  2136. * @reason: the reason (oops, panic etc) for dumping
  2137. *
  2138. * Call each of the registered dumper's dump() callback, which can
  2139. * retrieve the kmsg records with kmsg_dump_get_line() or
  2140. * kmsg_dump_get_buffer().
  2141. */
  2142. void kmsg_dump(enum kmsg_dump_reason reason)
  2143. {
  2144. struct kmsg_dumper *dumper;
  2145. unsigned long flags;
  2146. if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
  2147. return;
  2148. rcu_read_lock();
  2149. list_for_each_entry_rcu(dumper, &dump_list, list) {
  2150. if (dumper->max_reason && reason > dumper->max_reason)
  2151. continue;
  2152. /* initialize iterator with data about the stored records */
  2153. dumper->active = true;
  2154. raw_spin_lock_irqsave(&logbuf_lock, flags);
  2155. dumper->cur_seq = clear_seq;
  2156. dumper->cur_idx = clear_idx;
  2157. dumper->next_seq = log_next_seq;
  2158. dumper->next_idx = log_next_idx;
  2159. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2160. /* invoke dumper which will iterate over records */
  2161. dumper->dump(dumper, reason);
  2162. /* reset iterator */
  2163. dumper->active = false;
  2164. }
  2165. rcu_read_unlock();
  2166. }
  2167. /**
  2168. * kmsg_dump_get_line - retrieve one kmsg log line
  2169. * @dumper: registered kmsg dumper
  2170. * @syslog: include the "<4>" prefixes
  2171. * @line: buffer to copy the line to
  2172. * @size: maximum size of the buffer
  2173. * @len: length of line placed into buffer
  2174. *
  2175. * Start at the beginning of the kmsg buffer, with the oldest kmsg
  2176. * record, and copy one record into the provided buffer.
  2177. *
  2178. * Consecutive calls will return the next available record moving
  2179. * towards the end of the buffer with the youngest messages.
  2180. *
  2181. * A return value of FALSE indicates that there are no more records to
  2182. * read.
  2183. */
  2184. bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
  2185. char *line, size_t size, size_t *len)
  2186. {
  2187. unsigned long flags;
  2188. struct log *msg;
  2189. size_t l = 0;
  2190. bool ret = false;
  2191. if (!dumper->active)
  2192. goto out;
  2193. raw_spin_lock_irqsave(&logbuf_lock, flags);
  2194. if (dumper->cur_seq < log_first_seq) {
  2195. /* messages are gone, move to first available one */
  2196. dumper->cur_seq = log_first_seq;
  2197. dumper->cur_idx = log_first_idx;
  2198. }
  2199. /* last entry */
  2200. if (dumper->cur_seq >= log_next_seq) {
  2201. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2202. goto out;
  2203. }
  2204. msg = log_from_idx(dumper->cur_idx);
  2205. l = msg_print_text(msg, syslog,
  2206. line, size);
  2207. dumper->cur_idx = log_next(dumper->cur_idx);
  2208. dumper->cur_seq++;
  2209. ret = true;
  2210. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2211. out:
  2212. if (len)
  2213. *len = l;
  2214. return ret;
  2215. }
  2216. EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
  2217. /**
  2218. * kmsg_dump_get_buffer - copy kmsg log lines
  2219. * @dumper: registered kmsg dumper
  2220. * @syslog: include the "<4>" prefixes
  2221. * @buf: buffer to copy the line to
  2222. * @size: maximum size of the buffer
  2223. * @len: length of line placed into buffer
  2224. *
  2225. * Start at the end of the kmsg buffer and fill the provided buffer
  2226. * with as many of the the *youngest* kmsg records that fit into it.
  2227. * If the buffer is large enough, all available kmsg records will be
  2228. * copied with a single call.
  2229. *
  2230. * Consecutive calls will fill the buffer with the next block of
  2231. * available older records, not including the earlier retrieved ones.
  2232. *
  2233. * A return value of FALSE indicates that there are no more records to
  2234. * read.
  2235. */
  2236. bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
  2237. char *buf, size_t size, size_t *len)
  2238. {
  2239. unsigned long flags;
  2240. u64 seq;
  2241. u32 idx;
  2242. u64 next_seq;
  2243. u32 next_idx;
  2244. size_t l = 0;
  2245. bool ret = false;
  2246. if (!dumper->active)
  2247. goto out;
  2248. raw_spin_lock_irqsave(&logbuf_lock, flags);
  2249. if (dumper->cur_seq < log_first_seq) {
  2250. /* messages are gone, move to first available one */
  2251. dumper->cur_seq = log_first_seq;
  2252. dumper->cur_idx = log_first_idx;
  2253. }
  2254. /* last entry */
  2255. if (dumper->cur_seq >= dumper->next_seq) {
  2256. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2257. goto out;
  2258. }
  2259. /* calculate length of entire buffer */
  2260. seq = dumper->cur_seq;
  2261. idx = dumper->cur_idx;
  2262. while (seq < dumper->next_seq) {
  2263. struct log *msg = log_from_idx(idx);
  2264. l += msg_print_text(msg, true, NULL, 0);
  2265. idx = log_next(idx);
  2266. seq++;
  2267. }
  2268. /* move first record forward until length fits into the buffer */
  2269. seq = dumper->cur_seq;
  2270. idx = dumper->cur_idx;
  2271. while (l > size && seq < dumper->next_seq) {
  2272. struct log *msg = log_from_idx(idx);
  2273. l -= msg_print_text(msg, true, NULL, 0);
  2274. idx = log_next(idx);
  2275. seq++;
  2276. }
  2277. /* last message in next interation */
  2278. next_seq = seq;
  2279. next_idx = idx;
  2280. l = 0;
  2281. while (seq < dumper->next_seq) {
  2282. struct log *msg = log_from_idx(idx);
  2283. l += msg_print_text(msg, syslog,
  2284. buf + l, size - l);
  2285. idx = log_next(idx);
  2286. seq++;
  2287. }
  2288. dumper->next_seq = next_seq;
  2289. dumper->next_idx = next_idx;
  2290. ret = true;
  2291. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2292. out:
  2293. if (len)
  2294. *len = l;
  2295. return ret;
  2296. }
  2297. EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
  2298. /**
  2299. * kmsg_dump_rewind - reset the interator
  2300. * @dumper: registered kmsg dumper
  2301. *
  2302. * Reset the dumper's iterator so that kmsg_dump_get_line() and
  2303. * kmsg_dump_get_buffer() can be called again and used multiple
  2304. * times within the same dumper.dump() callback.
  2305. */
  2306. void kmsg_dump_rewind(struct kmsg_dumper *dumper)
  2307. {
  2308. unsigned long flags;
  2309. raw_spin_lock_irqsave(&logbuf_lock, flags);
  2310. dumper->cur_seq = clear_seq;
  2311. dumper->cur_idx = clear_idx;
  2312. dumper->next_seq = log_next_seq;
  2313. dumper->next_idx = log_next_idx;
  2314. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  2315. }
  2316. EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
  2317. #endif