printk.c 70 KB

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