printk.c 84 KB

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