printk.c 78 KB

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