printk.c 62 KB

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