printk.c 56 KB

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