printk.c 84 KB

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