printk.c 75 KB

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