printk.c 70 KB

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