printk.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/printk.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * Modified to make sys_syslog() more flexible: added commands to
  8. * return the last 4k of kernel messages, regardless of whether
  9. * they've been read or not. Added option to suppress kernel printk's
  10. * to the console. Added hook for sending the console messages
  11. * elsewhere, in preparation for a serial line console (someday).
  12. * Ted Ts'o, 2/11/93.
  13. * Modified for sysctl support, 1/8/97, Chris Horn.
  14. * Fixed SMP synchronization, 08/08/99, Manfred Spraul
  15. * [email protected]
  16. * Rewrote bits to get rid of console_lock
  17. * 01Mar01 Andrew Morton
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_driver.h>
  24. #include <linux/console.h>
  25. #include <linux/init.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/nmi.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/delay.h>
  31. #include <linux/smp.h>
  32. #include <linux/security.h>
  33. #include <linux/memblock.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/crash_core.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. #undef CREATE_TRACE_POINTS
  54. #include <trace/hooks/printk.h>
  55. #include "printk_ringbuffer.h"
  56. #include "console_cmdline.h"
  57. #include "braille.h"
  58. #include "internal.h"
  59. int console_printk[4] = {
  60. CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */
  61. MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */
  62. CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */
  63. CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */
  64. };
  65. EXPORT_SYMBOL_GPL(console_printk);
  66. atomic_t ignore_console_lock_warning __read_mostly = ATOMIC_INIT(0);
  67. EXPORT_SYMBOL(ignore_console_lock_warning);
  68. /*
  69. * Low level drivers may need that to know if they can schedule in
  70. * their unblank() callback or not. So let's export it.
  71. */
  72. int oops_in_progress;
  73. EXPORT_SYMBOL(oops_in_progress);
  74. /*
  75. * console_sem protects the console_drivers list, and also
  76. * provides serialisation for access to the entire console
  77. * driver system.
  78. */
  79. static DEFINE_SEMAPHORE(console_sem);
  80. struct console *console_drivers;
  81. EXPORT_SYMBOL_GPL(console_drivers);
  82. /*
  83. * System may need to suppress printk message under certain
  84. * circumstances, like after kernel panic happens.
  85. */
  86. int __read_mostly suppress_printk;
  87. /*
  88. * During panic, heavy printk by other CPUs can delay the
  89. * panic and risk deadlock on console resources.
  90. */
  91. static int __read_mostly suppress_panic_printk;
  92. #ifdef CONFIG_LOCKDEP
  93. static struct lockdep_map console_lock_dep_map = {
  94. .name = "console_lock"
  95. };
  96. #endif
  97. enum devkmsg_log_bits {
  98. __DEVKMSG_LOG_BIT_ON = 0,
  99. __DEVKMSG_LOG_BIT_OFF,
  100. __DEVKMSG_LOG_BIT_LOCK,
  101. };
  102. enum devkmsg_log_masks {
  103. DEVKMSG_LOG_MASK_ON = BIT(__DEVKMSG_LOG_BIT_ON),
  104. DEVKMSG_LOG_MASK_OFF = BIT(__DEVKMSG_LOG_BIT_OFF),
  105. DEVKMSG_LOG_MASK_LOCK = BIT(__DEVKMSG_LOG_BIT_LOCK),
  106. };
  107. /* Keep both the 'on' and 'off' bits clear, i.e. ratelimit by default: */
  108. #define DEVKMSG_LOG_MASK_DEFAULT 0
  109. static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
  110. static int __control_devkmsg(char *str)
  111. {
  112. size_t len;
  113. if (!str)
  114. return -EINVAL;
  115. len = str_has_prefix(str, "on");
  116. if (len) {
  117. devkmsg_log = DEVKMSG_LOG_MASK_ON;
  118. return len;
  119. }
  120. len = str_has_prefix(str, "off");
  121. if (len) {
  122. devkmsg_log = DEVKMSG_LOG_MASK_OFF;
  123. return len;
  124. }
  125. len = str_has_prefix(str, "ratelimit");
  126. if (len) {
  127. devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
  128. return len;
  129. }
  130. return -EINVAL;
  131. }
  132. static int __init control_devkmsg(char *str)
  133. {
  134. if (__control_devkmsg(str) < 0) {
  135. pr_warn("printk.devkmsg: bad option string '%s'\n", str);
  136. return 1;
  137. }
  138. /*
  139. * Set sysctl string accordingly:
  140. */
  141. if (devkmsg_log == DEVKMSG_LOG_MASK_ON)
  142. strcpy(devkmsg_log_str, "on");
  143. else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF)
  144. strcpy(devkmsg_log_str, "off");
  145. /* else "ratelimit" which is set by default. */
  146. /*
  147. * Sysctl cannot change it anymore. The kernel command line setting of
  148. * this parameter is to force the setting to be permanent throughout the
  149. * runtime of the system. This is a precation measure against userspace
  150. * trying to be a smarta** and attempting to change it up on us.
  151. */
  152. devkmsg_log |= DEVKMSG_LOG_MASK_LOCK;
  153. return 1;
  154. }
  155. __setup("printk.devkmsg=", control_devkmsg);
  156. char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit";
  157. #if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL)
  158. int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
  159. void *buffer, size_t *lenp, loff_t *ppos)
  160. {
  161. char old_str[DEVKMSG_STR_MAX_SIZE];
  162. unsigned int old;
  163. int err;
  164. if (write) {
  165. if (devkmsg_log & DEVKMSG_LOG_MASK_LOCK)
  166. return -EINVAL;
  167. old = devkmsg_log;
  168. strncpy(old_str, devkmsg_log_str, DEVKMSG_STR_MAX_SIZE);
  169. }
  170. err = proc_dostring(table, write, buffer, lenp, ppos);
  171. if (err)
  172. return err;
  173. if (write) {
  174. err = __control_devkmsg(devkmsg_log_str);
  175. /*
  176. * Do not accept an unknown string OR a known string with
  177. * trailing crap...
  178. */
  179. if (err < 0 || (err + 1 != *lenp)) {
  180. /* ... and restore old setting. */
  181. devkmsg_log = old;
  182. strncpy(devkmsg_log_str, old_str, DEVKMSG_STR_MAX_SIZE);
  183. return -EINVAL;
  184. }
  185. }
  186. return 0;
  187. }
  188. #endif /* CONFIG_PRINTK && CONFIG_SYSCTL */
  189. /*
  190. * Helper macros to handle lockdep when locking/unlocking console_sem. We use
  191. * macros instead of functions so that _RET_IP_ contains useful information.
  192. */
  193. #define down_console_sem() do { \
  194. down(&console_sem);\
  195. mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);\
  196. } while (0)
  197. static int __down_trylock_console_sem(unsigned long ip)
  198. {
  199. int lock_failed;
  200. unsigned long flags;
  201. /*
  202. * Here and in __up_console_sem() we need to be in safe mode,
  203. * because spindump/WARN/etc from under console ->lock will
  204. * deadlock in printk()->down_trylock_console_sem() otherwise.
  205. */
  206. printk_safe_enter_irqsave(flags);
  207. lock_failed = down_trylock(&console_sem);
  208. printk_safe_exit_irqrestore(flags);
  209. if (lock_failed)
  210. return 1;
  211. mutex_acquire(&console_lock_dep_map, 0, 1, ip);
  212. return 0;
  213. }
  214. #define down_trylock_console_sem() __down_trylock_console_sem(_RET_IP_)
  215. static void __up_console_sem(unsigned long ip)
  216. {
  217. unsigned long flags;
  218. mutex_release(&console_lock_dep_map, ip);
  219. printk_safe_enter_irqsave(flags);
  220. up(&console_sem);
  221. printk_safe_exit_irqrestore(flags);
  222. }
  223. #define up_console_sem() __up_console_sem(_RET_IP_)
  224. static bool panic_in_progress(void)
  225. {
  226. return unlikely(atomic_read(&panic_cpu) != PANIC_CPU_INVALID);
  227. }
  228. /*
  229. * This is used for debugging the mess that is the VT code by
  230. * keeping track if we have the console semaphore held. It's
  231. * definitely not the perfect debug tool (we don't know if _WE_
  232. * hold it and are racing, but it helps tracking those weird code
  233. * paths in the console code where we end up in places I want
  234. * locked without the console semaphore held).
  235. */
  236. static int console_locked, console_suspended;
  237. /*
  238. * Array of consoles built from command line options (console=)
  239. */
  240. #define MAX_CMDLINECONSOLES 8
  241. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  242. static int preferred_console = -1;
  243. int console_set_on_cmdline;
  244. EXPORT_SYMBOL(console_set_on_cmdline);
  245. /* Flag: console code may call schedule() */
  246. static int console_may_schedule;
  247. enum con_msg_format_flags {
  248. MSG_FORMAT_DEFAULT = 0,
  249. MSG_FORMAT_SYSLOG = (1 << 0),
  250. };
  251. static int console_msg_format = MSG_FORMAT_DEFAULT;
  252. /*
  253. * The printk log buffer consists of a sequenced collection of records, each
  254. * containing variable length message text. Every record also contains its
  255. * own meta-data (@info).
  256. *
  257. * Every record meta-data carries the timestamp in microseconds, as well as
  258. * the standard userspace syslog level and syslog facility. The usual kernel
  259. * messages use LOG_KERN; userspace-injected messages always carry a matching
  260. * syslog facility, by default LOG_USER. The origin of every message can be
  261. * reliably determined that way.
  262. *
  263. * The human readable log message of a record is available in @text, the
  264. * length of the message text in @text_len. The stored message is not
  265. * terminated.
  266. *
  267. * Optionally, a record can carry a dictionary of properties (key/value
  268. * pairs), to provide userspace with a machine-readable message context.
  269. *
  270. * Examples for well-defined, commonly used property names are:
  271. * DEVICE=b12:8 device identifier
  272. * b12:8 block dev_t
  273. * c127:3 char dev_t
  274. * n8 netdev ifindex
  275. * +sound:card0 subsystem:devname
  276. * SUBSYSTEM=pci driver-core subsystem name
  277. *
  278. * Valid characters in property names are [a-zA-Z0-9.-_]. Property names
  279. * and values are terminated by a '\0' character.
  280. *
  281. * Example of record values:
  282. * record.text_buf = "it's a line" (unterminated)
  283. * record.info.seq = 56
  284. * record.info.ts_nsec = 36863
  285. * record.info.text_len = 11
  286. * record.info.facility = 0 (LOG_KERN)
  287. * record.info.flags = 0
  288. * record.info.level = 3 (LOG_ERR)
  289. * record.info.caller_id = 299 (task 299)
  290. * record.info.dev_info.subsystem = "pci" (terminated)
  291. * record.info.dev_info.device = "+pci:0000:00:01.0" (terminated)
  292. *
  293. * The 'struct printk_info' buffer must never be directly exported to
  294. * userspace, it is a kernel-private implementation detail that might
  295. * need to be changed in the future, when the requirements change.
  296. *
  297. * /dev/kmsg exports the structured data in the following line format:
  298. * "<level>,<sequnum>,<timestamp>,<contflag>[,additional_values, ... ];<message text>\n"
  299. *
  300. * Users of the export format should ignore possible additional values
  301. * separated by ',', and find the message after the ';' character.
  302. *
  303. * The optional key/value pairs are attached as continuation lines starting
  304. * with a space character and terminated by a newline. All possible
  305. * non-prinatable characters are escaped in the "\xff" notation.
  306. */
  307. /* syslog_lock protects syslog_* variables and write access to clear_seq. */
  308. static DEFINE_MUTEX(syslog_lock);
  309. #ifdef CONFIG_PRINTK
  310. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  311. /* All 3 protected by @syslog_lock. */
  312. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  313. static u64 syslog_seq;
  314. static size_t syslog_partial;
  315. static bool syslog_time;
  316. struct latched_seq {
  317. seqcount_latch_t latch;
  318. u64 val[2];
  319. };
  320. /*
  321. * The next printk record to read after the last 'clear' command. There are
  322. * two copies (updated with seqcount_latch) so that reads can locklessly
  323. * access a valid value. Writers are synchronized by @syslog_lock.
  324. */
  325. static struct latched_seq clear_seq = {
  326. .latch = SEQCNT_LATCH_ZERO(clear_seq.latch),
  327. .val[0] = 0,
  328. .val[1] = 0,
  329. };
  330. #ifdef CONFIG_PRINTK_CALLER
  331. #define PREFIX_MAX 48
  332. #else
  333. #define PREFIX_MAX 32
  334. #endif
  335. /* the maximum size of a formatted record (i.e. with prefix added per line) */
  336. #define CONSOLE_LOG_MAX 1024
  337. /* the maximum size for a dropped text message */
  338. #define DROPPED_TEXT_MAX 64
  339. /* the maximum size allowed to be reserved for a record */
  340. #define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX)
  341. #define LOG_LEVEL(v) ((v) & 0x07)
  342. #define LOG_FACILITY(v) ((v) >> 3 & 0xff)
  343. /* record buffer */
  344. #define LOG_ALIGN __alignof__(unsigned long)
  345. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  346. #define LOG_BUF_LEN_MAX (u32)(1 << 31)
  347. static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
  348. static char *log_buf = __log_buf;
  349. static u32 log_buf_len = __LOG_BUF_LEN;
  350. /*
  351. * Define the average message size. This only affects the number of
  352. * descriptors that will be available. Underestimating is better than
  353. * overestimating (too many available descriptors is better than not enough).
  354. */
  355. #define PRB_AVGBITS 5 /* 32 character average length */
  356. #if CONFIG_LOG_BUF_SHIFT <= PRB_AVGBITS
  357. #error CONFIG_LOG_BUF_SHIFT value too small.
  358. #endif
  359. _DEFINE_PRINTKRB(printk_rb_static, CONFIG_LOG_BUF_SHIFT - PRB_AVGBITS,
  360. PRB_AVGBITS, &__log_buf[0]);
  361. static struct printk_ringbuffer printk_rb_dynamic;
  362. static struct printk_ringbuffer *prb = &printk_rb_static;
  363. /*
  364. * We cannot access per-CPU data (e.g. per-CPU flush irq_work) before
  365. * per_cpu_areas are initialised. This variable is set to true when
  366. * it's safe to access per-CPU data.
  367. */
  368. static bool __printk_percpu_data_ready __ro_after_init;
  369. bool printk_percpu_data_ready(void)
  370. {
  371. return __printk_percpu_data_ready;
  372. }
  373. /* Must be called under syslog_lock. */
  374. static void latched_seq_write(struct latched_seq *ls, u64 val)
  375. {
  376. raw_write_seqcount_latch(&ls->latch);
  377. ls->val[0] = val;
  378. raw_write_seqcount_latch(&ls->latch);
  379. ls->val[1] = val;
  380. }
  381. /* Can be called from any context. */
  382. static u64 latched_seq_read_nolock(struct latched_seq *ls)
  383. {
  384. unsigned int seq;
  385. unsigned int idx;
  386. u64 val;
  387. do {
  388. seq = raw_read_seqcount_latch(&ls->latch);
  389. idx = seq & 0x1;
  390. val = ls->val[idx];
  391. } while (read_seqcount_latch_retry(&ls->latch, seq));
  392. return val;
  393. }
  394. /* Return log buffer address */
  395. char *log_buf_addr_get(void)
  396. {
  397. return log_buf;
  398. }
  399. /* Return log buffer size */
  400. u32 log_buf_len_get(void)
  401. {
  402. return log_buf_len;
  403. }
  404. /*
  405. * Define how much of the log buffer we could take at maximum. The value
  406. * must be greater than two. Note that only half of the buffer is available
  407. * when the index points to the middle.
  408. */
  409. #define MAX_LOG_TAKE_PART 4
  410. static const char trunc_msg[] = "<truncated>";
  411. static void truncate_msg(u16 *text_len, u16 *trunc_msg_len)
  412. {
  413. /*
  414. * The message should not take the whole buffer. Otherwise, it might
  415. * get removed too soon.
  416. */
  417. u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
  418. if (*text_len > max_text_len)
  419. *text_len = max_text_len;
  420. /* enable the warning message (if there is room) */
  421. *trunc_msg_len = strlen(trunc_msg);
  422. if (*text_len >= *trunc_msg_len)
  423. *text_len -= *trunc_msg_len;
  424. else
  425. *trunc_msg_len = 0;
  426. }
  427. int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT);
  428. static int syslog_action_restricted(int type)
  429. {
  430. if (dmesg_restrict)
  431. return 1;
  432. /*
  433. * Unless restricted, we allow "read all" and "get buffer size"
  434. * for everybody.
  435. */
  436. return type != SYSLOG_ACTION_READ_ALL &&
  437. type != SYSLOG_ACTION_SIZE_BUFFER;
  438. }
  439. static int check_syslog_permissions(int type, int source)
  440. {
  441. /*
  442. * If this is from /proc/kmsg and we've already opened it, then we've
  443. * already done the capabilities checks at open time.
  444. */
  445. if (source == SYSLOG_FROM_PROC && type != SYSLOG_ACTION_OPEN)
  446. goto ok;
  447. if (syslog_action_restricted(type)) {
  448. if (capable(CAP_SYSLOG))
  449. goto ok;
  450. /*
  451. * For historical reasons, accept CAP_SYS_ADMIN too, with
  452. * a warning.
  453. */
  454. if (capable(CAP_SYS_ADMIN)) {
  455. pr_warn_once("%s (%d): Attempt to access syslog with "
  456. "CAP_SYS_ADMIN but no CAP_SYSLOG "
  457. "(deprecated).\n",
  458. current->comm, task_pid_nr(current));
  459. goto ok;
  460. }
  461. return -EPERM;
  462. }
  463. ok:
  464. return security_syslog(type);
  465. }
  466. static void append_char(char **pp, char *e, char c)
  467. {
  468. if (*pp < e)
  469. *(*pp)++ = c;
  470. }
  471. static ssize_t info_print_ext_header(char *buf, size_t size,
  472. struct printk_info *info)
  473. {
  474. u64 ts_usec = info->ts_nsec;
  475. char caller[20];
  476. #ifdef CONFIG_PRINTK_CALLER
  477. u32 id = info->caller_id;
  478. snprintf(caller, sizeof(caller), ",caller=%c%u",
  479. id & 0x80000000 ? 'C' : 'T', id & ~0x80000000);
  480. #else
  481. caller[0] = '\0';
  482. #endif
  483. do_div(ts_usec, 1000);
  484. return scnprintf(buf, size, "%u,%llu,%llu,%c%s;",
  485. (info->facility << 3) | info->level, info->seq,
  486. ts_usec, info->flags & LOG_CONT ? 'c' : '-', caller);
  487. }
  488. static ssize_t msg_add_ext_text(char *buf, size_t size,
  489. const char *text, size_t text_len,
  490. unsigned char endc)
  491. {
  492. char *p = buf, *e = buf + size;
  493. size_t i;
  494. /* escape non-printable characters */
  495. for (i = 0; i < text_len; i++) {
  496. unsigned char c = text[i];
  497. if (c < ' ' || c >= 127 || c == '\\')
  498. p += scnprintf(p, e - p, "\\x%02x", c);
  499. else
  500. append_char(&p, e, c);
  501. }
  502. append_char(&p, e, endc);
  503. return p - buf;
  504. }
  505. static ssize_t msg_add_dict_text(char *buf, size_t size,
  506. const char *key, const char *val)
  507. {
  508. size_t val_len = strlen(val);
  509. ssize_t len;
  510. if (!val_len)
  511. return 0;
  512. len = msg_add_ext_text(buf, size, "", 0, ' '); /* dict prefix */
  513. len += msg_add_ext_text(buf + len, size - len, key, strlen(key), '=');
  514. len += msg_add_ext_text(buf + len, size - len, val, val_len, '\n');
  515. return len;
  516. }
  517. static ssize_t msg_print_ext_body(char *buf, size_t size,
  518. char *text, size_t text_len,
  519. struct dev_printk_info *dev_info)
  520. {
  521. ssize_t len;
  522. len = msg_add_ext_text(buf, size, text, text_len, '\n');
  523. if (!dev_info)
  524. goto out;
  525. len += msg_add_dict_text(buf + len, size - len, "SUBSYSTEM",
  526. dev_info->subsystem);
  527. len += msg_add_dict_text(buf + len, size - len, "DEVICE",
  528. dev_info->device);
  529. out:
  530. return len;
  531. }
  532. /* /dev/kmsg - userspace message inject/listen interface */
  533. struct devkmsg_user {
  534. atomic64_t seq;
  535. struct ratelimit_state rs;
  536. struct mutex lock;
  537. char buf[CONSOLE_EXT_LOG_MAX];
  538. struct printk_info info;
  539. char text_buf[CONSOLE_EXT_LOG_MAX];
  540. struct printk_record record;
  541. };
  542. static __printf(3, 4) __cold
  543. int devkmsg_emit(int facility, int level, const char *fmt, ...)
  544. {
  545. va_list args;
  546. int r;
  547. va_start(args, fmt);
  548. r = vprintk_emit(facility, level, NULL, fmt, args);
  549. va_end(args);
  550. return r;
  551. }
  552. static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
  553. {
  554. char *buf, *line;
  555. int level = default_message_loglevel;
  556. int facility = 1; /* LOG_USER */
  557. struct file *file = iocb->ki_filp;
  558. struct devkmsg_user *user = file->private_data;
  559. size_t len = iov_iter_count(from);
  560. ssize_t ret = len;
  561. if (!user || len > LOG_LINE_MAX)
  562. return -EINVAL;
  563. /* Ignore when user logging is disabled. */
  564. if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
  565. return len;
  566. /* Ratelimit when not explicitly enabled. */
  567. if (!(devkmsg_log & DEVKMSG_LOG_MASK_ON)) {
  568. if (!___ratelimit(&user->rs, current->comm))
  569. return ret;
  570. }
  571. buf = kmalloc(len+1, GFP_KERNEL);
  572. if (buf == NULL)
  573. return -ENOMEM;
  574. buf[len] = '\0';
  575. if (!copy_from_iter_full(buf, len, from)) {
  576. kfree(buf);
  577. return -EFAULT;
  578. }
  579. /*
  580. * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
  581. * the decimal value represents 32bit, the lower 3 bit are the log
  582. * level, the rest are the log facility.
  583. *
  584. * If no prefix or no userspace facility is specified, we
  585. * enforce LOG_USER, to be able to reliably distinguish
  586. * kernel-generated messages from userspace-injected ones.
  587. */
  588. line = buf;
  589. if (line[0] == '<') {
  590. char *endp = NULL;
  591. unsigned int u;
  592. u = simple_strtoul(line + 1, &endp, 10);
  593. if (endp && endp[0] == '>') {
  594. level = LOG_LEVEL(u);
  595. if (LOG_FACILITY(u) != 0)
  596. facility = LOG_FACILITY(u);
  597. endp++;
  598. line = endp;
  599. }
  600. }
  601. devkmsg_emit(facility, level, "%s", line);
  602. kfree(buf);
  603. return ret;
  604. }
  605. static ssize_t devkmsg_read(struct file *file, char __user *buf,
  606. size_t count, loff_t *ppos)
  607. {
  608. struct devkmsg_user *user = file->private_data;
  609. struct printk_record *r = &user->record;
  610. size_t len;
  611. ssize_t ret;
  612. if (!user)
  613. return -EBADF;
  614. ret = mutex_lock_interruptible(&user->lock);
  615. if (ret)
  616. return ret;
  617. if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) {
  618. if (file->f_flags & O_NONBLOCK) {
  619. ret = -EAGAIN;
  620. goto out;
  621. }
  622. /*
  623. * Guarantee this task is visible on the waitqueue before
  624. * checking the wake condition.
  625. *
  626. * The full memory barrier within set_current_state() of
  627. * prepare_to_wait_event() pairs with the full memory barrier
  628. * within wq_has_sleeper().
  629. *
  630. * This pairs with __wake_up_klogd:A.
  631. */
  632. ret = wait_event_interruptible(log_wait,
  633. prb_read_valid(prb,
  634. atomic64_read(&user->seq), r)); /* LMM(devkmsg_read:A) */
  635. if (ret)
  636. goto out;
  637. }
  638. if (r->info->seq != atomic64_read(&user->seq)) {
  639. /* our last seen message is gone, return error and reset */
  640. atomic64_set(&user->seq, r->info->seq);
  641. ret = -EPIPE;
  642. goto out;
  643. }
  644. len = info_print_ext_header(user->buf, sizeof(user->buf), r->info);
  645. len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len,
  646. &r->text_buf[0], r->info->text_len,
  647. &r->info->dev_info);
  648. atomic64_set(&user->seq, r->info->seq + 1);
  649. if (len > count) {
  650. ret = -EINVAL;
  651. goto out;
  652. }
  653. if (copy_to_user(buf, user->buf, len)) {
  654. ret = -EFAULT;
  655. goto out;
  656. }
  657. ret = len;
  658. out:
  659. mutex_unlock(&user->lock);
  660. return ret;
  661. }
  662. /*
  663. * Be careful when modifying this function!!!
  664. *
  665. * Only few operations are supported because the device works only with the
  666. * entire variable length messages (records). Non-standard values are
  667. * returned in the other cases and has been this way for quite some time.
  668. * User space applications might depend on this behavior.
  669. */
  670. static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
  671. {
  672. struct devkmsg_user *user = file->private_data;
  673. loff_t ret = 0;
  674. if (!user)
  675. return -EBADF;
  676. if (offset)
  677. return -ESPIPE;
  678. switch (whence) {
  679. case SEEK_SET:
  680. /* the first record */
  681. atomic64_set(&user->seq, prb_first_valid_seq(prb));
  682. break;
  683. case SEEK_DATA:
  684. /*
  685. * The first record after the last SYSLOG_ACTION_CLEAR,
  686. * like issued by 'dmesg -c'. Reading /dev/kmsg itself
  687. * changes no global state, and does not clear anything.
  688. */
  689. atomic64_set(&user->seq, latched_seq_read_nolock(&clear_seq));
  690. break;
  691. case SEEK_END:
  692. /* after the last record */
  693. atomic64_set(&user->seq, prb_next_seq(prb));
  694. break;
  695. default:
  696. ret = -EINVAL;
  697. }
  698. return ret;
  699. }
  700. static __poll_t devkmsg_poll(struct file *file, poll_table *wait)
  701. {
  702. struct devkmsg_user *user = file->private_data;
  703. struct printk_info info;
  704. __poll_t ret = 0;
  705. if (!user)
  706. return EPOLLERR|EPOLLNVAL;
  707. poll_wait(file, &log_wait, wait);
  708. if (prb_read_valid_info(prb, atomic64_read(&user->seq), &info, NULL)) {
  709. /* return error when data has vanished underneath us */
  710. if (info.seq != atomic64_read(&user->seq))
  711. ret = EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
  712. else
  713. ret = EPOLLIN|EPOLLRDNORM;
  714. }
  715. return ret;
  716. }
  717. static int devkmsg_open(struct inode *inode, struct file *file)
  718. {
  719. struct devkmsg_user *user;
  720. int err;
  721. if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
  722. return -EPERM;
  723. /* write-only does not need any file context */
  724. if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
  725. err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
  726. SYSLOG_FROM_READER);
  727. if (err)
  728. return err;
  729. }
  730. user = kvmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
  731. if (!user)
  732. return -ENOMEM;
  733. ratelimit_default_init(&user->rs);
  734. ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE);
  735. mutex_init(&user->lock);
  736. prb_rec_init_rd(&user->record, &user->info,
  737. &user->text_buf[0], sizeof(user->text_buf));
  738. atomic64_set(&user->seq, prb_first_valid_seq(prb));
  739. file->private_data = user;
  740. return 0;
  741. }
  742. static int devkmsg_release(struct inode *inode, struct file *file)
  743. {
  744. struct devkmsg_user *user = file->private_data;
  745. if (!user)
  746. return 0;
  747. ratelimit_state_exit(&user->rs);
  748. mutex_destroy(&user->lock);
  749. kvfree(user);
  750. return 0;
  751. }
  752. const struct file_operations kmsg_fops = {
  753. .open = devkmsg_open,
  754. .read = devkmsg_read,
  755. .write_iter = devkmsg_write,
  756. .llseek = devkmsg_llseek,
  757. .poll = devkmsg_poll,
  758. .release = devkmsg_release,
  759. };
  760. #ifdef CONFIG_CRASH_CORE
  761. /*
  762. * This appends the listed symbols to /proc/vmcore
  763. *
  764. * /proc/vmcore is used by various utilities, like crash and makedumpfile to
  765. * obtain access to symbols that are otherwise very difficult to locate. These
  766. * symbols are specifically used so that utilities can access and extract the
  767. * dmesg log from a vmcore file after a crash.
  768. */
  769. void log_buf_vmcoreinfo_setup(void)
  770. {
  771. struct dev_printk_info *dev_info = NULL;
  772. VMCOREINFO_SYMBOL(prb);
  773. VMCOREINFO_SYMBOL(printk_rb_static);
  774. VMCOREINFO_SYMBOL(clear_seq);
  775. /*
  776. * Export struct size and field offsets. User space tools can
  777. * parse it and detect any changes to structure down the line.
  778. */
  779. VMCOREINFO_STRUCT_SIZE(printk_ringbuffer);
  780. VMCOREINFO_OFFSET(printk_ringbuffer, desc_ring);
  781. VMCOREINFO_OFFSET(printk_ringbuffer, text_data_ring);
  782. VMCOREINFO_OFFSET(printk_ringbuffer, fail);
  783. VMCOREINFO_STRUCT_SIZE(prb_desc_ring);
  784. VMCOREINFO_OFFSET(prb_desc_ring, count_bits);
  785. VMCOREINFO_OFFSET(prb_desc_ring, descs);
  786. VMCOREINFO_OFFSET(prb_desc_ring, infos);
  787. VMCOREINFO_OFFSET(prb_desc_ring, head_id);
  788. VMCOREINFO_OFFSET(prb_desc_ring, tail_id);
  789. VMCOREINFO_STRUCT_SIZE(prb_desc);
  790. VMCOREINFO_OFFSET(prb_desc, state_var);
  791. VMCOREINFO_OFFSET(prb_desc, text_blk_lpos);
  792. VMCOREINFO_STRUCT_SIZE(prb_data_blk_lpos);
  793. VMCOREINFO_OFFSET(prb_data_blk_lpos, begin);
  794. VMCOREINFO_OFFSET(prb_data_blk_lpos, next);
  795. VMCOREINFO_STRUCT_SIZE(printk_info);
  796. VMCOREINFO_OFFSET(printk_info, seq);
  797. VMCOREINFO_OFFSET(printk_info, ts_nsec);
  798. VMCOREINFO_OFFSET(printk_info, text_len);
  799. VMCOREINFO_OFFSET(printk_info, caller_id);
  800. VMCOREINFO_OFFSET(printk_info, dev_info);
  801. VMCOREINFO_STRUCT_SIZE(dev_printk_info);
  802. VMCOREINFO_OFFSET(dev_printk_info, subsystem);
  803. VMCOREINFO_LENGTH(printk_info_subsystem, sizeof(dev_info->subsystem));
  804. VMCOREINFO_OFFSET(dev_printk_info, device);
  805. VMCOREINFO_LENGTH(printk_info_device, sizeof(dev_info->device));
  806. VMCOREINFO_STRUCT_SIZE(prb_data_ring);
  807. VMCOREINFO_OFFSET(prb_data_ring, size_bits);
  808. VMCOREINFO_OFFSET(prb_data_ring, data);
  809. VMCOREINFO_OFFSET(prb_data_ring, head_lpos);
  810. VMCOREINFO_OFFSET(prb_data_ring, tail_lpos);
  811. VMCOREINFO_SIZE(atomic_long_t);
  812. VMCOREINFO_TYPE_OFFSET(atomic_long_t, counter);
  813. VMCOREINFO_STRUCT_SIZE(latched_seq);
  814. VMCOREINFO_OFFSET(latched_seq, val);
  815. }
  816. #endif
  817. /* requested log_buf_len from kernel cmdline */
  818. static unsigned long __initdata new_log_buf_len;
  819. /* we practice scaling the ring buffer by powers of 2 */
  820. static void __init log_buf_len_update(u64 size)
  821. {
  822. if (size > (u64)LOG_BUF_LEN_MAX) {
  823. size = (u64)LOG_BUF_LEN_MAX;
  824. pr_err("log_buf over 2G is not supported.\n");
  825. }
  826. if (size)
  827. size = roundup_pow_of_two(size);
  828. if (size > log_buf_len)
  829. new_log_buf_len = (unsigned long)size;
  830. }
  831. /* save requested log_buf_len since it's too early to process it */
  832. static int __init log_buf_len_setup(char *str)
  833. {
  834. u64 size;
  835. if (!str)
  836. return -EINVAL;
  837. size = memparse(str, &str);
  838. log_buf_len_update(size);
  839. return 0;
  840. }
  841. early_param("log_buf_len", log_buf_len_setup);
  842. #ifdef CONFIG_SMP
  843. #define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT)
  844. static void __init log_buf_add_cpu(void)
  845. {
  846. unsigned int cpu_extra;
  847. /*
  848. * archs should set up cpu_possible_bits properly with
  849. * set_cpu_possible() after setup_arch() but just in
  850. * case lets ensure this is valid.
  851. */
  852. if (num_possible_cpus() == 1)
  853. return;
  854. cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN;
  855. /* by default this will only continue through for large > 64 CPUs */
  856. if (cpu_extra <= __LOG_BUF_LEN / 2)
  857. return;
  858. pr_info("log_buf_len individual max cpu contribution: %d bytes\n",
  859. __LOG_CPU_MAX_BUF_LEN);
  860. pr_info("log_buf_len total cpu_extra contributions: %d bytes\n",
  861. cpu_extra);
  862. pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN);
  863. log_buf_len_update(cpu_extra + __LOG_BUF_LEN);
  864. }
  865. #else /* !CONFIG_SMP */
  866. static inline void log_buf_add_cpu(void) {}
  867. #endif /* CONFIG_SMP */
  868. static void __init set_percpu_data_ready(void)
  869. {
  870. __printk_percpu_data_ready = true;
  871. }
  872. static unsigned int __init add_to_rb(struct printk_ringbuffer *rb,
  873. struct printk_record *r)
  874. {
  875. struct prb_reserved_entry e;
  876. struct printk_record dest_r;
  877. prb_rec_init_wr(&dest_r, r->info->text_len);
  878. if (!prb_reserve(&e, rb, &dest_r))
  879. return 0;
  880. memcpy(&dest_r.text_buf[0], &r->text_buf[0], r->info->text_len);
  881. dest_r.info->text_len = r->info->text_len;
  882. dest_r.info->facility = r->info->facility;
  883. dest_r.info->level = r->info->level;
  884. dest_r.info->flags = r->info->flags;
  885. dest_r.info->ts_nsec = r->info->ts_nsec;
  886. dest_r.info->caller_id = r->info->caller_id;
  887. memcpy(&dest_r.info->dev_info, &r->info->dev_info, sizeof(dest_r.info->dev_info));
  888. prb_final_commit(&e);
  889. return prb_record_text_space(&e);
  890. }
  891. static char setup_text_buf[LOG_LINE_MAX] __initdata;
  892. void __init setup_log_buf(int early)
  893. {
  894. struct printk_info *new_infos;
  895. unsigned int new_descs_count;
  896. struct prb_desc *new_descs;
  897. struct printk_info info;
  898. struct printk_record r;
  899. unsigned int text_size;
  900. size_t new_descs_size;
  901. size_t new_infos_size;
  902. unsigned long flags;
  903. char *new_log_buf;
  904. unsigned int free;
  905. u64 seq;
  906. /*
  907. * Some archs call setup_log_buf() multiple times - first is very
  908. * early, e.g. from setup_arch(), and second - when percpu_areas
  909. * are initialised.
  910. */
  911. if (!early)
  912. set_percpu_data_ready();
  913. if (log_buf != __log_buf)
  914. return;
  915. if (!early && !new_log_buf_len)
  916. log_buf_add_cpu();
  917. if (!new_log_buf_len)
  918. return;
  919. new_descs_count = new_log_buf_len >> PRB_AVGBITS;
  920. if (new_descs_count == 0) {
  921. pr_err("new_log_buf_len: %lu too small\n", new_log_buf_len);
  922. return;
  923. }
  924. new_log_buf = memblock_alloc(new_log_buf_len, LOG_ALIGN);
  925. if (unlikely(!new_log_buf)) {
  926. pr_err("log_buf_len: %lu text bytes not available\n",
  927. new_log_buf_len);
  928. return;
  929. }
  930. new_descs_size = new_descs_count * sizeof(struct prb_desc);
  931. new_descs = memblock_alloc(new_descs_size, LOG_ALIGN);
  932. if (unlikely(!new_descs)) {
  933. pr_err("log_buf_len: %zu desc bytes not available\n",
  934. new_descs_size);
  935. goto err_free_log_buf;
  936. }
  937. new_infos_size = new_descs_count * sizeof(struct printk_info);
  938. new_infos = memblock_alloc(new_infos_size, LOG_ALIGN);
  939. if (unlikely(!new_infos)) {
  940. pr_err("log_buf_len: %zu info bytes not available\n",
  941. new_infos_size);
  942. goto err_free_descs;
  943. }
  944. prb_rec_init_rd(&r, &info, &setup_text_buf[0], sizeof(setup_text_buf));
  945. prb_init(&printk_rb_dynamic,
  946. new_log_buf, ilog2(new_log_buf_len),
  947. new_descs, ilog2(new_descs_count),
  948. new_infos);
  949. local_irq_save(flags);
  950. log_buf_len = new_log_buf_len;
  951. log_buf = new_log_buf;
  952. new_log_buf_len = 0;
  953. free = __LOG_BUF_LEN;
  954. prb_for_each_record(0, &printk_rb_static, seq, &r) {
  955. text_size = add_to_rb(&printk_rb_dynamic, &r);
  956. if (text_size > free)
  957. free = 0;
  958. else
  959. free -= text_size;
  960. }
  961. prb = &printk_rb_dynamic;
  962. local_irq_restore(flags);
  963. /*
  964. * Copy any remaining messages that might have appeared from
  965. * NMI context after copying but before switching to the
  966. * dynamic buffer.
  967. */
  968. prb_for_each_record(seq, &printk_rb_static, seq, &r) {
  969. text_size = add_to_rb(&printk_rb_dynamic, &r);
  970. if (text_size > free)
  971. free = 0;
  972. else
  973. free -= text_size;
  974. }
  975. if (seq != prb_next_seq(&printk_rb_static)) {
  976. pr_err("dropped %llu messages\n",
  977. prb_next_seq(&printk_rb_static) - seq);
  978. }
  979. pr_info("log_buf_len: %u bytes\n", log_buf_len);
  980. pr_info("early log buf free: %u(%u%%)\n",
  981. free, (free * 100) / __LOG_BUF_LEN);
  982. return;
  983. err_free_descs:
  984. memblock_free(new_descs, new_descs_size);
  985. err_free_log_buf:
  986. memblock_free(new_log_buf, new_log_buf_len);
  987. }
  988. static bool __read_mostly ignore_loglevel;
  989. static int __init ignore_loglevel_setup(char *str)
  990. {
  991. ignore_loglevel = true;
  992. pr_info("debug: ignoring loglevel setting.\n");
  993. return 0;
  994. }
  995. early_param("ignore_loglevel", ignore_loglevel_setup);
  996. module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
  997. MODULE_PARM_DESC(ignore_loglevel,
  998. "ignore loglevel setting (prints all kernel messages to the console)");
  999. static bool suppress_message_printing(int level)
  1000. {
  1001. return (level >= console_loglevel && !ignore_loglevel);
  1002. }
  1003. #ifdef CONFIG_BOOT_PRINTK_DELAY
  1004. static int boot_delay; /* msecs delay after each printk during bootup */
  1005. static unsigned long long loops_per_msec; /* based on boot_delay */
  1006. static int __init boot_delay_setup(char *str)
  1007. {
  1008. unsigned long lpj;
  1009. lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
  1010. loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
  1011. get_option(&str, &boot_delay);
  1012. if (boot_delay > 10 * 1000)
  1013. boot_delay = 0;
  1014. pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
  1015. "HZ: %d, loops_per_msec: %llu\n",
  1016. boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
  1017. return 0;
  1018. }
  1019. early_param("boot_delay", boot_delay_setup);
  1020. static void boot_delay_msec(int level)
  1021. {
  1022. unsigned long long k;
  1023. unsigned long timeout;
  1024. if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING)
  1025. || suppress_message_printing(level)) {
  1026. return;
  1027. }
  1028. k = (unsigned long long)loops_per_msec * boot_delay;
  1029. timeout = jiffies + msecs_to_jiffies(boot_delay);
  1030. while (k) {
  1031. k--;
  1032. cpu_relax();
  1033. /*
  1034. * use (volatile) jiffies to prevent
  1035. * compiler reduction; loop termination via jiffies
  1036. * is secondary and may or may not happen.
  1037. */
  1038. if (time_after(jiffies, timeout))
  1039. break;
  1040. touch_nmi_watchdog();
  1041. }
  1042. }
  1043. #else
  1044. static inline void boot_delay_msec(int level)
  1045. {
  1046. }
  1047. #endif
  1048. static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
  1049. module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
  1050. static size_t print_syslog(unsigned int level, char *buf)
  1051. {
  1052. return sprintf(buf, "<%u>", level);
  1053. }
  1054. static size_t print_time(u64 ts, char *buf)
  1055. {
  1056. unsigned long rem_nsec = do_div(ts, 1000000000);
  1057. return sprintf(buf, "[%5lu.%06lu]",
  1058. (unsigned long)ts, rem_nsec / 1000);
  1059. }
  1060. #ifdef CONFIG_PRINTK_CALLER
  1061. static size_t print_caller(u32 id, char *buf)
  1062. {
  1063. char caller[12];
  1064. snprintf(caller, sizeof(caller), "%c%u",
  1065. id & 0x80000000 ? 'C' : 'T', id & ~0x80000000);
  1066. return sprintf(buf, "[%6s]", caller);
  1067. }
  1068. #else
  1069. #define print_caller(id, buf) 0
  1070. #endif
  1071. static size_t info_print_prefix(const struct printk_info *info, bool syslog,
  1072. bool time, char *buf)
  1073. {
  1074. size_t len = 0;
  1075. if (syslog)
  1076. len = print_syslog((info->facility << 3) | info->level, buf);
  1077. if (time)
  1078. len += print_time(info->ts_nsec, buf + len);
  1079. len += print_caller(info->caller_id, buf + len);
  1080. if (IS_ENABLED(CONFIG_PRINTK_CALLER) || time) {
  1081. buf[len++] = ' ';
  1082. buf[len] = '\0';
  1083. }
  1084. return len;
  1085. }
  1086. /*
  1087. * Prepare the record for printing. The text is shifted within the given
  1088. * buffer to avoid a need for another one. The following operations are
  1089. * done:
  1090. *
  1091. * - Add prefix for each line.
  1092. * - Drop truncated lines that no longer fit into the buffer.
  1093. * - Add the trailing newline that has been removed in vprintk_store().
  1094. * - Add a string terminator.
  1095. *
  1096. * Since the produced string is always terminated, the maximum possible
  1097. * return value is @r->text_buf_size - 1;
  1098. *
  1099. * Return: The length of the updated/prepared text, including the added
  1100. * prefixes and the newline. The terminator is not counted. The dropped
  1101. * line(s) are not counted.
  1102. */
  1103. static size_t record_print_text(struct printk_record *r, bool syslog,
  1104. bool time)
  1105. {
  1106. size_t text_len = r->info->text_len;
  1107. size_t buf_size = r->text_buf_size;
  1108. char *text = r->text_buf;
  1109. char prefix[PREFIX_MAX];
  1110. bool truncated = false;
  1111. size_t prefix_len;
  1112. size_t line_len;
  1113. size_t len = 0;
  1114. char *next;
  1115. /*
  1116. * If the message was truncated because the buffer was not large
  1117. * enough, treat the available text as if it were the full text.
  1118. */
  1119. if (text_len > buf_size)
  1120. text_len = buf_size;
  1121. prefix_len = info_print_prefix(r->info, syslog, time, prefix);
  1122. /*
  1123. * @text_len: bytes of unprocessed text
  1124. * @line_len: bytes of current line _without_ newline
  1125. * @text: pointer to beginning of current line
  1126. * @len: number of bytes prepared in r->text_buf
  1127. */
  1128. for (;;) {
  1129. next = memchr(text, '\n', text_len);
  1130. if (next) {
  1131. line_len = next - text;
  1132. } else {
  1133. /* Drop truncated line(s). */
  1134. if (truncated)
  1135. break;
  1136. line_len = text_len;
  1137. }
  1138. /*
  1139. * Truncate the text if there is not enough space to add the
  1140. * prefix and a trailing newline and a terminator.
  1141. */
  1142. if (len + prefix_len + text_len + 1 + 1 > buf_size) {
  1143. /* Drop even the current line if no space. */
  1144. if (len + prefix_len + line_len + 1 + 1 > buf_size)
  1145. break;
  1146. text_len = buf_size - len - prefix_len - 1 - 1;
  1147. truncated = true;
  1148. }
  1149. memmove(text + prefix_len, text, text_len);
  1150. memcpy(text, prefix, prefix_len);
  1151. /*
  1152. * Increment the prepared length to include the text and
  1153. * prefix that were just moved+copied. Also increment for the
  1154. * newline at the end of this line. If this is the last line,
  1155. * there is no newline, but it will be added immediately below.
  1156. */
  1157. len += prefix_len + line_len + 1;
  1158. if (text_len == line_len) {
  1159. /*
  1160. * This is the last line. Add the trailing newline
  1161. * removed in vprintk_store().
  1162. */
  1163. text[prefix_len + line_len] = '\n';
  1164. break;
  1165. }
  1166. /*
  1167. * Advance beyond the added prefix and the related line with
  1168. * its newline.
  1169. */
  1170. text += prefix_len + line_len + 1;
  1171. /*
  1172. * The remaining text has only decreased by the line with its
  1173. * newline.
  1174. *
  1175. * Note that @text_len can become zero. It happens when @text
  1176. * ended with a newline (either due to truncation or the
  1177. * original string ending with "\n\n"). The loop is correctly
  1178. * repeated and (if not truncated) an empty line with a prefix
  1179. * will be prepared.
  1180. */
  1181. text_len -= line_len + 1;
  1182. }
  1183. /*
  1184. * If a buffer was provided, it will be terminated. Space for the
  1185. * string terminator is guaranteed to be available. The terminator is
  1186. * not counted in the return value.
  1187. */
  1188. if (buf_size > 0)
  1189. r->text_buf[len] = 0;
  1190. return len;
  1191. }
  1192. static size_t get_record_print_text_size(struct printk_info *info,
  1193. unsigned int line_count,
  1194. bool syslog, bool time)
  1195. {
  1196. char prefix[PREFIX_MAX];
  1197. size_t prefix_len;
  1198. prefix_len = info_print_prefix(info, syslog, time, prefix);
  1199. /*
  1200. * Each line will be preceded with a prefix. The intermediate
  1201. * newlines are already within the text, but a final trailing
  1202. * newline will be added.
  1203. */
  1204. return ((prefix_len * line_count) + info->text_len + 1);
  1205. }
  1206. /*
  1207. * Beginning with @start_seq, find the first record where it and all following
  1208. * records up to (but not including) @max_seq fit into @size.
  1209. *
  1210. * @max_seq is simply an upper bound and does not need to exist. If the caller
  1211. * does not require an upper bound, -1 can be used for @max_seq.
  1212. */
  1213. static u64 find_first_fitting_seq(u64 start_seq, u64 max_seq, size_t size,
  1214. bool syslog, bool time)
  1215. {
  1216. struct printk_info info;
  1217. unsigned int line_count;
  1218. size_t len = 0;
  1219. u64 seq;
  1220. /* Determine the size of the records up to @max_seq. */
  1221. prb_for_each_info(start_seq, prb, seq, &info, &line_count) {
  1222. if (info.seq >= max_seq)
  1223. break;
  1224. len += get_record_print_text_size(&info, line_count, syslog, time);
  1225. }
  1226. /*
  1227. * Adjust the upper bound for the next loop to avoid subtracting
  1228. * lengths that were never added.
  1229. */
  1230. if (seq < max_seq)
  1231. max_seq = seq;
  1232. /*
  1233. * Move first record forward until length fits into the buffer. Ignore
  1234. * newest messages that were not counted in the above cycle. Messages
  1235. * might appear and get lost in the meantime. This is a best effort
  1236. * that prevents an infinite loop that could occur with a retry.
  1237. */
  1238. prb_for_each_info(start_seq, prb, seq, &info, &line_count) {
  1239. if (len <= size || info.seq >= max_seq)
  1240. break;
  1241. len -= get_record_print_text_size(&info, line_count, syslog, time);
  1242. }
  1243. return seq;
  1244. }
  1245. /* The caller is responsible for making sure @size is greater than 0. */
  1246. static int syslog_print(char __user *buf, int size)
  1247. {
  1248. struct printk_info info;
  1249. struct printk_record r;
  1250. char *text;
  1251. int len = 0;
  1252. u64 seq;
  1253. text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL);
  1254. if (!text)
  1255. return -ENOMEM;
  1256. prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
  1257. mutex_lock(&syslog_lock);
  1258. /*
  1259. * Wait for the @syslog_seq record to be available. @syslog_seq may
  1260. * change while waiting.
  1261. */
  1262. do {
  1263. seq = syslog_seq;
  1264. mutex_unlock(&syslog_lock);
  1265. /*
  1266. * Guarantee this task is visible on the waitqueue before
  1267. * checking the wake condition.
  1268. *
  1269. * The full memory barrier within set_current_state() of
  1270. * prepare_to_wait_event() pairs with the full memory barrier
  1271. * within wq_has_sleeper().
  1272. *
  1273. * This pairs with __wake_up_klogd:A.
  1274. */
  1275. len = wait_event_interruptible(log_wait,
  1276. prb_read_valid(prb, seq, NULL)); /* LMM(syslog_print:A) */
  1277. mutex_lock(&syslog_lock);
  1278. if (len)
  1279. goto out;
  1280. } while (syslog_seq != seq);
  1281. /*
  1282. * Copy records that fit into the buffer. The above cycle makes sure
  1283. * that the first record is always available.
  1284. */
  1285. do {
  1286. size_t n;
  1287. size_t skip;
  1288. int err;
  1289. if (!prb_read_valid(prb, syslog_seq, &r))
  1290. break;
  1291. if (r.info->seq != syslog_seq) {
  1292. /* message is gone, move to next valid one */
  1293. syslog_seq = r.info->seq;
  1294. syslog_partial = 0;
  1295. }
  1296. /*
  1297. * To keep reading/counting partial line consistent,
  1298. * use printk_time value as of the beginning of a line.
  1299. */
  1300. if (!syslog_partial)
  1301. syslog_time = printk_time;
  1302. skip = syslog_partial;
  1303. n = record_print_text(&r, true, syslog_time);
  1304. if (n - syslog_partial <= size) {
  1305. /* message fits into buffer, move forward */
  1306. syslog_seq = r.info->seq + 1;
  1307. n -= syslog_partial;
  1308. syslog_partial = 0;
  1309. } else if (!len){
  1310. /* partial read(), remember position */
  1311. n = size;
  1312. syslog_partial += n;
  1313. } else
  1314. n = 0;
  1315. if (!n)
  1316. break;
  1317. mutex_unlock(&syslog_lock);
  1318. err = copy_to_user(buf, text + skip, n);
  1319. mutex_lock(&syslog_lock);
  1320. if (err) {
  1321. if (!len)
  1322. len = -EFAULT;
  1323. break;
  1324. }
  1325. len += n;
  1326. size -= n;
  1327. buf += n;
  1328. } while (size);
  1329. out:
  1330. mutex_unlock(&syslog_lock);
  1331. kfree(text);
  1332. return len;
  1333. }
  1334. static int syslog_print_all(char __user *buf, int size, bool clear)
  1335. {
  1336. struct printk_info info;
  1337. struct printk_record r;
  1338. char *text;
  1339. int len = 0;
  1340. u64 seq;
  1341. bool time;
  1342. text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL);
  1343. if (!text)
  1344. return -ENOMEM;
  1345. time = printk_time;
  1346. /*
  1347. * Find first record that fits, including all following records,
  1348. * into the user-provided buffer for this dump.
  1349. */
  1350. seq = find_first_fitting_seq(latched_seq_read_nolock(&clear_seq), -1,
  1351. size, true, time);
  1352. prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
  1353. len = 0;
  1354. prb_for_each_record(seq, prb, seq, &r) {
  1355. int textlen;
  1356. textlen = record_print_text(&r, true, time);
  1357. if (len + textlen > size) {
  1358. seq--;
  1359. break;
  1360. }
  1361. if (copy_to_user(buf + len, text, textlen))
  1362. len = -EFAULT;
  1363. else
  1364. len += textlen;
  1365. if (len < 0)
  1366. break;
  1367. }
  1368. if (clear) {
  1369. mutex_lock(&syslog_lock);
  1370. latched_seq_write(&clear_seq, seq);
  1371. mutex_unlock(&syslog_lock);
  1372. }
  1373. kfree(text);
  1374. return len;
  1375. }
  1376. static void syslog_clear(void)
  1377. {
  1378. mutex_lock(&syslog_lock);
  1379. latched_seq_write(&clear_seq, prb_next_seq(prb));
  1380. mutex_unlock(&syslog_lock);
  1381. }
  1382. int do_syslog(int type, char __user *buf, int len, int source)
  1383. {
  1384. struct printk_info info;
  1385. bool clear = false;
  1386. static int saved_console_loglevel = LOGLEVEL_DEFAULT;
  1387. int error;
  1388. error = check_syslog_permissions(type, source);
  1389. if (error)
  1390. return error;
  1391. switch (type) {
  1392. case SYSLOG_ACTION_CLOSE: /* Close log */
  1393. break;
  1394. case SYSLOG_ACTION_OPEN: /* Open log */
  1395. break;
  1396. case SYSLOG_ACTION_READ: /* Read from log */
  1397. if (!buf || len < 0)
  1398. return -EINVAL;
  1399. if (!len)
  1400. return 0;
  1401. if (!access_ok(buf, len))
  1402. return -EFAULT;
  1403. error = syslog_print(buf, len);
  1404. break;
  1405. /* Read/clear last kernel messages */
  1406. case SYSLOG_ACTION_READ_CLEAR:
  1407. clear = true;
  1408. fallthrough;
  1409. /* Read last kernel messages */
  1410. case SYSLOG_ACTION_READ_ALL:
  1411. if (!buf || len < 0)
  1412. return -EINVAL;
  1413. if (!len)
  1414. return 0;
  1415. if (!access_ok(buf, len))
  1416. return -EFAULT;
  1417. error = syslog_print_all(buf, len, clear);
  1418. break;
  1419. /* Clear ring buffer */
  1420. case SYSLOG_ACTION_CLEAR:
  1421. syslog_clear();
  1422. break;
  1423. /* Disable logging to console */
  1424. case SYSLOG_ACTION_CONSOLE_OFF:
  1425. if (saved_console_loglevel == LOGLEVEL_DEFAULT)
  1426. saved_console_loglevel = console_loglevel;
  1427. console_loglevel = minimum_console_loglevel;
  1428. break;
  1429. /* Enable logging to console */
  1430. case SYSLOG_ACTION_CONSOLE_ON:
  1431. if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
  1432. console_loglevel = saved_console_loglevel;
  1433. saved_console_loglevel = LOGLEVEL_DEFAULT;
  1434. }
  1435. break;
  1436. /* Set level of messages printed to console */
  1437. case SYSLOG_ACTION_CONSOLE_LEVEL:
  1438. if (len < 1 || len > 8)
  1439. return -EINVAL;
  1440. if (len < minimum_console_loglevel)
  1441. len = minimum_console_loglevel;
  1442. console_loglevel = len;
  1443. /* Implicitly re-enable logging to console */
  1444. saved_console_loglevel = LOGLEVEL_DEFAULT;
  1445. break;
  1446. /* Number of chars in the log buffer */
  1447. case SYSLOG_ACTION_SIZE_UNREAD:
  1448. mutex_lock(&syslog_lock);
  1449. if (!prb_read_valid_info(prb, syslog_seq, &info, NULL)) {
  1450. /* No unread messages. */
  1451. mutex_unlock(&syslog_lock);
  1452. return 0;
  1453. }
  1454. if (info.seq != syslog_seq) {
  1455. /* messages are gone, move to first one */
  1456. syslog_seq = info.seq;
  1457. syslog_partial = 0;
  1458. }
  1459. if (source == SYSLOG_FROM_PROC) {
  1460. /*
  1461. * Short-cut for poll(/"proc/kmsg") which simply checks
  1462. * for pending data, not the size; return the count of
  1463. * records, not the length.
  1464. */
  1465. error = prb_next_seq(prb) - syslog_seq;
  1466. } else {
  1467. bool time = syslog_partial ? syslog_time : printk_time;
  1468. unsigned int line_count;
  1469. u64 seq;
  1470. prb_for_each_info(syslog_seq, prb, seq, &info,
  1471. &line_count) {
  1472. error += get_record_print_text_size(&info, line_count,
  1473. true, time);
  1474. time = printk_time;
  1475. }
  1476. error -= syslog_partial;
  1477. }
  1478. mutex_unlock(&syslog_lock);
  1479. break;
  1480. /* Size of the log buffer */
  1481. case SYSLOG_ACTION_SIZE_BUFFER:
  1482. error = log_buf_len;
  1483. break;
  1484. default:
  1485. error = -EINVAL;
  1486. break;
  1487. }
  1488. return error;
  1489. }
  1490. SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
  1491. {
  1492. return do_syslog(type, buf, len, SYSLOG_FROM_READER);
  1493. }
  1494. /*
  1495. * Special console_lock variants that help to reduce the risk of soft-lockups.
  1496. * They allow to pass console_lock to another printk() call using a busy wait.
  1497. */
  1498. #ifdef CONFIG_LOCKDEP
  1499. static struct lockdep_map console_owner_dep_map = {
  1500. .name = "console_owner"
  1501. };
  1502. #endif
  1503. static DEFINE_RAW_SPINLOCK(console_owner_lock);
  1504. static struct task_struct *console_owner;
  1505. static bool console_waiter;
  1506. /**
  1507. * console_lock_spinning_enable - mark beginning of code where another
  1508. * thread might safely busy wait
  1509. *
  1510. * This basically converts console_lock into a spinlock. This marks
  1511. * the section where the console_lock owner can not sleep, because
  1512. * there may be a waiter spinning (like a spinlock). Also it must be
  1513. * ready to hand over the lock at the end of the section.
  1514. */
  1515. static void console_lock_spinning_enable(void)
  1516. {
  1517. raw_spin_lock(&console_owner_lock);
  1518. console_owner = current;
  1519. raw_spin_unlock(&console_owner_lock);
  1520. /* The waiter may spin on us after setting console_owner */
  1521. spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
  1522. }
  1523. /**
  1524. * console_lock_spinning_disable_and_check - mark end of code where another
  1525. * thread was able to busy wait and check if there is a waiter
  1526. *
  1527. * This is called at the end of the section where spinning is allowed.
  1528. * It has two functions. First, it is a signal that it is no longer
  1529. * safe to start busy waiting for the lock. Second, it checks if
  1530. * there is a busy waiter and passes the lock rights to her.
  1531. *
  1532. * Important: Callers lose the lock if there was a busy waiter.
  1533. * They must not touch items synchronized by console_lock
  1534. * in this case.
  1535. *
  1536. * Return: 1 if the lock rights were passed, 0 otherwise.
  1537. */
  1538. static int console_lock_spinning_disable_and_check(void)
  1539. {
  1540. int waiter;
  1541. raw_spin_lock(&console_owner_lock);
  1542. waiter = READ_ONCE(console_waiter);
  1543. console_owner = NULL;
  1544. raw_spin_unlock(&console_owner_lock);
  1545. if (!waiter) {
  1546. spin_release(&console_owner_dep_map, _THIS_IP_);
  1547. return 0;
  1548. }
  1549. /* The waiter is now free to continue */
  1550. WRITE_ONCE(console_waiter, false);
  1551. spin_release(&console_owner_dep_map, _THIS_IP_);
  1552. /*
  1553. * Hand off console_lock to waiter. The waiter will perform
  1554. * the up(). After this, the waiter is the console_lock owner.
  1555. */
  1556. mutex_release(&console_lock_dep_map, _THIS_IP_);
  1557. return 1;
  1558. }
  1559. /**
  1560. * console_trylock_spinning - try to get console_lock by busy waiting
  1561. *
  1562. * This allows to busy wait for the console_lock when the current
  1563. * owner is running in specially marked sections. It means that
  1564. * the current owner is running and cannot reschedule until it
  1565. * is ready to lose the lock.
  1566. *
  1567. * Return: 1 if we got the lock, 0 othrewise
  1568. */
  1569. static int console_trylock_spinning(void)
  1570. {
  1571. struct task_struct *owner = NULL;
  1572. bool waiter;
  1573. bool spin = false;
  1574. unsigned long flags;
  1575. if (console_trylock())
  1576. return 1;
  1577. /*
  1578. * It's unsafe to spin once a panic has begun. If we are the
  1579. * panic CPU, we may have already halted the owner of the
  1580. * console_sem. If we are not the panic CPU, then we should
  1581. * avoid taking console_sem, so the panic CPU has a better
  1582. * chance of cleanly acquiring it later.
  1583. */
  1584. if (panic_in_progress())
  1585. return 0;
  1586. printk_safe_enter_irqsave(flags);
  1587. raw_spin_lock(&console_owner_lock);
  1588. owner = READ_ONCE(console_owner);
  1589. waiter = READ_ONCE(console_waiter);
  1590. if (!waiter && owner && owner != current) {
  1591. WRITE_ONCE(console_waiter, true);
  1592. spin = true;
  1593. }
  1594. raw_spin_unlock(&console_owner_lock);
  1595. /*
  1596. * If there is an active printk() writing to the
  1597. * consoles, instead of having it write our data too,
  1598. * see if we can offload that load from the active
  1599. * printer, and do some printing ourselves.
  1600. * Go into a spin only if there isn't already a waiter
  1601. * spinning, and there is an active printer, and
  1602. * that active printer isn't us (recursive printk?).
  1603. */
  1604. if (!spin) {
  1605. printk_safe_exit_irqrestore(flags);
  1606. return 0;
  1607. }
  1608. /* We spin waiting for the owner to release us */
  1609. spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
  1610. /* Owner will clear console_waiter on hand off */
  1611. while (READ_ONCE(console_waiter))
  1612. cpu_relax();
  1613. spin_release(&console_owner_dep_map, _THIS_IP_);
  1614. printk_safe_exit_irqrestore(flags);
  1615. /*
  1616. * The owner passed the console lock to us.
  1617. * Since we did not spin on console lock, annotate
  1618. * this as a trylock. Otherwise lockdep will
  1619. * complain.
  1620. */
  1621. mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_);
  1622. return 1;
  1623. }
  1624. /*
  1625. * Call the specified console driver, asking it to write out the specified
  1626. * text and length. If @dropped_text is non-NULL and any records have been
  1627. * dropped, a dropped message will be written out first.
  1628. */
  1629. static void call_console_driver(struct console *con, const char *text, size_t len,
  1630. char *dropped_text)
  1631. {
  1632. size_t dropped_len;
  1633. if (con->dropped && dropped_text) {
  1634. dropped_len = snprintf(dropped_text, DROPPED_TEXT_MAX,
  1635. "** %lu printk messages dropped **\n",
  1636. con->dropped);
  1637. con->dropped = 0;
  1638. con->write(con, dropped_text, dropped_len);
  1639. }
  1640. con->write(con, text, len);
  1641. }
  1642. /*
  1643. * Recursion is tracked separately on each CPU. If NMIs are supported, an
  1644. * additional NMI context per CPU is also separately tracked. Until per-CPU
  1645. * is available, a separate "early tracking" is performed.
  1646. */
  1647. static DEFINE_PER_CPU(u8, printk_count);
  1648. static u8 printk_count_early;
  1649. #ifdef CONFIG_HAVE_NMI
  1650. static DEFINE_PER_CPU(u8, printk_count_nmi);
  1651. static u8 printk_count_nmi_early;
  1652. #endif
  1653. /*
  1654. * Recursion is limited to keep the output sane. printk() should not require
  1655. * more than 1 level of recursion (allowing, for example, printk() to trigger
  1656. * a WARN), but a higher value is used in case some printk-internal errors
  1657. * exist, such as the ringbuffer validation checks failing.
  1658. */
  1659. #define PRINTK_MAX_RECURSION 3
  1660. /*
  1661. * Return a pointer to the dedicated counter for the CPU+context of the
  1662. * caller.
  1663. */
  1664. static u8 *__printk_recursion_counter(void)
  1665. {
  1666. #ifdef CONFIG_HAVE_NMI
  1667. if (in_nmi()) {
  1668. if (printk_percpu_data_ready())
  1669. return this_cpu_ptr(&printk_count_nmi);
  1670. return &printk_count_nmi_early;
  1671. }
  1672. #endif
  1673. if (printk_percpu_data_ready())
  1674. return this_cpu_ptr(&printk_count);
  1675. return &printk_count_early;
  1676. }
  1677. /*
  1678. * Enter recursion tracking. Interrupts are disabled to simplify tracking.
  1679. * The caller must check the boolean return value to see if the recursion is
  1680. * allowed. On failure, interrupts are not disabled.
  1681. *
  1682. * @recursion_ptr must be a variable of type (u8 *) and is the same variable
  1683. * that is passed to printk_exit_irqrestore().
  1684. */
  1685. #define printk_enter_irqsave(recursion_ptr, flags) \
  1686. ({ \
  1687. bool success = true; \
  1688. \
  1689. typecheck(u8 *, recursion_ptr); \
  1690. local_irq_save(flags); \
  1691. (recursion_ptr) = __printk_recursion_counter(); \
  1692. if (*(recursion_ptr) > PRINTK_MAX_RECURSION) { \
  1693. local_irq_restore(flags); \
  1694. success = false; \
  1695. } else { \
  1696. (*(recursion_ptr))++; \
  1697. } \
  1698. success; \
  1699. })
  1700. /* Exit recursion tracking, restoring interrupts. */
  1701. #define printk_exit_irqrestore(recursion_ptr, flags) \
  1702. do { \
  1703. typecheck(u8 *, recursion_ptr); \
  1704. (*(recursion_ptr))--; \
  1705. local_irq_restore(flags); \
  1706. } while (0)
  1707. int printk_delay_msec __read_mostly;
  1708. static inline void printk_delay(int level)
  1709. {
  1710. boot_delay_msec(level);
  1711. if (unlikely(printk_delay_msec)) {
  1712. int m = printk_delay_msec;
  1713. while (m--) {
  1714. mdelay(1);
  1715. touch_nmi_watchdog();
  1716. }
  1717. }
  1718. }
  1719. static inline u32 printk_caller_id(void)
  1720. {
  1721. return in_task() ? task_pid_nr(current) :
  1722. 0x80000000 + smp_processor_id();
  1723. }
  1724. /**
  1725. * printk_parse_prefix - Parse level and control flags.
  1726. *
  1727. * @text: The terminated text message.
  1728. * @level: A pointer to the current level value, will be updated.
  1729. * @flags: A pointer to the current printk_info flags, will be updated.
  1730. *
  1731. * @level may be NULL if the caller is not interested in the parsed value.
  1732. * Otherwise the variable pointed to by @level must be set to
  1733. * LOGLEVEL_DEFAULT in order to be updated with the parsed value.
  1734. *
  1735. * @flags may be NULL if the caller is not interested in the parsed value.
  1736. * Otherwise the variable pointed to by @flags will be OR'd with the parsed
  1737. * value.
  1738. *
  1739. * Return: The length of the parsed level and control flags.
  1740. */
  1741. u16 printk_parse_prefix(const char *text, int *level,
  1742. enum printk_info_flags *flags)
  1743. {
  1744. u16 prefix_len = 0;
  1745. int kern_level;
  1746. while (*text) {
  1747. kern_level = printk_get_level(text);
  1748. if (!kern_level)
  1749. break;
  1750. switch (kern_level) {
  1751. case '0' ... '7':
  1752. if (level && *level == LOGLEVEL_DEFAULT)
  1753. *level = kern_level - '0';
  1754. break;
  1755. case 'c': /* KERN_CONT */
  1756. if (flags)
  1757. *flags |= LOG_CONT;
  1758. }
  1759. prefix_len += 2;
  1760. text += 2;
  1761. }
  1762. return prefix_len;
  1763. }
  1764. __printf(5, 0)
  1765. static u16 printk_sprint(char *text, u16 size, int facility,
  1766. enum printk_info_flags *flags, const char *fmt,
  1767. va_list args)
  1768. {
  1769. u16 text_len;
  1770. text_len = vscnprintf(text, size, fmt, args);
  1771. /* Mark and strip a trailing newline. */
  1772. if (text_len && text[text_len - 1] == '\n') {
  1773. text_len--;
  1774. *flags |= LOG_NEWLINE;
  1775. }
  1776. /* Strip log level and control flags. */
  1777. if (facility == 0) {
  1778. u16 prefix_len;
  1779. prefix_len = printk_parse_prefix(text, NULL, NULL);
  1780. if (prefix_len) {
  1781. text_len -= prefix_len;
  1782. memmove(text, text + prefix_len, text_len);
  1783. }
  1784. }
  1785. trace_console_rcuidle(text, text_len);
  1786. return text_len;
  1787. }
  1788. EXPORT_TRACEPOINT_SYMBOL_GPL(console);
  1789. __printf(4, 0)
  1790. int vprintk_store(int facility, int level,
  1791. const struct dev_printk_info *dev_info,
  1792. const char *fmt, va_list args)
  1793. {
  1794. struct prb_reserved_entry e;
  1795. enum printk_info_flags flags = 0;
  1796. struct printk_record r;
  1797. unsigned long irqflags;
  1798. u16 trunc_msg_len = 0;
  1799. char prefix_buf[8];
  1800. u8 *recursion_ptr;
  1801. u16 reserve_size;
  1802. va_list args2;
  1803. u32 caller_id;
  1804. u16 text_len;
  1805. int ret = 0;
  1806. u64 ts_nsec;
  1807. if (!printk_enter_irqsave(recursion_ptr, irqflags))
  1808. return 0;
  1809. /*
  1810. * Since the duration of printk() can vary depending on the message
  1811. * and state of the ringbuffer, grab the timestamp now so that it is
  1812. * close to the call of printk(). This provides a more deterministic
  1813. * timestamp with respect to the caller.
  1814. */
  1815. ts_nsec = local_clock();
  1816. caller_id = printk_caller_id();
  1817. /*
  1818. * The sprintf needs to come first since the syslog prefix might be
  1819. * passed in as a parameter. An extra byte must be reserved so that
  1820. * later the vscnprintf() into the reserved buffer has room for the
  1821. * terminating '\0', which is not counted by vsnprintf().
  1822. */
  1823. va_copy(args2, args);
  1824. reserve_size = vsnprintf(&prefix_buf[0], sizeof(prefix_buf), fmt, args2) + 1;
  1825. va_end(args2);
  1826. if (reserve_size > LOG_LINE_MAX)
  1827. reserve_size = LOG_LINE_MAX;
  1828. /* Extract log level or control flags. */
  1829. if (facility == 0)
  1830. printk_parse_prefix(&prefix_buf[0], &level, &flags);
  1831. if (level == LOGLEVEL_DEFAULT)
  1832. level = default_message_loglevel;
  1833. if (dev_info)
  1834. flags |= LOG_NEWLINE;
  1835. if (flags & LOG_CONT) {
  1836. prb_rec_init_wr(&r, reserve_size);
  1837. if (prb_reserve_in_last(&e, prb, &r, caller_id, LOG_LINE_MAX)) {
  1838. text_len = printk_sprint(&r.text_buf[r.info->text_len], reserve_size,
  1839. facility, &flags, fmt, args);
  1840. r.info->text_len += text_len;
  1841. if (flags & LOG_NEWLINE) {
  1842. r.info->flags |= LOG_NEWLINE;
  1843. prb_final_commit(&e);
  1844. } else {
  1845. prb_commit(&e);
  1846. }
  1847. ret = text_len;
  1848. goto out;
  1849. }
  1850. }
  1851. /*
  1852. * Explicitly initialize the record before every prb_reserve() call.
  1853. * prb_reserve_in_last() and prb_reserve() purposely invalidate the
  1854. * structure when they fail.
  1855. */
  1856. prb_rec_init_wr(&r, reserve_size);
  1857. if (!prb_reserve(&e, prb, &r)) {
  1858. /* truncate the message if it is too long for empty buffer */
  1859. truncate_msg(&reserve_size, &trunc_msg_len);
  1860. prb_rec_init_wr(&r, reserve_size + trunc_msg_len);
  1861. if (!prb_reserve(&e, prb, &r))
  1862. goto out;
  1863. }
  1864. /* fill message */
  1865. text_len = printk_sprint(&r.text_buf[0], reserve_size, facility, &flags, fmt, args);
  1866. if (trunc_msg_len)
  1867. memcpy(&r.text_buf[text_len], trunc_msg, trunc_msg_len);
  1868. r.info->text_len = text_len + trunc_msg_len;
  1869. r.info->facility = facility;
  1870. r.info->level = level & 7;
  1871. r.info->flags = flags & 0x1f;
  1872. r.info->ts_nsec = ts_nsec;
  1873. r.info->caller_id = caller_id;
  1874. if (dev_info)
  1875. memcpy(&r.info->dev_info, dev_info, sizeof(r.info->dev_info));
  1876. /* A message without a trailing newline can be continued. */
  1877. if (!(flags & LOG_NEWLINE))
  1878. prb_commit(&e);
  1879. else
  1880. prb_final_commit(&e);
  1881. ret = text_len + trunc_msg_len;
  1882. out:
  1883. printk_exit_irqrestore(recursion_ptr, irqflags);
  1884. return ret;
  1885. }
  1886. asmlinkage int vprintk_emit(int facility, int level,
  1887. const struct dev_printk_info *dev_info,
  1888. const char *fmt, va_list args)
  1889. {
  1890. int printed_len;
  1891. bool in_sched = false;
  1892. /* Suppress unimportant messages after panic happens */
  1893. if (unlikely(suppress_printk))
  1894. return 0;
  1895. if (unlikely(suppress_panic_printk) &&
  1896. atomic_read(&panic_cpu) != raw_smp_processor_id())
  1897. return 0;
  1898. if (level == LOGLEVEL_SCHED) {
  1899. level = LOGLEVEL_DEFAULT;
  1900. in_sched = true;
  1901. }
  1902. printk_delay(level);
  1903. printed_len = vprintk_store(facility, level, dev_info, fmt, args);
  1904. /* If called from the scheduler, we can not call up(). */
  1905. if (!in_sched) {
  1906. /*
  1907. * The caller may be holding system-critical or
  1908. * timing-sensitive locks. Disable preemption during
  1909. * printing of all remaining records to all consoles so that
  1910. * this context can return as soon as possible. Hopefully
  1911. * another printk() caller will take over the printing.
  1912. */
  1913. preempt_disable();
  1914. /*
  1915. * Try to acquire and then immediately release the console
  1916. * semaphore. The release will print out buffers. With the
  1917. * spinning variant, this context tries to take over the
  1918. * printing from another printing context.
  1919. */
  1920. if (console_trylock_spinning())
  1921. console_unlock();
  1922. preempt_enable();
  1923. }
  1924. if (in_sched)
  1925. defer_console_output();
  1926. else
  1927. wake_up_klogd();
  1928. return printed_len;
  1929. }
  1930. EXPORT_SYMBOL(vprintk_emit);
  1931. int vprintk_default(const char *fmt, va_list args)
  1932. {
  1933. return vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
  1934. }
  1935. EXPORT_SYMBOL_GPL(vprintk_default);
  1936. asmlinkage __visible int _printk(const char *fmt, ...)
  1937. {
  1938. va_list args;
  1939. int r;
  1940. va_start(args, fmt);
  1941. r = vprintk(fmt, args);
  1942. va_end(args);
  1943. return r;
  1944. }
  1945. EXPORT_SYMBOL(_printk);
  1946. static bool pr_flush(int timeout_ms, bool reset_on_progress);
  1947. static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress);
  1948. #else /* CONFIG_PRINTK */
  1949. #define CONSOLE_LOG_MAX 0
  1950. #define DROPPED_TEXT_MAX 0
  1951. #define printk_time false
  1952. #define prb_read_valid(rb, seq, r) false
  1953. #define prb_first_valid_seq(rb) 0
  1954. #define prb_next_seq(rb) 0
  1955. static u64 syslog_seq;
  1956. static size_t record_print_text(const struct printk_record *r,
  1957. bool syslog, bool time)
  1958. {
  1959. return 0;
  1960. }
  1961. static ssize_t info_print_ext_header(char *buf, size_t size,
  1962. struct printk_info *info)
  1963. {
  1964. return 0;
  1965. }
  1966. static ssize_t msg_print_ext_body(char *buf, size_t size,
  1967. char *text, size_t text_len,
  1968. struct dev_printk_info *dev_info) { return 0; }
  1969. static void console_lock_spinning_enable(void) { }
  1970. static int console_lock_spinning_disable_and_check(void) { return 0; }
  1971. static void call_console_driver(struct console *con, const char *text, size_t len,
  1972. char *dropped_text)
  1973. {
  1974. }
  1975. static bool suppress_message_printing(int level) { return false; }
  1976. static bool pr_flush(int timeout_ms, bool reset_on_progress) { return true; }
  1977. static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; }
  1978. #endif /* CONFIG_PRINTK */
  1979. #ifdef CONFIG_EARLY_PRINTK
  1980. struct console *early_console;
  1981. asmlinkage __visible void early_printk(const char *fmt, ...)
  1982. {
  1983. va_list ap;
  1984. char buf[512];
  1985. int n;
  1986. if (!early_console)
  1987. return;
  1988. va_start(ap, fmt);
  1989. n = vscnprintf(buf, sizeof(buf), fmt, ap);
  1990. va_end(ap);
  1991. early_console->write(early_console, buf, n);
  1992. }
  1993. #endif
  1994. static void set_user_specified(struct console_cmdline *c, bool user_specified)
  1995. {
  1996. if (!user_specified)
  1997. return;
  1998. /*
  1999. * @c console was defined by the user on the command line.
  2000. * Do not clear when added twice also by SPCR or the device tree.
  2001. */
  2002. c->user_specified = true;
  2003. /* At least one console defined by the user on the command line. */
  2004. console_set_on_cmdline = 1;
  2005. }
  2006. static int __add_preferred_console(char *name, int idx, char *options,
  2007. char *brl_options, bool user_specified)
  2008. {
  2009. struct console_cmdline *c;
  2010. int i;
  2011. /*
  2012. * See if this tty is not yet registered, and
  2013. * if we have a slot free.
  2014. */
  2015. for (i = 0, c = console_cmdline;
  2016. i < MAX_CMDLINECONSOLES && c->name[0];
  2017. i++, c++) {
  2018. if (strcmp(c->name, name) == 0 && c->index == idx) {
  2019. if (!brl_options)
  2020. preferred_console = i;
  2021. set_user_specified(c, user_specified);
  2022. return 0;
  2023. }
  2024. }
  2025. if (i == MAX_CMDLINECONSOLES)
  2026. return -E2BIG;
  2027. if (!brl_options)
  2028. preferred_console = i;
  2029. strlcpy(c->name, name, sizeof(c->name));
  2030. c->options = options;
  2031. set_user_specified(c, user_specified);
  2032. braille_set_options(c, brl_options);
  2033. c->index = idx;
  2034. return 0;
  2035. }
  2036. static int __init console_msg_format_setup(char *str)
  2037. {
  2038. if (!strcmp(str, "syslog"))
  2039. console_msg_format = MSG_FORMAT_SYSLOG;
  2040. if (!strcmp(str, "default"))
  2041. console_msg_format = MSG_FORMAT_DEFAULT;
  2042. return 1;
  2043. }
  2044. __setup("console_msg_format=", console_msg_format_setup);
  2045. /*
  2046. * Set up a console. Called via do_early_param() in init/main.c
  2047. * for each "console=" parameter in the boot command line.
  2048. */
  2049. static int __init console_setup(char *str)
  2050. {
  2051. char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */
  2052. char *s, *options, *brl_options = NULL;
  2053. int idx;
  2054. /*
  2055. * console="" or console=null have been suggested as a way to
  2056. * disable console output. Use ttynull that has been created
  2057. * for exactly this purpose.
  2058. */
  2059. if (str[0] == 0 || strcmp(str, "null") == 0) {
  2060. __add_preferred_console("ttynull", 0, NULL, NULL, true);
  2061. return 1;
  2062. }
  2063. if (_braille_console_setup(&str, &brl_options))
  2064. return 1;
  2065. /*
  2066. * Decode str into name, index, options.
  2067. */
  2068. if (str[0] >= '0' && str[0] <= '9') {
  2069. strcpy(buf, "ttyS");
  2070. strncpy(buf + 4, str, sizeof(buf) - 5);
  2071. } else {
  2072. strncpy(buf, str, sizeof(buf) - 1);
  2073. }
  2074. buf[sizeof(buf) - 1] = 0;
  2075. options = strchr(str, ',');
  2076. if (options)
  2077. *(options++) = 0;
  2078. #ifdef __sparc__
  2079. if (!strcmp(str, "ttya"))
  2080. strcpy(buf, "ttyS0");
  2081. if (!strcmp(str, "ttyb"))
  2082. strcpy(buf, "ttyS1");
  2083. #endif
  2084. for (s = buf; *s; s++)
  2085. if (isdigit(*s) || *s == ',')
  2086. break;
  2087. idx = simple_strtoul(s, NULL, 10);
  2088. *s = 0;
  2089. __add_preferred_console(buf, idx, options, brl_options, true);
  2090. return 1;
  2091. }
  2092. __setup("console=", console_setup);
  2093. /**
  2094. * add_preferred_console - add a device to the list of preferred consoles.
  2095. * @name: device name
  2096. * @idx: device index
  2097. * @options: options for this console
  2098. *
  2099. * The last preferred console added will be used for kernel messages
  2100. * and stdin/out/err for init. Normally this is used by console_setup
  2101. * above to handle user-supplied console arguments; however it can also
  2102. * be used by arch-specific code either to override the user or more
  2103. * commonly to provide a default console (ie from PROM variables) when
  2104. * the user has not supplied one.
  2105. */
  2106. int add_preferred_console(char *name, int idx, char *options)
  2107. {
  2108. return __add_preferred_console(name, idx, options, NULL, false);
  2109. }
  2110. bool console_suspend_enabled = true;
  2111. EXPORT_SYMBOL(console_suspend_enabled);
  2112. static int __init console_suspend_disable(char *str)
  2113. {
  2114. console_suspend_enabled = false;
  2115. return 1;
  2116. }
  2117. __setup("no_console_suspend", console_suspend_disable);
  2118. module_param_named(console_suspend, console_suspend_enabled,
  2119. bool, S_IRUGO | S_IWUSR);
  2120. MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
  2121. " and hibernate operations");
  2122. static bool printk_console_no_auto_verbose;
  2123. void console_verbose(void)
  2124. {
  2125. if (console_loglevel && !printk_console_no_auto_verbose)
  2126. console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
  2127. }
  2128. EXPORT_SYMBOL_GPL(console_verbose);
  2129. module_param_named(console_no_auto_verbose, printk_console_no_auto_verbose, bool, 0644);
  2130. MODULE_PARM_DESC(console_no_auto_verbose, "Disable console loglevel raise to highest on oops/panic/etc");
  2131. /**
  2132. * suspend_console - suspend the console subsystem
  2133. *
  2134. * This disables printk() while we go into suspend states
  2135. */
  2136. void suspend_console(void)
  2137. {
  2138. if (!console_suspend_enabled)
  2139. return;
  2140. pr_info("Suspending console(s) (use no_console_suspend to debug)\n");
  2141. pr_flush(1000, true);
  2142. console_lock();
  2143. console_suspended = 1;
  2144. up_console_sem();
  2145. }
  2146. void resume_console(void)
  2147. {
  2148. if (!console_suspend_enabled)
  2149. return;
  2150. down_console_sem();
  2151. console_suspended = 0;
  2152. console_unlock();
  2153. pr_flush(1000, true);
  2154. }
  2155. /**
  2156. * console_cpu_notify - print deferred console messages after CPU hotplug
  2157. * @cpu: unused
  2158. *
  2159. * If printk() is called from a CPU that is not online yet, the messages
  2160. * will be printed on the console only if there are CON_ANYTIME consoles.
  2161. * This function is called when a new CPU comes online (or fails to come
  2162. * up) or goes offline.
  2163. */
  2164. static int console_cpu_notify(unsigned int cpu)
  2165. {
  2166. int flag = 0;
  2167. trace_android_vh_printk_hotplug(&flag);
  2168. if (flag)
  2169. return 0;
  2170. if (!cpuhp_tasks_frozen) {
  2171. /* If trylock fails, someone else is doing the printing */
  2172. if (console_trylock())
  2173. console_unlock();
  2174. }
  2175. return 0;
  2176. }
  2177. /*
  2178. * Return true when this CPU should unlock console_sem without pushing all
  2179. * messages to the console. This reduces the chance that the console is
  2180. * locked when the panic CPU tries to use it.
  2181. */
  2182. static bool abandon_console_lock_in_panic(void)
  2183. {
  2184. if (!panic_in_progress())
  2185. return false;
  2186. /*
  2187. * We can use raw_smp_processor_id() here because it is impossible for
  2188. * the task to be migrated to the panic_cpu, or away from it. If
  2189. * panic_cpu has already been set, and we're not currently executing on
  2190. * that CPU, then we never will be.
  2191. */
  2192. return atomic_read(&panic_cpu) != raw_smp_processor_id();
  2193. }
  2194. /**
  2195. * console_lock - lock the console system for exclusive use.
  2196. *
  2197. * Acquires a lock which guarantees that the caller has
  2198. * exclusive access to the console system and the console_drivers list.
  2199. *
  2200. * Can sleep, returns nothing.
  2201. */
  2202. void console_lock(void)
  2203. {
  2204. might_sleep();
  2205. /* On panic, the console_lock must be left to the panic cpu. */
  2206. while (abandon_console_lock_in_panic())
  2207. msleep(1000);
  2208. down_console_sem();
  2209. if (console_suspended)
  2210. return;
  2211. console_locked = 1;
  2212. console_may_schedule = 1;
  2213. }
  2214. EXPORT_SYMBOL(console_lock);
  2215. /**
  2216. * console_trylock - try to lock the console system for exclusive use.
  2217. *
  2218. * Try to acquire a lock which guarantees that the caller has exclusive
  2219. * access to the console system and the console_drivers list.
  2220. *
  2221. * returns 1 on success, and 0 on failure to acquire the lock.
  2222. */
  2223. int console_trylock(void)
  2224. {
  2225. /* On panic, the console_lock must be left to the panic cpu. */
  2226. if (abandon_console_lock_in_panic())
  2227. return 0;
  2228. if (down_trylock_console_sem())
  2229. return 0;
  2230. if (console_suspended) {
  2231. up_console_sem();
  2232. return 0;
  2233. }
  2234. console_locked = 1;
  2235. console_may_schedule = 0;
  2236. return 1;
  2237. }
  2238. EXPORT_SYMBOL(console_trylock);
  2239. int is_console_locked(void)
  2240. {
  2241. return console_locked;
  2242. }
  2243. EXPORT_SYMBOL(is_console_locked);
  2244. /*
  2245. * Check if the given console is currently capable and allowed to print
  2246. * records.
  2247. *
  2248. * Requires the console_lock.
  2249. */
  2250. static inline bool console_is_usable(struct console *con)
  2251. {
  2252. if (!(con->flags & CON_ENABLED))
  2253. return false;
  2254. if (!con->write)
  2255. return false;
  2256. /*
  2257. * Console drivers may assume that per-cpu resources have been
  2258. * allocated. So unless they're explicitly marked as being able to
  2259. * cope (CON_ANYTIME) don't call them until this CPU is officially up.
  2260. */
  2261. if (!cpu_online(raw_smp_processor_id()) &&
  2262. !(con->flags & CON_ANYTIME))
  2263. return false;
  2264. return true;
  2265. }
  2266. static void __console_unlock(void)
  2267. {
  2268. console_locked = 0;
  2269. up_console_sem();
  2270. }
  2271. /*
  2272. * Print one record for the given console. The record printed is whatever
  2273. * record is the next available record for the given console.
  2274. *
  2275. * @text is a buffer of size CONSOLE_LOG_MAX.
  2276. *
  2277. * If extended messages should be printed, @ext_text is a buffer of size
  2278. * CONSOLE_EXT_LOG_MAX. Otherwise @ext_text must be NULL.
  2279. *
  2280. * If dropped messages should be printed, @dropped_text is a buffer of size
  2281. * DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL.
  2282. *
  2283. * @handover will be set to true if a printk waiter has taken over the
  2284. * console_lock, in which case the caller is no longer holding the
  2285. * console_lock. Otherwise it is set to false.
  2286. *
  2287. * Returns false if the given console has no next record to print, otherwise
  2288. * true.
  2289. *
  2290. * Requires the console_lock.
  2291. */
  2292. static bool console_emit_next_record(struct console *con, char *text, char *ext_text,
  2293. char *dropped_text, bool *handover)
  2294. {
  2295. static int panic_console_dropped;
  2296. struct printk_info info;
  2297. struct printk_record r;
  2298. unsigned long flags;
  2299. char *write_text;
  2300. size_t len;
  2301. prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
  2302. *handover = false;
  2303. if (!prb_read_valid(prb, con->seq, &r))
  2304. return false;
  2305. if (con->seq != r.info->seq) {
  2306. con->dropped += r.info->seq - con->seq;
  2307. con->seq = r.info->seq;
  2308. if (panic_in_progress() && panic_console_dropped++ > 10) {
  2309. suppress_panic_printk = 1;
  2310. pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n");
  2311. }
  2312. }
  2313. /* Skip record that has level above the console loglevel. */
  2314. if (suppress_message_printing(r.info->level)) {
  2315. con->seq++;
  2316. goto skip;
  2317. }
  2318. if (ext_text) {
  2319. write_text = ext_text;
  2320. len = info_print_ext_header(ext_text, CONSOLE_EXT_LOG_MAX, r.info);
  2321. len += msg_print_ext_body(ext_text + len, CONSOLE_EXT_LOG_MAX - len,
  2322. &r.text_buf[0], r.info->text_len, &r.info->dev_info);
  2323. } else {
  2324. write_text = text;
  2325. len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time);
  2326. }
  2327. /*
  2328. * While actively printing out messages, if another printk()
  2329. * were to occur on another CPU, it may wait for this one to
  2330. * finish. This task can not be preempted if there is a
  2331. * waiter waiting to take over.
  2332. *
  2333. * Interrupts are disabled because the hand over to a waiter
  2334. * must not be interrupted until the hand over is completed
  2335. * (@console_waiter is cleared).
  2336. */
  2337. printk_safe_enter_irqsave(flags);
  2338. console_lock_spinning_enable();
  2339. stop_critical_timings(); /* don't trace print latency */
  2340. call_console_driver(con, write_text, len, dropped_text);
  2341. start_critical_timings();
  2342. con->seq++;
  2343. *handover = console_lock_spinning_disable_and_check();
  2344. printk_safe_exit_irqrestore(flags);
  2345. skip:
  2346. return true;
  2347. }
  2348. /*
  2349. * Print out all remaining records to all consoles.
  2350. *
  2351. * @do_cond_resched is set by the caller. It can be true only in schedulable
  2352. * context.
  2353. *
  2354. * @next_seq is set to the sequence number after the last available record.
  2355. * The value is valid only when this function returns true. It means that all
  2356. * usable consoles are completely flushed.
  2357. *
  2358. * @handover will be set to true if a printk waiter has taken over the
  2359. * console_lock, in which case the caller is no longer holding the
  2360. * console_lock. Otherwise it is set to false.
  2361. *
  2362. * Returns true when there was at least one usable console and all messages
  2363. * were flushed to all usable consoles. A returned false informs the caller
  2364. * that everything was not flushed (either there were no usable consoles or
  2365. * another context has taken over printing or it is a panic situation and this
  2366. * is not the panic CPU). Regardless the reason, the caller should assume it
  2367. * is not useful to immediately try again.
  2368. *
  2369. * Requires the console_lock.
  2370. */
  2371. static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover)
  2372. {
  2373. static char dropped_text[DROPPED_TEXT_MAX];
  2374. static char ext_text[CONSOLE_EXT_LOG_MAX];
  2375. static char text[CONSOLE_LOG_MAX];
  2376. bool any_usable = false;
  2377. struct console *con;
  2378. bool any_progress;
  2379. *next_seq = 0;
  2380. *handover = false;
  2381. do {
  2382. any_progress = false;
  2383. for_each_console(con) {
  2384. bool progress;
  2385. if (!console_is_usable(con))
  2386. continue;
  2387. any_usable = true;
  2388. if (con->flags & CON_EXTENDED) {
  2389. /* Extended consoles do not print "dropped messages". */
  2390. progress = console_emit_next_record(con, &text[0],
  2391. &ext_text[0], NULL,
  2392. handover);
  2393. } else {
  2394. progress = console_emit_next_record(con, &text[0],
  2395. NULL, &dropped_text[0],
  2396. handover);
  2397. }
  2398. if (*handover)
  2399. return false;
  2400. /* Track the next of the highest seq flushed. */
  2401. if (con->seq > *next_seq)
  2402. *next_seq = con->seq;
  2403. if (!progress)
  2404. continue;
  2405. any_progress = true;
  2406. /* Allow panic_cpu to take over the consoles safely. */
  2407. if (abandon_console_lock_in_panic())
  2408. return false;
  2409. if (do_cond_resched)
  2410. cond_resched();
  2411. }
  2412. } while (any_progress);
  2413. return any_usable;
  2414. }
  2415. /**
  2416. * console_unlock - unlock the console system
  2417. *
  2418. * Releases the console_lock which the caller holds on the console system
  2419. * and the console driver list.
  2420. *
  2421. * While the console_lock was held, console output may have been buffered
  2422. * by printk(). If this is the case, console_unlock(); emits
  2423. * the output prior to releasing the lock.
  2424. *
  2425. * console_unlock(); may be called from any context.
  2426. */
  2427. void console_unlock(void)
  2428. {
  2429. bool do_cond_resched;
  2430. bool handover;
  2431. bool flushed;
  2432. u64 next_seq;
  2433. if (console_suspended) {
  2434. up_console_sem();
  2435. return;
  2436. }
  2437. /*
  2438. * Console drivers are called with interrupts disabled, so
  2439. * @console_may_schedule should be cleared before; however, we may
  2440. * end up dumping a lot of lines, for example, if called from
  2441. * console registration path, and should invoke cond_resched()
  2442. * between lines if allowable. Not doing so can cause a very long
  2443. * scheduling stall on a slow console leading to RCU stall and
  2444. * softlockup warnings which exacerbate the issue with more
  2445. * messages practically incapacitating the system. Therefore, create
  2446. * a local to use for the printing loop.
  2447. */
  2448. do_cond_resched = console_may_schedule;
  2449. do {
  2450. console_may_schedule = 0;
  2451. flushed = console_flush_all(do_cond_resched, &next_seq, &handover);
  2452. if (!handover)
  2453. __console_unlock();
  2454. /*
  2455. * Abort if there was a failure to flush all messages to all
  2456. * usable consoles. Either it is not possible to flush (in
  2457. * which case it would be an infinite loop of retrying) or
  2458. * another context has taken over printing.
  2459. */
  2460. if (!flushed)
  2461. break;
  2462. /*
  2463. * Some context may have added new records after
  2464. * console_flush_all() but before unlocking the console.
  2465. * Re-check if there is a new record to flush. If the trylock
  2466. * fails, another context is already handling the printing.
  2467. */
  2468. } while (prb_read_valid(prb, next_seq, NULL) && console_trylock());
  2469. }
  2470. EXPORT_SYMBOL(console_unlock);
  2471. /**
  2472. * console_conditional_schedule - yield the CPU if required
  2473. *
  2474. * If the console code is currently allowed to sleep, and
  2475. * if this CPU should yield the CPU to another task, do
  2476. * so here.
  2477. *
  2478. * Must be called within console_lock();.
  2479. */
  2480. void __sched console_conditional_schedule(void)
  2481. {
  2482. if (console_may_schedule)
  2483. cond_resched();
  2484. }
  2485. EXPORT_SYMBOL(console_conditional_schedule);
  2486. void console_unblank(void)
  2487. {
  2488. struct console *c;
  2489. /*
  2490. * console_unblank can no longer be called in interrupt context unless
  2491. * oops_in_progress is set to 1..
  2492. */
  2493. if (oops_in_progress) {
  2494. if (down_trylock_console_sem() != 0)
  2495. return;
  2496. } else
  2497. console_lock();
  2498. console_locked = 1;
  2499. console_may_schedule = 0;
  2500. for_each_console(c)
  2501. if ((c->flags & CON_ENABLED) && c->unblank)
  2502. c->unblank();
  2503. console_unlock();
  2504. if (!oops_in_progress)
  2505. pr_flush(1000, true);
  2506. }
  2507. /**
  2508. * console_flush_on_panic - flush console content on panic
  2509. * @mode: flush all messages in buffer or just the pending ones
  2510. *
  2511. * Immediately output all pending messages no matter what.
  2512. */
  2513. void console_flush_on_panic(enum con_flush_mode mode)
  2514. {
  2515. /*
  2516. * If someone else is holding the console lock, trylock will fail
  2517. * and may_schedule may be set. Ignore and proceed to unlock so
  2518. * that messages are flushed out. As this can be called from any
  2519. * context and we don't want to get preempted while flushing,
  2520. * ensure may_schedule is cleared.
  2521. */
  2522. console_trylock();
  2523. console_may_schedule = 0;
  2524. if (mode == CONSOLE_REPLAY_ALL) {
  2525. struct console *c;
  2526. u64 seq;
  2527. seq = prb_first_valid_seq(prb);
  2528. for_each_console(c)
  2529. c->seq = seq;
  2530. }
  2531. console_unlock();
  2532. }
  2533. /*
  2534. * Return the console tty driver structure and its associated index
  2535. */
  2536. struct tty_driver *console_device(int *index)
  2537. {
  2538. struct console *c;
  2539. struct tty_driver *driver = NULL;
  2540. console_lock();
  2541. for_each_console(c) {
  2542. if (!c->device)
  2543. continue;
  2544. driver = c->device(c, index);
  2545. if (driver)
  2546. break;
  2547. }
  2548. console_unlock();
  2549. return driver;
  2550. }
  2551. /*
  2552. * Prevent further output on the passed console device so that (for example)
  2553. * serial drivers can disable console output before suspending a port, and can
  2554. * re-enable output afterwards.
  2555. */
  2556. void console_stop(struct console *console)
  2557. {
  2558. __pr_flush(console, 1000, true);
  2559. console_lock();
  2560. console->flags &= ~CON_ENABLED;
  2561. console_unlock();
  2562. }
  2563. EXPORT_SYMBOL(console_stop);
  2564. void console_start(struct console *console)
  2565. {
  2566. console_lock();
  2567. console->flags |= CON_ENABLED;
  2568. console_unlock();
  2569. __pr_flush(console, 1000, true);
  2570. }
  2571. EXPORT_SYMBOL(console_start);
  2572. static int __read_mostly keep_bootcon;
  2573. static int __init keep_bootcon_setup(char *str)
  2574. {
  2575. keep_bootcon = 1;
  2576. pr_info("debug: skip boot console de-registration.\n");
  2577. return 0;
  2578. }
  2579. early_param("keep_bootcon", keep_bootcon_setup);
  2580. /*
  2581. * This is called by register_console() to try to match
  2582. * the newly registered console with any of the ones selected
  2583. * by either the command line or add_preferred_console() and
  2584. * setup/enable it.
  2585. *
  2586. * Care need to be taken with consoles that are statically
  2587. * enabled such as netconsole
  2588. */
  2589. static int try_enable_preferred_console(struct console *newcon,
  2590. bool user_specified)
  2591. {
  2592. struct console_cmdline *c;
  2593. int i, err;
  2594. for (i = 0, c = console_cmdline;
  2595. i < MAX_CMDLINECONSOLES && c->name[0];
  2596. i++, c++) {
  2597. if (c->user_specified != user_specified)
  2598. continue;
  2599. if (!newcon->match ||
  2600. newcon->match(newcon, c->name, c->index, c->options) != 0) {
  2601. /* default matching */
  2602. BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
  2603. if (strcmp(c->name, newcon->name) != 0)
  2604. continue;
  2605. if (newcon->index >= 0 &&
  2606. newcon->index != c->index)
  2607. continue;
  2608. if (newcon->index < 0)
  2609. newcon->index = c->index;
  2610. if (_braille_register_console(newcon, c))
  2611. return 0;
  2612. if (newcon->setup &&
  2613. (err = newcon->setup(newcon, c->options)) != 0)
  2614. return err;
  2615. }
  2616. newcon->flags |= CON_ENABLED;
  2617. if (i == preferred_console)
  2618. newcon->flags |= CON_CONSDEV;
  2619. return 0;
  2620. }
  2621. /*
  2622. * Some consoles, such as pstore and netconsole, can be enabled even
  2623. * without matching. Accept the pre-enabled consoles only when match()
  2624. * and setup() had a chance to be called.
  2625. */
  2626. if (newcon->flags & CON_ENABLED && c->user_specified == user_specified)
  2627. return 0;
  2628. return -ENOENT;
  2629. }
  2630. /* Try to enable the console unconditionally */
  2631. static void try_enable_default_console(struct console *newcon)
  2632. {
  2633. if (newcon->index < 0)
  2634. newcon->index = 0;
  2635. if (newcon->setup && newcon->setup(newcon, NULL) != 0)
  2636. return;
  2637. newcon->flags |= CON_ENABLED;
  2638. if (newcon->device)
  2639. newcon->flags |= CON_CONSDEV;
  2640. }
  2641. #define con_printk(lvl, con, fmt, ...) \
  2642. printk(lvl pr_fmt("%sconsole [%s%d] " fmt), \
  2643. (con->flags & CON_BOOT) ? "boot" : "", \
  2644. con->name, con->index, ##__VA_ARGS__)
  2645. /*
  2646. * The console driver calls this routine during kernel initialization
  2647. * to register the console printing procedure with printk() and to
  2648. * print any messages that were printed by the kernel before the
  2649. * console driver was initialized.
  2650. *
  2651. * This can happen pretty early during the boot process (because of
  2652. * early_printk) - sometimes before setup_arch() completes - be careful
  2653. * of what kernel features are used - they may not be initialised yet.
  2654. *
  2655. * There are two types of consoles - bootconsoles (early_printk) and
  2656. * "real" consoles (everything which is not a bootconsole) which are
  2657. * handled differently.
  2658. * - Any number of bootconsoles can be registered at any time.
  2659. * - As soon as a "real" console is registered, all bootconsoles
  2660. * will be unregistered automatically.
  2661. * - Once a "real" console is registered, any attempt to register a
  2662. * bootconsoles will be rejected
  2663. */
  2664. void register_console(struct console *newcon)
  2665. {
  2666. struct console *con;
  2667. bool bootcon_enabled = false;
  2668. bool realcon_enabled = false;
  2669. int err;
  2670. for_each_console(con) {
  2671. if (WARN(con == newcon, "console '%s%d' already registered\n",
  2672. con->name, con->index))
  2673. return;
  2674. }
  2675. for_each_console(con) {
  2676. if (con->flags & CON_BOOT)
  2677. bootcon_enabled = true;
  2678. else
  2679. realcon_enabled = true;
  2680. }
  2681. /* Do not register boot consoles when there already is a real one. */
  2682. if (newcon->flags & CON_BOOT && realcon_enabled) {
  2683. pr_info("Too late to register bootconsole %s%d\n",
  2684. newcon->name, newcon->index);
  2685. return;
  2686. }
  2687. /*
  2688. * See if we want to enable this console driver by default.
  2689. *
  2690. * Nope when a console is preferred by the command line, device
  2691. * tree, or SPCR.
  2692. *
  2693. * The first real console with tty binding (driver) wins. More
  2694. * consoles might get enabled before the right one is found.
  2695. *
  2696. * Note that a console with tty binding will have CON_CONSDEV
  2697. * flag set and will be first in the list.
  2698. */
  2699. if (preferred_console < 0) {
  2700. if (!console_drivers || !console_drivers->device ||
  2701. console_drivers->flags & CON_BOOT) {
  2702. try_enable_default_console(newcon);
  2703. }
  2704. }
  2705. /* See if this console matches one we selected on the command line */
  2706. err = try_enable_preferred_console(newcon, true);
  2707. /* If not, try to match against the platform default(s) */
  2708. if (err == -ENOENT)
  2709. err = try_enable_preferred_console(newcon, false);
  2710. /* printk() messages are not printed to the Braille console. */
  2711. if (err || newcon->flags & CON_BRL)
  2712. return;
  2713. /*
  2714. * If we have a bootconsole, and are switching to a real console,
  2715. * don't print everything out again, since when the boot console, and
  2716. * the real console are the same physical device, it's annoying to
  2717. * see the beginning boot messages twice
  2718. */
  2719. if (bootcon_enabled &&
  2720. ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
  2721. newcon->flags &= ~CON_PRINTBUFFER;
  2722. }
  2723. /*
  2724. * Put this console in the list - keep the
  2725. * preferred driver at the head of the list.
  2726. */
  2727. console_lock();
  2728. if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
  2729. newcon->next = console_drivers;
  2730. console_drivers = newcon;
  2731. if (newcon->next)
  2732. newcon->next->flags &= ~CON_CONSDEV;
  2733. /* Ensure this flag is always set for the head of the list */
  2734. newcon->flags |= CON_CONSDEV;
  2735. } else {
  2736. newcon->next = console_drivers->next;
  2737. console_drivers->next = newcon;
  2738. }
  2739. newcon->dropped = 0;
  2740. if (newcon->flags & CON_PRINTBUFFER) {
  2741. /* Get a consistent copy of @syslog_seq. */
  2742. mutex_lock(&syslog_lock);
  2743. newcon->seq = syslog_seq;
  2744. mutex_unlock(&syslog_lock);
  2745. } else {
  2746. /* Begin with next message. */
  2747. newcon->seq = prb_next_seq(prb);
  2748. }
  2749. console_unlock();
  2750. console_sysfs_notify();
  2751. /*
  2752. * By unregistering the bootconsoles after we enable the real console
  2753. * we get the "console xxx enabled" message on all the consoles -
  2754. * boot consoles, real consoles, etc - this is to ensure that end
  2755. * users know there might be something in the kernel's log buffer that
  2756. * went to the bootconsole (that they do not see on the real console)
  2757. */
  2758. con_printk(KERN_INFO, newcon, "enabled\n");
  2759. if (bootcon_enabled &&
  2760. ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
  2761. !keep_bootcon) {
  2762. for_each_console(con)
  2763. if (con->flags & CON_BOOT)
  2764. unregister_console(con);
  2765. }
  2766. }
  2767. EXPORT_SYMBOL(register_console);
  2768. int unregister_console(struct console *console)
  2769. {
  2770. struct console *con;
  2771. int res;
  2772. con_printk(KERN_INFO, console, "disabled\n");
  2773. res = _braille_unregister_console(console);
  2774. if (res < 0)
  2775. return res;
  2776. if (res > 0)
  2777. return 0;
  2778. res = -ENODEV;
  2779. console_lock();
  2780. if (console_drivers == console) {
  2781. console_drivers=console->next;
  2782. res = 0;
  2783. } else {
  2784. for_each_console(con) {
  2785. if (con->next == console) {
  2786. con->next = console->next;
  2787. res = 0;
  2788. break;
  2789. }
  2790. }
  2791. }
  2792. if (res)
  2793. goto out_disable_unlock;
  2794. /*
  2795. * If this isn't the last console and it has CON_CONSDEV set, we
  2796. * need to set it on the next preferred console.
  2797. */
  2798. if (console_drivers != NULL && console->flags & CON_CONSDEV)
  2799. console_drivers->flags |= CON_CONSDEV;
  2800. console->flags &= ~CON_ENABLED;
  2801. console_unlock();
  2802. console_sysfs_notify();
  2803. if (console->exit)
  2804. res = console->exit(console);
  2805. return res;
  2806. out_disable_unlock:
  2807. console->flags &= ~CON_ENABLED;
  2808. console_unlock();
  2809. return res;
  2810. }
  2811. EXPORT_SYMBOL(unregister_console);
  2812. /*
  2813. * Initialize the console device. This is called *early*, so
  2814. * we can't necessarily depend on lots of kernel help here.
  2815. * Just do some early initializations, and do the complex setup
  2816. * later.
  2817. */
  2818. void __init console_init(void)
  2819. {
  2820. int ret;
  2821. initcall_t call;
  2822. initcall_entry_t *ce;
  2823. /* Setup the default TTY line discipline. */
  2824. n_tty_init();
  2825. /*
  2826. * set up the console device so that later boot sequences can
  2827. * inform about problems etc..
  2828. */
  2829. ce = __con_initcall_start;
  2830. trace_initcall_level("console");
  2831. while (ce < __con_initcall_end) {
  2832. call = initcall_from_entry(ce);
  2833. trace_initcall_start(call);
  2834. ret = call();
  2835. trace_initcall_finish(call, ret);
  2836. ce++;
  2837. }
  2838. }
  2839. /*
  2840. * Some boot consoles access data that is in the init section and which will
  2841. * be discarded after the initcalls have been run. To make sure that no code
  2842. * will access this data, unregister the boot consoles in a late initcall.
  2843. *
  2844. * If for some reason, such as deferred probe or the driver being a loadable
  2845. * module, the real console hasn't registered yet at this point, there will
  2846. * be a brief interval in which no messages are logged to the console, which
  2847. * makes it difficult to diagnose problems that occur during this time.
  2848. *
  2849. * To mitigate this problem somewhat, only unregister consoles whose memory
  2850. * intersects with the init section. Note that all other boot consoles will
  2851. * get unregistered when the real preferred console is registered.
  2852. */
  2853. static int __init printk_late_init(void)
  2854. {
  2855. struct console *con;
  2856. int ret;
  2857. for_each_console(con) {
  2858. if (!(con->flags & CON_BOOT))
  2859. continue;
  2860. /* Check addresses that might be used for enabled consoles. */
  2861. if (init_section_intersects(con, sizeof(*con)) ||
  2862. init_section_contains(con->write, 0) ||
  2863. init_section_contains(con->read, 0) ||
  2864. init_section_contains(con->device, 0) ||
  2865. init_section_contains(con->unblank, 0) ||
  2866. init_section_contains(con->data, 0)) {
  2867. /*
  2868. * Please, consider moving the reported consoles out
  2869. * of the init section.
  2870. */
  2871. pr_warn("bootconsole [%s%d] uses init memory and must be disabled even before the real one is ready\n",
  2872. con->name, con->index);
  2873. unregister_console(con);
  2874. }
  2875. }
  2876. ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL,
  2877. console_cpu_notify);
  2878. WARN_ON(ret < 0);
  2879. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "printk:online",
  2880. console_cpu_notify, NULL);
  2881. WARN_ON(ret < 0);
  2882. printk_sysctl_init();
  2883. return 0;
  2884. }
  2885. late_initcall(printk_late_init);
  2886. #if defined CONFIG_PRINTK
  2887. /* If @con is specified, only wait for that console. Otherwise wait for all. */
  2888. static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress)
  2889. {
  2890. int remaining = timeout_ms;
  2891. struct console *c;
  2892. u64 last_diff = 0;
  2893. u64 printk_seq;
  2894. u64 diff;
  2895. u64 seq;
  2896. might_sleep();
  2897. seq = prb_next_seq(prb);
  2898. for (;;) {
  2899. diff = 0;
  2900. console_lock();
  2901. for_each_console(c) {
  2902. if (con && con != c)
  2903. continue;
  2904. if (!console_is_usable(c))
  2905. continue;
  2906. printk_seq = c->seq;
  2907. if (printk_seq < seq)
  2908. diff += seq - printk_seq;
  2909. }
  2910. /*
  2911. * If consoles are suspended, it cannot be expected that they
  2912. * make forward progress, so timeout immediately. @diff is
  2913. * still used to return a valid flush status.
  2914. */
  2915. if (console_suspended)
  2916. remaining = 0;
  2917. else if (diff != last_diff && reset_on_progress)
  2918. remaining = timeout_ms;
  2919. console_unlock();
  2920. if (diff == 0 || remaining == 0)
  2921. break;
  2922. if (remaining < 0) {
  2923. /* no timeout limit */
  2924. msleep(100);
  2925. } else if (remaining < 100) {
  2926. msleep(remaining);
  2927. remaining = 0;
  2928. } else {
  2929. msleep(100);
  2930. remaining -= 100;
  2931. }
  2932. last_diff = diff;
  2933. }
  2934. return (diff == 0);
  2935. }
  2936. /**
  2937. * pr_flush() - Wait for printing threads to catch up.
  2938. *
  2939. * @timeout_ms: The maximum time (in ms) to wait.
  2940. * @reset_on_progress: Reset the timeout if forward progress is seen.
  2941. *
  2942. * A value of 0 for @timeout_ms means no waiting will occur. A value of -1
  2943. * represents infinite waiting.
  2944. *
  2945. * If @reset_on_progress is true, the timeout will be reset whenever any
  2946. * printer has been seen to make some forward progress.
  2947. *
  2948. * Context: Process context. May sleep while acquiring console lock.
  2949. * Return: true if all enabled printers are caught up.
  2950. */
  2951. static bool pr_flush(int timeout_ms, bool reset_on_progress)
  2952. {
  2953. return __pr_flush(NULL, timeout_ms, reset_on_progress);
  2954. }
  2955. /*
  2956. * Delayed printk version, for scheduler-internal messages:
  2957. */
  2958. #define PRINTK_PENDING_WAKEUP 0x01
  2959. #define PRINTK_PENDING_OUTPUT 0x02
  2960. static DEFINE_PER_CPU(int, printk_pending);
  2961. static void wake_up_klogd_work_func(struct irq_work *irq_work)
  2962. {
  2963. int pending = this_cpu_xchg(printk_pending, 0);
  2964. if (pending & PRINTK_PENDING_OUTPUT) {
  2965. /* If trylock fails, someone else is doing the printing */
  2966. if (console_trylock())
  2967. console_unlock();
  2968. }
  2969. if (pending & PRINTK_PENDING_WAKEUP)
  2970. wake_up_interruptible(&log_wait);
  2971. }
  2972. static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) =
  2973. IRQ_WORK_INIT_LAZY(wake_up_klogd_work_func);
  2974. static void __wake_up_klogd(int val)
  2975. {
  2976. if (!printk_percpu_data_ready())
  2977. return;
  2978. preempt_disable();
  2979. /*
  2980. * Guarantee any new records can be seen by tasks preparing to wait
  2981. * before this context checks if the wait queue is empty.
  2982. *
  2983. * The full memory barrier within wq_has_sleeper() pairs with the full
  2984. * memory barrier within set_current_state() of
  2985. * prepare_to_wait_event(), which is called after ___wait_event() adds
  2986. * the waiter but before it has checked the wait condition.
  2987. *
  2988. * This pairs with devkmsg_read:A and syslog_print:A.
  2989. */
  2990. if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */
  2991. (val & PRINTK_PENDING_OUTPUT)) {
  2992. this_cpu_or(printk_pending, val);
  2993. irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
  2994. }
  2995. preempt_enable();
  2996. }
  2997. /**
  2998. * wake_up_klogd - Wake kernel logging daemon
  2999. *
  3000. * Use this function when new records have been added to the ringbuffer
  3001. * and the console printing of those records has already occurred or is
  3002. * known to be handled by some other context. This function will only
  3003. * wake the logging daemon.
  3004. *
  3005. * Context: Any context.
  3006. */
  3007. void wake_up_klogd(void)
  3008. {
  3009. __wake_up_klogd(PRINTK_PENDING_WAKEUP);
  3010. }
  3011. /**
  3012. * defer_console_output - Wake kernel logging daemon and trigger
  3013. * console printing in a deferred context
  3014. *
  3015. * Use this function when new records have been added to the ringbuffer,
  3016. * this context is responsible for console printing those records, but
  3017. * the current context is not allowed to perform the console printing.
  3018. * Trigger an irq_work context to perform the console printing. This
  3019. * function also wakes the logging daemon.
  3020. *
  3021. * Context: Any context.
  3022. */
  3023. void defer_console_output(void)
  3024. {
  3025. /*
  3026. * New messages may have been added directly to the ringbuffer
  3027. * using vprintk_store(), so wake any waiters as well.
  3028. */
  3029. __wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT);
  3030. }
  3031. void printk_trigger_flush(void)
  3032. {
  3033. defer_console_output();
  3034. }
  3035. int vprintk_deferred(const char *fmt, va_list args)
  3036. {
  3037. return vprintk_emit(0, LOGLEVEL_SCHED, NULL, fmt, args);
  3038. }
  3039. int _printk_deferred(const char *fmt, ...)
  3040. {
  3041. va_list args;
  3042. int r;
  3043. va_start(args, fmt);
  3044. r = vprintk_deferred(fmt, args);
  3045. va_end(args);
  3046. return r;
  3047. }
  3048. EXPORT_SYMBOL_GPL(_printk_deferred);
  3049. /*
  3050. * printk rate limiting, lifted from the networking subsystem.
  3051. *
  3052. * This enforces a rate limit: not more than 10 kernel messages
  3053. * every 5s to make a denial-of-service attack impossible.
  3054. */
  3055. DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
  3056. int __printk_ratelimit(const char *func)
  3057. {
  3058. return ___ratelimit(&printk_ratelimit_state, func);
  3059. }
  3060. EXPORT_SYMBOL(__printk_ratelimit);
  3061. /**
  3062. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  3063. * @caller_jiffies: pointer to caller's state
  3064. * @interval_msecs: minimum interval between prints
  3065. *
  3066. * printk_timed_ratelimit() returns true if more than @interval_msecs
  3067. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  3068. * returned true.
  3069. */
  3070. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  3071. unsigned int interval_msecs)
  3072. {
  3073. unsigned long elapsed = jiffies - *caller_jiffies;
  3074. if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs))
  3075. return false;
  3076. *caller_jiffies = jiffies;
  3077. return true;
  3078. }
  3079. EXPORT_SYMBOL(printk_timed_ratelimit);
  3080. static DEFINE_SPINLOCK(dump_list_lock);
  3081. static LIST_HEAD(dump_list);
  3082. /**
  3083. * kmsg_dump_register - register a kernel log dumper.
  3084. * @dumper: pointer to the kmsg_dumper structure
  3085. *
  3086. * Adds a kernel log dumper to the system. The dump callback in the
  3087. * structure will be called when the kernel oopses or panics and must be
  3088. * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
  3089. */
  3090. int kmsg_dump_register(struct kmsg_dumper *dumper)
  3091. {
  3092. unsigned long flags;
  3093. int err = -EBUSY;
  3094. /* The dump callback needs to be set */
  3095. if (!dumper->dump)
  3096. return -EINVAL;
  3097. spin_lock_irqsave(&dump_list_lock, flags);
  3098. /* Don't allow registering multiple times */
  3099. if (!dumper->registered) {
  3100. dumper->registered = 1;
  3101. list_add_tail_rcu(&dumper->list, &dump_list);
  3102. err = 0;
  3103. }
  3104. spin_unlock_irqrestore(&dump_list_lock, flags);
  3105. return err;
  3106. }
  3107. EXPORT_SYMBOL_GPL(kmsg_dump_register);
  3108. /**
  3109. * kmsg_dump_unregister - unregister a kmsg dumper.
  3110. * @dumper: pointer to the kmsg_dumper structure
  3111. *
  3112. * Removes a dump device from the system. Returns zero on success and
  3113. * %-EINVAL otherwise.
  3114. */
  3115. int kmsg_dump_unregister(struct kmsg_dumper *dumper)
  3116. {
  3117. unsigned long flags;
  3118. int err = -EINVAL;
  3119. spin_lock_irqsave(&dump_list_lock, flags);
  3120. if (dumper->registered) {
  3121. dumper->registered = 0;
  3122. list_del_rcu(&dumper->list);
  3123. err = 0;
  3124. }
  3125. spin_unlock_irqrestore(&dump_list_lock, flags);
  3126. synchronize_rcu();
  3127. return err;
  3128. }
  3129. EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
  3130. static bool always_kmsg_dump;
  3131. module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
  3132. const char *kmsg_dump_reason_str(enum kmsg_dump_reason reason)
  3133. {
  3134. switch (reason) {
  3135. case KMSG_DUMP_PANIC:
  3136. return "Panic";
  3137. case KMSG_DUMP_OOPS:
  3138. return "Oops";
  3139. case KMSG_DUMP_EMERG:
  3140. return "Emergency";
  3141. case KMSG_DUMP_SHUTDOWN:
  3142. return "Shutdown";
  3143. default:
  3144. return "Unknown";
  3145. }
  3146. }
  3147. EXPORT_SYMBOL_GPL(kmsg_dump_reason_str);
  3148. /**
  3149. * kmsg_dump - dump kernel log to kernel message dumpers.
  3150. * @reason: the reason (oops, panic etc) for dumping
  3151. *
  3152. * Call each of the registered dumper's dump() callback, which can
  3153. * retrieve the kmsg records with kmsg_dump_get_line() or
  3154. * kmsg_dump_get_buffer().
  3155. */
  3156. void kmsg_dump(enum kmsg_dump_reason reason)
  3157. {
  3158. struct kmsg_dumper *dumper;
  3159. rcu_read_lock();
  3160. list_for_each_entry_rcu(dumper, &dump_list, list) {
  3161. enum kmsg_dump_reason max_reason = dumper->max_reason;
  3162. /*
  3163. * If client has not provided a specific max_reason, default
  3164. * to KMSG_DUMP_OOPS, unless always_kmsg_dump was set.
  3165. */
  3166. if (max_reason == KMSG_DUMP_UNDEF) {
  3167. max_reason = always_kmsg_dump ? KMSG_DUMP_MAX :
  3168. KMSG_DUMP_OOPS;
  3169. }
  3170. if (reason > max_reason)
  3171. continue;
  3172. /* invoke dumper which will iterate over records */
  3173. dumper->dump(dumper, reason);
  3174. }
  3175. rcu_read_unlock();
  3176. }
  3177. /**
  3178. * kmsg_dump_get_line - retrieve one kmsg log line
  3179. * @iter: kmsg dump iterator
  3180. * @syslog: include the "<4>" prefixes
  3181. * @line: buffer to copy the line to
  3182. * @size: maximum size of the buffer
  3183. * @len: length of line placed into buffer
  3184. *
  3185. * Start at the beginning of the kmsg buffer, with the oldest kmsg
  3186. * record, and copy one record into the provided buffer.
  3187. *
  3188. * Consecutive calls will return the next available record moving
  3189. * towards the end of the buffer with the youngest messages.
  3190. *
  3191. * A return value of FALSE indicates that there are no more records to
  3192. * read.
  3193. */
  3194. bool kmsg_dump_get_line(struct kmsg_dump_iter *iter, bool syslog,
  3195. char *line, size_t size, size_t *len)
  3196. {
  3197. u64 min_seq = latched_seq_read_nolock(&clear_seq);
  3198. struct printk_info info;
  3199. unsigned int line_count;
  3200. struct printk_record r;
  3201. size_t l = 0;
  3202. bool ret = false;
  3203. if (iter->cur_seq < min_seq)
  3204. iter->cur_seq = min_seq;
  3205. prb_rec_init_rd(&r, &info, line, size);
  3206. /* Read text or count text lines? */
  3207. if (line) {
  3208. if (!prb_read_valid(prb, iter->cur_seq, &r))
  3209. goto out;
  3210. l = record_print_text(&r, syslog, printk_time);
  3211. } else {
  3212. if (!prb_read_valid_info(prb, iter->cur_seq,
  3213. &info, &line_count)) {
  3214. goto out;
  3215. }
  3216. l = get_record_print_text_size(&info, line_count, syslog,
  3217. printk_time);
  3218. }
  3219. iter->cur_seq = r.info->seq + 1;
  3220. ret = true;
  3221. out:
  3222. if (len)
  3223. *len = l;
  3224. return ret;
  3225. }
  3226. EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
  3227. /**
  3228. * kmsg_dump_get_buffer - copy kmsg log lines
  3229. * @iter: kmsg dump iterator
  3230. * @syslog: include the "<4>" prefixes
  3231. * @buf: buffer to copy the line to
  3232. * @size: maximum size of the buffer
  3233. * @len_out: length of line placed into buffer
  3234. *
  3235. * Start at the end of the kmsg buffer and fill the provided buffer
  3236. * with as many of the *youngest* kmsg records that fit into it.
  3237. * If the buffer is large enough, all available kmsg records will be
  3238. * copied with a single call.
  3239. *
  3240. * Consecutive calls will fill the buffer with the next block of
  3241. * available older records, not including the earlier retrieved ones.
  3242. *
  3243. * A return value of FALSE indicates that there are no more records to
  3244. * read.
  3245. */
  3246. bool kmsg_dump_get_buffer(struct kmsg_dump_iter *iter, bool syslog,
  3247. char *buf, size_t size, size_t *len_out)
  3248. {
  3249. u64 min_seq = latched_seq_read_nolock(&clear_seq);
  3250. struct printk_info info;
  3251. struct printk_record r;
  3252. u64 seq;
  3253. u64 next_seq;
  3254. size_t len = 0;
  3255. bool ret = false;
  3256. bool time = printk_time;
  3257. if (!buf || !size)
  3258. goto out;
  3259. if (iter->cur_seq < min_seq)
  3260. iter->cur_seq = min_seq;
  3261. if (prb_read_valid_info(prb, iter->cur_seq, &info, NULL)) {
  3262. if (info.seq != iter->cur_seq) {
  3263. /* messages are gone, move to first available one */
  3264. iter->cur_seq = info.seq;
  3265. }
  3266. }
  3267. /* last entry */
  3268. if (iter->cur_seq >= iter->next_seq)
  3269. goto out;
  3270. /*
  3271. * Find first record that fits, including all following records,
  3272. * into the user-provided buffer for this dump. Pass in size-1
  3273. * because this function (by way of record_print_text()) will
  3274. * not write more than size-1 bytes of text into @buf.
  3275. */
  3276. seq = find_first_fitting_seq(iter->cur_seq, iter->next_seq,
  3277. size - 1, syslog, time);
  3278. /*
  3279. * Next kmsg_dump_get_buffer() invocation will dump block of
  3280. * older records stored right before this one.
  3281. */
  3282. next_seq = seq;
  3283. prb_rec_init_rd(&r, &info, buf, size);
  3284. len = 0;
  3285. prb_for_each_record(seq, prb, seq, &r) {
  3286. if (r.info->seq >= iter->next_seq)
  3287. break;
  3288. len += record_print_text(&r, syslog, time);
  3289. /* Adjust record to store to remaining buffer space. */
  3290. prb_rec_init_rd(&r, &info, buf + len, size - len);
  3291. }
  3292. iter->next_seq = next_seq;
  3293. ret = true;
  3294. out:
  3295. if (len_out)
  3296. *len_out = len;
  3297. return ret;
  3298. }
  3299. EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
  3300. /**
  3301. * kmsg_dump_rewind - reset the iterator
  3302. * @iter: kmsg dump iterator
  3303. *
  3304. * Reset the dumper's iterator so that kmsg_dump_get_line() and
  3305. * kmsg_dump_get_buffer() can be called again and used multiple
  3306. * times within the same dumper.dump() callback.
  3307. */
  3308. void kmsg_dump_rewind(struct kmsg_dump_iter *iter)
  3309. {
  3310. iter->cur_seq = latched_seq_read_nolock(&clear_seq);
  3311. iter->next_seq = prb_next_seq(prb);
  3312. }
  3313. EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
  3314. #endif
  3315. #ifdef CONFIG_SMP
  3316. static atomic_t printk_cpu_sync_owner = ATOMIC_INIT(-1);
  3317. static atomic_t printk_cpu_sync_nested = ATOMIC_INIT(0);
  3318. /**
  3319. * __printk_cpu_sync_wait() - Busy wait until the printk cpu-reentrant
  3320. * spinning lock is not owned by any CPU.
  3321. *
  3322. * Context: Any context.
  3323. */
  3324. void __printk_cpu_sync_wait(void)
  3325. {
  3326. do {
  3327. cpu_relax();
  3328. } while (atomic_read(&printk_cpu_sync_owner) != -1);
  3329. }
  3330. EXPORT_SYMBOL(__printk_cpu_sync_wait);
  3331. /**
  3332. * __printk_cpu_sync_try_get() - Try to acquire the printk cpu-reentrant
  3333. * spinning lock.
  3334. *
  3335. * If no processor has the lock, the calling processor takes the lock and
  3336. * becomes the owner. If the calling processor is already the owner of the
  3337. * lock, this function succeeds immediately.
  3338. *
  3339. * Context: Any context. Expects interrupts to be disabled.
  3340. * Return: 1 on success, otherwise 0.
  3341. */
  3342. int __printk_cpu_sync_try_get(void)
  3343. {
  3344. int cpu;
  3345. int old;
  3346. cpu = smp_processor_id();
  3347. /*
  3348. * Guarantee loads and stores from this CPU when it is the lock owner
  3349. * are _not_ visible to the previous lock owner. This pairs with
  3350. * __printk_cpu_sync_put:B.
  3351. *
  3352. * Memory barrier involvement:
  3353. *
  3354. * If __printk_cpu_sync_try_get:A reads from __printk_cpu_sync_put:B,
  3355. * then __printk_cpu_sync_put:A can never read from
  3356. * __printk_cpu_sync_try_get:B.
  3357. *
  3358. * Relies on:
  3359. *
  3360. * RELEASE from __printk_cpu_sync_put:A to __printk_cpu_sync_put:B
  3361. * of the previous CPU
  3362. * matching
  3363. * ACQUIRE from __printk_cpu_sync_try_get:A to
  3364. * __printk_cpu_sync_try_get:B of this CPU
  3365. */
  3366. old = atomic_cmpxchg_acquire(&printk_cpu_sync_owner, -1,
  3367. cpu); /* LMM(__printk_cpu_sync_try_get:A) */
  3368. if (old == -1) {
  3369. /*
  3370. * This CPU is now the owner and begins loading/storing
  3371. * data: LMM(__printk_cpu_sync_try_get:B)
  3372. */
  3373. return 1;
  3374. } else if (old == cpu) {
  3375. /* This CPU is already the owner. */
  3376. atomic_inc(&printk_cpu_sync_nested);
  3377. return 1;
  3378. }
  3379. return 0;
  3380. }
  3381. EXPORT_SYMBOL(__printk_cpu_sync_try_get);
  3382. /**
  3383. * __printk_cpu_sync_put() - Release the printk cpu-reentrant spinning lock.
  3384. *
  3385. * The calling processor must be the owner of the lock.
  3386. *
  3387. * Context: Any context. Expects interrupts to be disabled.
  3388. */
  3389. void __printk_cpu_sync_put(void)
  3390. {
  3391. if (atomic_read(&printk_cpu_sync_nested)) {
  3392. atomic_dec(&printk_cpu_sync_nested);
  3393. return;
  3394. }
  3395. /*
  3396. * This CPU is finished loading/storing data:
  3397. * LMM(__printk_cpu_sync_put:A)
  3398. */
  3399. /*
  3400. * Guarantee loads and stores from this CPU when it was the
  3401. * lock owner are visible to the next lock owner. This pairs
  3402. * with __printk_cpu_sync_try_get:A.
  3403. *
  3404. * Memory barrier involvement:
  3405. *
  3406. * If __printk_cpu_sync_try_get:A reads from __printk_cpu_sync_put:B,
  3407. * then __printk_cpu_sync_try_get:B reads from __printk_cpu_sync_put:A.
  3408. *
  3409. * Relies on:
  3410. *
  3411. * RELEASE from __printk_cpu_sync_put:A to __printk_cpu_sync_put:B
  3412. * of this CPU
  3413. * matching
  3414. * ACQUIRE from __printk_cpu_sync_try_get:A to
  3415. * __printk_cpu_sync_try_get:B of the next CPU
  3416. */
  3417. atomic_set_release(&printk_cpu_sync_owner,
  3418. -1); /* LMM(__printk_cpu_sync_put:B) */
  3419. }
  3420. EXPORT_SYMBOL(__printk_cpu_sync_put);
  3421. #endif /* CONFIG_SMP */