printk.c 83 KB

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