printk.c 72 KB

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