printk.c 84 KB

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