printk.c 67 KB

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