printk.c 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011
  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. * manfred@colorfullife.com
  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/syscore_ops.h>
  36. #include <linux/vmcore_info.h>
  37. #include <linux/ratelimit.h>
  38. #include <linux/kmsg_dump.h>
  39. #include <linux/syslog.h>
  40. #include <linux/cpu.h>
  41. #include <linux/rculist.h>
  42. #include <linux/poll.h>
  43. #include <linux/irq_work.h>
  44. #include <linux/ctype.h>
  45. #include <linux/uio.h>
  46. #include <linux/sched/clock.h>
  47. #include <linux/sched/debug.h>
  48. #include <linux/sched/task_stack.h>
  49. #include <linux/uaccess.h>
  50. #include <asm/sections.h>
  51. #include <trace/events/initcall.h>
  52. #define CREATE_TRACE_POINTS
  53. #include <trace/events/printk.h>
  54. #include "printk_ringbuffer.h"
  55. #include "console_cmdline.h"
  56. #include "braille.h"
  57. #include "internal.h"
  58. int console_printk[4] = {
  59. CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */
  60. MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */
  61. CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */
  62. CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */
  63. };
  64. EXPORT_SYMBOL_GPL(console_printk);
  65. atomic_t ignore_console_lock_warning __read_mostly = ATOMIC_INIT(0);
  66. EXPORT_SYMBOL(ignore_console_lock_warning);
  67. EXPORT_TRACEPOINT_SYMBOL_GPL(console);
  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_mutex protects console_list updates and console->flags updates.
  76. * The flags are synchronized only for consoles that are registered, i.e.
  77. * accessible via the console list.
  78. */
  79. static DEFINE_MUTEX(console_mutex);
  80. /*
  81. * console_sem protects updates to console->seq
  82. * and also provides serialization for console printing.
  83. */
  84. static DEFINE_SEMAPHORE(console_sem, 1);
  85. HLIST_HEAD(console_list);
  86. EXPORT_SYMBOL_GPL(console_list);
  87. DEFINE_STATIC_SRCU(console_srcu);
  88. /*
  89. * System may need to suppress printk message under certain
  90. * circumstances, like after kernel panic happens.
  91. */
  92. int __read_mostly suppress_printk;
  93. #ifdef CONFIG_LOCKDEP
  94. static struct lockdep_map console_lock_dep_map = {
  95. .name = "console_lock"
  96. };
  97. void lockdep_assert_console_list_lock_held(void)
  98. {
  99. lockdep_assert_held(&console_mutex);
  100. }
  101. EXPORT_SYMBOL(lockdep_assert_console_list_lock_held);
  102. #endif
  103. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  104. bool console_srcu_read_lock_is_held(void)
  105. {
  106. return srcu_read_lock_held(&console_srcu);
  107. }
  108. EXPORT_SYMBOL(console_srcu_read_lock_is_held);
  109. #endif
  110. enum devkmsg_log_bits {
  111. __DEVKMSG_LOG_BIT_ON = 0,
  112. __DEVKMSG_LOG_BIT_OFF,
  113. __DEVKMSG_LOG_BIT_LOCK,
  114. };
  115. enum devkmsg_log_masks {
  116. DEVKMSG_LOG_MASK_ON = BIT(__DEVKMSG_LOG_BIT_ON),
  117. DEVKMSG_LOG_MASK_OFF = BIT(__DEVKMSG_LOG_BIT_OFF),
  118. DEVKMSG_LOG_MASK_LOCK = BIT(__DEVKMSG_LOG_BIT_LOCK),
  119. };
  120. /* Keep both the 'on' and 'off' bits clear, i.e. ratelimit by default: */
  121. #define DEVKMSG_LOG_MASK_DEFAULT 0
  122. static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
  123. static int __control_devkmsg(char *str)
  124. {
  125. size_t len;
  126. if (!str)
  127. return -EINVAL;
  128. len = str_has_prefix(str, "on");
  129. if (len) {
  130. devkmsg_log = DEVKMSG_LOG_MASK_ON;
  131. return len;
  132. }
  133. len = str_has_prefix(str, "off");
  134. if (len) {
  135. devkmsg_log = DEVKMSG_LOG_MASK_OFF;
  136. return len;
  137. }
  138. len = str_has_prefix(str, "ratelimit");
  139. if (len) {
  140. devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
  141. return len;
  142. }
  143. return -EINVAL;
  144. }
  145. static int __init control_devkmsg(char *str)
  146. {
  147. if (__control_devkmsg(str) < 0) {
  148. pr_warn("printk.devkmsg: bad option string '%s'\n", str);
  149. return 1;
  150. }
  151. /*
  152. * Set sysctl string accordingly:
  153. */
  154. if (devkmsg_log == DEVKMSG_LOG_MASK_ON)
  155. strscpy(devkmsg_log_str, "on");
  156. else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF)
  157. strscpy(devkmsg_log_str, "off");
  158. /* else "ratelimit" which is set by default. */
  159. /*
  160. * Sysctl cannot change it anymore. The kernel command line setting of
  161. * this parameter is to force the setting to be permanent throughout the
  162. * runtime of the system. This is a precation measure against userspace
  163. * trying to be a smarta** and attempting to change it up on us.
  164. */
  165. devkmsg_log |= DEVKMSG_LOG_MASK_LOCK;
  166. return 1;
  167. }
  168. __setup("printk.devkmsg=", control_devkmsg);
  169. char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit";
  170. #if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL)
  171. int devkmsg_sysctl_set_loglvl(const struct ctl_table *table, int write,
  172. void *buffer, size_t *lenp, loff_t *ppos)
  173. {
  174. char old_str[DEVKMSG_STR_MAX_SIZE];
  175. unsigned int old;
  176. int err;
  177. if (write) {
  178. if (devkmsg_log & DEVKMSG_LOG_MASK_LOCK)
  179. return -EINVAL;
  180. old = devkmsg_log;
  181. strscpy(old_str, devkmsg_log_str);
  182. }
  183. err = proc_dostring(table, write, buffer, lenp, ppos);
  184. if (err)
  185. return err;
  186. if (write) {
  187. err = __control_devkmsg(devkmsg_log_str);
  188. /*
  189. * Do not accept an unknown string OR a known string with
  190. * trailing crap...
  191. */
  192. if (err < 0 || (err + 1 != *lenp)) {
  193. /* ... and restore old setting. */
  194. devkmsg_log = old;
  195. strscpy(devkmsg_log_str, old_str);
  196. return -EINVAL;
  197. }
  198. }
  199. return 0;
  200. }
  201. #endif /* CONFIG_PRINTK && CONFIG_SYSCTL */
  202. /**
  203. * console_list_lock - Lock the console list
  204. *
  205. * For console list or console->flags updates
  206. */
  207. void console_list_lock(void)
  208. {
  209. /*
  210. * In unregister_console() and console_force_preferred_locked(),
  211. * synchronize_srcu() is called with the console_list_lock held.
  212. * Therefore it is not allowed that the console_list_lock is taken
  213. * with the srcu_lock held.
  214. *
  215. * Detecting if this context is really in the read-side critical
  216. * section is only possible if the appropriate debug options are
  217. * enabled.
  218. */
  219. WARN_ON_ONCE(debug_lockdep_rcu_enabled() &&
  220. srcu_read_lock_held(&console_srcu));
  221. mutex_lock(&console_mutex);
  222. }
  223. EXPORT_SYMBOL(console_list_lock);
  224. /**
  225. * console_list_unlock - Unlock the console list
  226. *
  227. * Counterpart to console_list_lock()
  228. */
  229. void console_list_unlock(void)
  230. {
  231. mutex_unlock(&console_mutex);
  232. }
  233. EXPORT_SYMBOL(console_list_unlock);
  234. /**
  235. * console_srcu_read_lock - Register a new reader for the
  236. * SRCU-protected console list
  237. *
  238. * Use for_each_console_srcu() to iterate the console list
  239. *
  240. * Context: Any context.
  241. * Return: A cookie to pass to console_srcu_read_unlock().
  242. */
  243. int console_srcu_read_lock(void)
  244. __acquires(&console_srcu)
  245. {
  246. return srcu_read_lock_nmisafe(&console_srcu);
  247. }
  248. EXPORT_SYMBOL(console_srcu_read_lock);
  249. /**
  250. * console_srcu_read_unlock - Unregister an old reader from
  251. * the SRCU-protected console list
  252. * @cookie: cookie returned from console_srcu_read_lock()
  253. *
  254. * Counterpart to console_srcu_read_lock()
  255. */
  256. void console_srcu_read_unlock(int cookie)
  257. __releases(&console_srcu)
  258. {
  259. srcu_read_unlock_nmisafe(&console_srcu, cookie);
  260. }
  261. EXPORT_SYMBOL(console_srcu_read_unlock);
  262. /*
  263. * Helper macros to handle lockdep when locking/unlocking console_sem. We use
  264. * macros instead of functions so that _RET_IP_ contains useful information.
  265. */
  266. #define down_console_sem() do { \
  267. down(&console_sem);\
  268. mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);\
  269. } while (0)
  270. static int __down_trylock_console_sem(unsigned long ip)
  271. {
  272. int lock_failed;
  273. unsigned long flags;
  274. /*
  275. * Here and in __up_console_sem() we need to be in safe mode,
  276. * because spindump/WARN/etc from under console ->lock will
  277. * deadlock in printk()->down_trylock_console_sem() otherwise.
  278. */
  279. printk_safe_enter_irqsave(flags);
  280. lock_failed = down_trylock(&console_sem);
  281. printk_safe_exit_irqrestore(flags);
  282. if (lock_failed)
  283. return 1;
  284. mutex_acquire(&console_lock_dep_map, 0, 1, ip);
  285. return 0;
  286. }
  287. #define down_trylock_console_sem() __down_trylock_console_sem(_RET_IP_)
  288. static void __up_console_sem(unsigned long ip)
  289. {
  290. unsigned long flags;
  291. mutex_release(&console_lock_dep_map, ip);
  292. printk_safe_enter_irqsave(flags);
  293. up(&console_sem);
  294. printk_safe_exit_irqrestore(flags);
  295. }
  296. #define up_console_sem() __up_console_sem(_RET_IP_)
  297. static bool panic_in_progress(void)
  298. {
  299. return unlikely(atomic_read(&panic_cpu) != PANIC_CPU_INVALID);
  300. }
  301. /* Return true if a panic is in progress on the current CPU. */
  302. bool this_cpu_in_panic(void)
  303. {
  304. /*
  305. * We can use raw_smp_processor_id() here because it is impossible for
  306. * the task to be migrated to the panic_cpu, or away from it. If
  307. * panic_cpu has already been set, and we're not currently executing on
  308. * that CPU, then we never will be.
  309. */
  310. return unlikely(atomic_read(&panic_cpu) == raw_smp_processor_id());
  311. }
  312. /*
  313. * Return true if a panic is in progress on a remote CPU.
  314. *
  315. * On true, the local CPU should immediately release any printing resources
  316. * that may be needed by the panic CPU.
  317. */
  318. bool other_cpu_in_panic(void)
  319. {
  320. return (panic_in_progress() && !this_cpu_in_panic());
  321. }
  322. /*
  323. * This is used for debugging the mess that is the VT code by
  324. * keeping track if we have the console semaphore held. It's
  325. * definitely not the perfect debug tool (we don't know if _WE_
  326. * hold it and are racing, but it helps tracking those weird code
  327. * paths in the console code where we end up in places I want
  328. * locked without the console semaphore held).
  329. */
  330. static int console_locked;
  331. /*
  332. * Array of consoles built from command line options (console=)
  333. */
  334. #define MAX_CMDLINECONSOLES 8
  335. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  336. static int preferred_console = -1;
  337. int console_set_on_cmdline;
  338. EXPORT_SYMBOL(console_set_on_cmdline);
  339. /* Flag: console code may call schedule() */
  340. static int console_may_schedule;
  341. enum con_msg_format_flags {
  342. MSG_FORMAT_DEFAULT = 0,
  343. MSG_FORMAT_SYSLOG = (1 << 0),
  344. };
  345. static int console_msg_format = MSG_FORMAT_DEFAULT;
  346. /*
  347. * The printk log buffer consists of a sequenced collection of records, each
  348. * containing variable length message text. Every record also contains its
  349. * own meta-data (@info).
  350. *
  351. * Every record meta-data carries the timestamp in microseconds, as well as
  352. * the standard userspace syslog level and syslog facility. The usual kernel
  353. * messages use LOG_KERN; userspace-injected messages always carry a matching
  354. * syslog facility, by default LOG_USER. The origin of every message can be
  355. * reliably determined that way.
  356. *
  357. * The human readable log message of a record is available in @text, the
  358. * length of the message text in @text_len. The stored message is not
  359. * terminated.
  360. *
  361. * Optionally, a record can carry a dictionary of properties (key/value
  362. * pairs), to provide userspace with a machine-readable message context.
  363. *
  364. * Examples for well-defined, commonly used property names are:
  365. * DEVICE=b12:8 device identifier
  366. * b12:8 block dev_t
  367. * c127:3 char dev_t
  368. * n8 netdev ifindex
  369. * +sound:card0 subsystem:devname
  370. * SUBSYSTEM=pci driver-core subsystem name
  371. *
  372. * Valid characters in property names are [a-zA-Z0-9.-_]. Property names
  373. * and values are terminated by a '\0' character.
  374. *
  375. * Example of record values:
  376. * record.text_buf = "it's a line" (unterminated)
  377. * record.info.seq = 56
  378. * record.info.ts_nsec = 36863
  379. * record.info.text_len = 11
  380. * record.info.facility = 0 (LOG_KERN)
  381. * record.info.flags = 0
  382. * record.info.level = 3 (LOG_ERR)
  383. * record.info.caller_id = 299 (task 299)
  384. * record.info.dev_info.subsystem = "pci" (terminated)
  385. * record.info.dev_info.device = "+pci:0000:00:01.0" (terminated)
  386. *
  387. * The 'struct printk_info' buffer must never be directly exported to
  388. * userspace, it is a kernel-private implementation detail that might
  389. * need to be changed in the future, when the requirements change.
  390. *
  391. * /dev/kmsg exports the structured data in the following line format:
  392. * "<level>,<sequnum>,<timestamp>,<contflag>[,additional_values, ... ];<message text>\n"
  393. *
  394. * Users of the export format should ignore possible additional values
  395. * separated by ',', and find the message after the ';' character.
  396. *
  397. * The optional key/value pairs are attached as continuation lines starting
  398. * with a space character and terminated by a newline. All possible
  399. * non-prinatable characters are escaped in the "\xff" notation.
  400. */
  401. /* syslog_lock protects syslog_* variables and write access to clear_seq. */
  402. static DEFINE_MUTEX(syslog_lock);
  403. /*
  404. * Specifies if a legacy console is registered. If legacy consoles are
  405. * present, it is necessary to perform the console lock/unlock dance
  406. * whenever console flushing should occur.
  407. */
  408. bool have_legacy_console;
  409. /*
  410. * Specifies if an nbcon console is registered. If nbcon consoles are present,
  411. * synchronous printing of legacy consoles will not occur during panic until
  412. * the backtrace has been stored to the ringbuffer.
  413. */
  414. bool have_nbcon_console;
  415. /*
  416. * Specifies if a boot console is registered. If boot consoles are present,
  417. * nbcon consoles cannot print simultaneously and must be synchronized by
  418. * the console lock. This is because boot consoles and nbcon consoles may
  419. * have mapped the same hardware.
  420. */
  421. bool have_boot_console;
  422. /* See printk_legacy_allow_panic_sync() for details. */
  423. bool legacy_allow_panic_sync;
  424. #ifdef CONFIG_PRINTK
  425. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  426. static DECLARE_WAIT_QUEUE_HEAD(legacy_wait);
  427. /* All 3 protected by @syslog_lock. */
  428. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  429. static u64 syslog_seq;
  430. static size_t syslog_partial;
  431. static bool syslog_time;
  432. /* True when _all_ printer threads are available for printing. */
  433. bool printk_kthreads_running;
  434. struct latched_seq {
  435. seqcount_latch_t latch;
  436. u64 val[2];
  437. };
  438. /*
  439. * The next printk record to read after the last 'clear' command. There are
  440. * two copies (updated with seqcount_latch) so that reads can locklessly
  441. * access a valid value. Writers are synchronized by @syslog_lock.
  442. */
  443. static struct latched_seq clear_seq = {
  444. .latch = SEQCNT_LATCH_ZERO(clear_seq.latch),
  445. .val[0] = 0,
  446. .val[1] = 0,
  447. };
  448. #define LOG_LEVEL(v) ((v) & 0x07)
  449. #define LOG_FACILITY(v) ((v) >> 3 & 0xff)
  450. /* record buffer */
  451. #define LOG_ALIGN __alignof__(unsigned long)
  452. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  453. #define LOG_BUF_LEN_MAX ((u32)1 << 31)
  454. static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
  455. static char *log_buf = __log_buf;
  456. static u32 log_buf_len = __LOG_BUF_LEN;
  457. /*
  458. * Define the average message size. This only affects the number of
  459. * descriptors that will be available. Underestimating is better than
  460. * overestimating (too many available descriptors is better than not enough).
  461. */
  462. #define PRB_AVGBITS 5 /* 32 character average length */
  463. #if CONFIG_LOG_BUF_SHIFT <= PRB_AVGBITS
  464. #error CONFIG_LOG_BUF_SHIFT value too small.
  465. #endif
  466. _DEFINE_PRINTKRB(printk_rb_static, CONFIG_LOG_BUF_SHIFT - PRB_AVGBITS,
  467. PRB_AVGBITS, &__log_buf[0]);
  468. static struct printk_ringbuffer printk_rb_dynamic;
  469. struct printk_ringbuffer *prb = &printk_rb_static;
  470. /*
  471. * We cannot access per-CPU data (e.g. per-CPU flush irq_work) before
  472. * per_cpu_areas are initialised. This variable is set to true when
  473. * it's safe to access per-CPU data.
  474. */
  475. static bool __printk_percpu_data_ready __ro_after_init;
  476. bool printk_percpu_data_ready(void)
  477. {
  478. return __printk_percpu_data_ready;
  479. }
  480. /* Must be called under syslog_lock. */
  481. static void latched_seq_write(struct latched_seq *ls, u64 val)
  482. {
  483. raw_write_seqcount_latch(&ls->latch);
  484. ls->val[0] = val;
  485. raw_write_seqcount_latch(&ls->latch);
  486. ls->val[1] = val;
  487. }
  488. /* Can be called from any context. */
  489. static u64 latched_seq_read_nolock(struct latched_seq *ls)
  490. {
  491. unsigned int seq;
  492. unsigned int idx;
  493. u64 val;
  494. do {
  495. seq = raw_read_seqcount_latch(&ls->latch);
  496. idx = seq & 0x1;
  497. val = ls->val[idx];
  498. } while (raw_read_seqcount_latch_retry(&ls->latch, seq));
  499. return val;
  500. }
  501. /* Return log buffer address */
  502. char *log_buf_addr_get(void)
  503. {
  504. return log_buf;
  505. }
  506. /* Return log buffer size */
  507. u32 log_buf_len_get(void)
  508. {
  509. return log_buf_len;
  510. }
  511. /*
  512. * Define how much of the log buffer we could take at maximum. The value
  513. * must be greater than two. Note that only half of the buffer is available
  514. * when the index points to the middle.
  515. */
  516. #define MAX_LOG_TAKE_PART 4
  517. static const char trunc_msg[] = "<truncated>";
  518. static void truncate_msg(u16 *text_len, u16 *trunc_msg_len)
  519. {
  520. /*
  521. * The message should not take the whole buffer. Otherwise, it might
  522. * get removed too soon.
  523. */
  524. u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
  525. if (*text_len > max_text_len)
  526. *text_len = max_text_len;
  527. /* enable the warning message (if there is room) */
  528. *trunc_msg_len = strlen(trunc_msg);
  529. if (*text_len >= *trunc_msg_len)
  530. *text_len -= *trunc_msg_len;
  531. else
  532. *trunc_msg_len = 0;
  533. }
  534. int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT);
  535. static int syslog_action_restricted(int type)
  536. {
  537. if (dmesg_restrict)
  538. return 1;
  539. /*
  540. * Unless restricted, we allow "read all" and "get buffer size"
  541. * for everybody.
  542. */
  543. return type != SYSLOG_ACTION_READ_ALL &&
  544. type != SYSLOG_ACTION_SIZE_BUFFER;
  545. }
  546. static int check_syslog_permissions(int type, int source)
  547. {
  548. /*
  549. * If this is from /proc/kmsg and we've already opened it, then we've
  550. * already done the capabilities checks at open time.
  551. */
  552. if (source == SYSLOG_FROM_PROC && type != SYSLOG_ACTION_OPEN)
  553. goto ok;
  554. if (syslog_action_restricted(type)) {
  555. if (capable(CAP_SYSLOG))
  556. goto ok;
  557. return -EPERM;
  558. }
  559. ok:
  560. return security_syslog(type);
  561. }
  562. static void append_char(char **pp, char *e, char c)
  563. {
  564. if (*pp < e)
  565. *(*pp)++ = c;
  566. }
  567. static ssize_t info_print_ext_header(char *buf, size_t size,
  568. struct printk_info *info)
  569. {
  570. u64 ts_usec = info->ts_nsec;
  571. char caller[20];
  572. #ifdef CONFIG_PRINTK_CALLER
  573. u32 id = info->caller_id;
  574. snprintf(caller, sizeof(caller), ",caller=%c%u",
  575. id & 0x80000000 ? 'C' : 'T', id & ~0x80000000);
  576. #else
  577. caller[0] = '\0';
  578. #endif
  579. do_div(ts_usec, 1000);
  580. return scnprintf(buf, size, "%u,%llu,%llu,%c%s;",
  581. (info->facility << 3) | info->level, info->seq,
  582. ts_usec, info->flags & LOG_CONT ? 'c' : '-', caller);
  583. }
  584. static ssize_t msg_add_ext_text(char *buf, size_t size,
  585. const char *text, size_t text_len,
  586. unsigned char endc)
  587. {
  588. char *p = buf, *e = buf + size;
  589. size_t i;
  590. /* escape non-printable characters */
  591. for (i = 0; i < text_len; i++) {
  592. unsigned char c = text[i];
  593. if (c < ' ' || c >= 127 || c == '\\')
  594. p += scnprintf(p, e - p, "\\x%02x", c);
  595. else
  596. append_char(&p, e, c);
  597. }
  598. append_char(&p, e, endc);
  599. return p - buf;
  600. }
  601. static ssize_t msg_add_dict_text(char *buf, size_t size,
  602. const char *key, const char *val)
  603. {
  604. size_t val_len = strlen(val);
  605. ssize_t len;
  606. if (!val_len)
  607. return 0;
  608. len = msg_add_ext_text(buf, size, "", 0, ' '); /* dict prefix */
  609. len += msg_add_ext_text(buf + len, size - len, key, strlen(key), '=');
  610. len += msg_add_ext_text(buf + len, size - len, val, val_len, '\n');
  611. return len;
  612. }
  613. static ssize_t msg_print_ext_body(char *buf, size_t size,
  614. char *text, size_t text_len,
  615. struct dev_printk_info *dev_info)
  616. {
  617. ssize_t len;
  618. len = msg_add_ext_text(buf, size, text, text_len, '\n');
  619. if (!dev_info)
  620. goto out;
  621. len += msg_add_dict_text(buf + len, size - len, "SUBSYSTEM",
  622. dev_info->subsystem);
  623. len += msg_add_dict_text(buf + len, size - len, "DEVICE",
  624. dev_info->device);
  625. out:
  626. return len;
  627. }
  628. /* /dev/kmsg - userspace message inject/listen interface */
  629. struct devkmsg_user {
  630. atomic64_t seq;
  631. struct ratelimit_state rs;
  632. struct mutex lock;
  633. struct printk_buffers pbufs;
  634. };
  635. static __printf(3, 4) __cold
  636. int devkmsg_emit(int facility, int level, const char *fmt, ...)
  637. {
  638. va_list args;
  639. int r;
  640. va_start(args, fmt);
  641. r = vprintk_emit(facility, level, NULL, fmt, args);
  642. va_end(args);
  643. return r;
  644. }
  645. static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
  646. {
  647. char *buf, *line;
  648. int level = default_message_loglevel;
  649. int facility = 1; /* LOG_USER */
  650. struct file *file = iocb->ki_filp;
  651. struct devkmsg_user *user = file->private_data;
  652. size_t len = iov_iter_count(from);
  653. ssize_t ret = len;
  654. if (len > PRINTKRB_RECORD_MAX)
  655. return -EINVAL;
  656. /* Ignore when user logging is disabled. */
  657. if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
  658. return len;
  659. /* Ratelimit when not explicitly enabled. */
  660. if (!(devkmsg_log & DEVKMSG_LOG_MASK_ON)) {
  661. if (!___ratelimit(&user->rs, current->comm))
  662. return ret;
  663. }
  664. buf = kmalloc(len+1, GFP_KERNEL);
  665. if (buf == NULL)
  666. return -ENOMEM;
  667. buf[len] = '\0';
  668. if (!copy_from_iter_full(buf, len, from)) {
  669. kfree(buf);
  670. return -EFAULT;
  671. }
  672. /*
  673. * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
  674. * the decimal value represents 32bit, the lower 3 bit are the log
  675. * level, the rest are the log facility.
  676. *
  677. * If no prefix or no userspace facility is specified, we
  678. * enforce LOG_USER, to be able to reliably distinguish
  679. * kernel-generated messages from userspace-injected ones.
  680. */
  681. line = buf;
  682. if (line[0] == '<') {
  683. char *endp = NULL;
  684. unsigned int u;
  685. u = simple_strtoul(line + 1, &endp, 10);
  686. if (endp && endp[0] == '>') {
  687. level = LOG_LEVEL(u);
  688. if (LOG_FACILITY(u) != 0)
  689. facility = LOG_FACILITY(u);
  690. endp++;
  691. line = endp;
  692. }
  693. }
  694. devkmsg_emit(facility, level, "%s", line);
  695. kfree(buf);
  696. return ret;
  697. }
  698. static ssize_t devkmsg_read(struct file *file, char __user *buf,
  699. size_t count, loff_t *ppos)
  700. {
  701. struct devkmsg_user *user = file->private_data;
  702. char *outbuf = &user->pbufs.outbuf[0];
  703. struct printk_message pmsg = {
  704. .pbufs = &user->pbufs,
  705. };
  706. ssize_t ret;
  707. ret = mutex_lock_interruptible(&user->lock);
  708. if (ret)
  709. return ret;
  710. if (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, false)) {
  711. if (file->f_flags & O_NONBLOCK) {
  712. ret = -EAGAIN;
  713. goto out;
  714. }
  715. /*
  716. * Guarantee this task is visible on the waitqueue before
  717. * checking the wake condition.
  718. *
  719. * The full memory barrier within set_current_state() of
  720. * prepare_to_wait_event() pairs with the full memory barrier
  721. * within wq_has_sleeper().
  722. *
  723. * This pairs with __wake_up_klogd:A.
  724. */
  725. ret = wait_event_interruptible(log_wait,
  726. printk_get_next_message(&pmsg, atomic64_read(&user->seq), true,
  727. false)); /* LMM(devkmsg_read:A) */
  728. if (ret)
  729. goto out;
  730. }
  731. if (pmsg.dropped) {
  732. /* our last seen message is gone, return error and reset */
  733. atomic64_set(&user->seq, pmsg.seq);
  734. ret = -EPIPE;
  735. goto out;
  736. }
  737. atomic64_set(&user->seq, pmsg.seq + 1);
  738. if (pmsg.outbuf_len > count) {
  739. ret = -EINVAL;
  740. goto out;
  741. }
  742. if (copy_to_user(buf, outbuf, pmsg.outbuf_len)) {
  743. ret = -EFAULT;
  744. goto out;
  745. }
  746. ret = pmsg.outbuf_len;
  747. out:
  748. mutex_unlock(&user->lock);
  749. return ret;
  750. }
  751. /*
  752. * Be careful when modifying this function!!!
  753. *
  754. * Only few operations are supported because the device works only with the
  755. * entire variable length messages (records). Non-standard values are
  756. * returned in the other cases and has been this way for quite some time.
  757. * User space applications might depend on this behavior.
  758. */
  759. static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
  760. {
  761. struct devkmsg_user *user = file->private_data;
  762. loff_t ret = 0;
  763. if (offset)
  764. return -ESPIPE;
  765. switch (whence) {
  766. case SEEK_SET:
  767. /* the first record */
  768. atomic64_set(&user->seq, prb_first_valid_seq(prb));
  769. break;
  770. case SEEK_DATA:
  771. /*
  772. * The first record after the last SYSLOG_ACTION_CLEAR,
  773. * like issued by 'dmesg -c'. Reading /dev/kmsg itself
  774. * changes no global state, and does not clear anything.
  775. */
  776. atomic64_set(&user->seq, latched_seq_read_nolock(&clear_seq));
  777. break;
  778. case SEEK_END:
  779. /* after the last record */
  780. atomic64_set(&user->seq, prb_next_seq(prb));
  781. break;
  782. default:
  783. ret = -EINVAL;
  784. }
  785. return ret;
  786. }
  787. static __poll_t devkmsg_poll(struct file *file, poll_table *wait)
  788. {
  789. struct devkmsg_user *user = file->private_data;
  790. struct printk_info info;
  791. __poll_t ret = 0;
  792. poll_wait(file, &log_wait, wait);
  793. if (prb_read_valid_info(prb, atomic64_read(&user->seq), &info, NULL)) {
  794. /* return error when data has vanished underneath us */
  795. if (info.seq != atomic64_read(&user->seq))
  796. ret = EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
  797. else
  798. ret = EPOLLIN|EPOLLRDNORM;
  799. }
  800. return ret;
  801. }
  802. static int devkmsg_open(struct inode *inode, struct file *file)
  803. {
  804. struct devkmsg_user *user;
  805. int err;
  806. if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
  807. return -EPERM;
  808. /* write-only does not need any file context */
  809. if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
  810. err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
  811. SYSLOG_FROM_READER);
  812. if (err)
  813. return err;
  814. }
  815. user = kvmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
  816. if (!user)
  817. return -ENOMEM;
  818. ratelimit_default_init(&user->rs);
  819. ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE);
  820. mutex_init(&user->lock);
  821. atomic64_set(&user->seq, prb_first_valid_seq(prb));
  822. file->private_data = user;
  823. return 0;
  824. }
  825. static int devkmsg_release(struct inode *inode, struct file *file)
  826. {
  827. struct devkmsg_user *user = file->private_data;
  828. ratelimit_state_exit(&user->rs);
  829. mutex_destroy(&user->lock);
  830. kvfree(user);
  831. return 0;
  832. }
  833. const struct file_operations kmsg_fops = {
  834. .open = devkmsg_open,
  835. .read = devkmsg_read,
  836. .write_iter = devkmsg_write,
  837. .llseek = devkmsg_llseek,
  838. .poll = devkmsg_poll,
  839. .release = devkmsg_release,
  840. };
  841. #ifdef CONFIG_VMCORE_INFO
  842. /*
  843. * This appends the listed symbols to /proc/vmcore
  844. *
  845. * /proc/vmcore is used by various utilities, like crash and makedumpfile to
  846. * obtain access to symbols that are otherwise very difficult to locate. These
  847. * symbols are specifically used so that utilities can access and extract the
  848. * dmesg log from a vmcore file after a crash.
  849. */
  850. void log_buf_vmcoreinfo_setup(void)
  851. {
  852. struct dev_printk_info *dev_info = NULL;
  853. VMCOREINFO_SYMBOL(prb);
  854. VMCOREINFO_SYMBOL(printk_rb_static);
  855. VMCOREINFO_SYMBOL(clear_seq);
  856. /*
  857. * Export struct size and field offsets. User space tools can
  858. * parse it and detect any changes to structure down the line.
  859. */
  860. VMCOREINFO_STRUCT_SIZE(printk_ringbuffer);
  861. VMCOREINFO_OFFSET(printk_ringbuffer, desc_ring);
  862. VMCOREINFO_OFFSET(printk_ringbuffer, text_data_ring);
  863. VMCOREINFO_OFFSET(printk_ringbuffer, fail);
  864. VMCOREINFO_STRUCT_SIZE(prb_desc_ring);
  865. VMCOREINFO_OFFSET(prb_desc_ring, count_bits);
  866. VMCOREINFO_OFFSET(prb_desc_ring, descs);
  867. VMCOREINFO_OFFSET(prb_desc_ring, infos);
  868. VMCOREINFO_OFFSET(prb_desc_ring, head_id);
  869. VMCOREINFO_OFFSET(prb_desc_ring, tail_id);
  870. VMCOREINFO_STRUCT_SIZE(prb_desc);
  871. VMCOREINFO_OFFSET(prb_desc, state_var);
  872. VMCOREINFO_OFFSET(prb_desc, text_blk_lpos);
  873. VMCOREINFO_STRUCT_SIZE(prb_data_blk_lpos);
  874. VMCOREINFO_OFFSET(prb_data_blk_lpos, begin);
  875. VMCOREINFO_OFFSET(prb_data_blk_lpos, next);
  876. VMCOREINFO_STRUCT_SIZE(printk_info);
  877. VMCOREINFO_OFFSET(printk_info, seq);
  878. VMCOREINFO_OFFSET(printk_info, ts_nsec);
  879. VMCOREINFO_OFFSET(printk_info, text_len);
  880. VMCOREINFO_OFFSET(printk_info, caller_id);
  881. VMCOREINFO_OFFSET(printk_info, dev_info);
  882. VMCOREINFO_STRUCT_SIZE(dev_printk_info);
  883. VMCOREINFO_OFFSET(dev_printk_info, subsystem);
  884. VMCOREINFO_LENGTH(printk_info_subsystem, sizeof(dev_info->subsystem));
  885. VMCOREINFO_OFFSET(dev_printk_info, device);
  886. VMCOREINFO_LENGTH(printk_info_device, sizeof(dev_info->device));
  887. VMCOREINFO_STRUCT_SIZE(prb_data_ring);
  888. VMCOREINFO_OFFSET(prb_data_ring, size_bits);
  889. VMCOREINFO_OFFSET(prb_data_ring, data);
  890. VMCOREINFO_OFFSET(prb_data_ring, head_lpos);
  891. VMCOREINFO_OFFSET(prb_data_ring, tail_lpos);
  892. VMCOREINFO_SIZE(atomic_long_t);
  893. VMCOREINFO_TYPE_OFFSET(atomic_long_t, counter);
  894. VMCOREINFO_STRUCT_SIZE(latched_seq);
  895. VMCOREINFO_OFFSET(latched_seq, val);
  896. }
  897. #endif
  898. /* requested log_buf_len from kernel cmdline */
  899. static unsigned long __initdata new_log_buf_len;
  900. /* we practice scaling the ring buffer by powers of 2 */
  901. static void __init log_buf_len_update(u64 size)
  902. {
  903. if (size > (u64)LOG_BUF_LEN_MAX) {
  904. size = (u64)LOG_BUF_LEN_MAX;
  905. pr_err("log_buf over 2G is not supported.\n");
  906. }
  907. if (size)
  908. size = roundup_pow_of_two(size);
  909. if (size > log_buf_len)
  910. new_log_buf_len = (unsigned long)size;
  911. }
  912. /* save requested log_buf_len since it's too early to process it */
  913. static int __init log_buf_len_setup(char *str)
  914. {
  915. u64 size;
  916. if (!str)
  917. return -EINVAL;
  918. size = memparse(str, &str);
  919. log_buf_len_update(size);
  920. return 0;
  921. }
  922. early_param("log_buf_len", log_buf_len_setup);
  923. #ifdef CONFIG_SMP
  924. #define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT)
  925. static void __init log_buf_add_cpu(void)
  926. {
  927. unsigned int cpu_extra;
  928. /*
  929. * archs should set up cpu_possible_bits properly with
  930. * set_cpu_possible() after setup_arch() but just in
  931. * case lets ensure this is valid.
  932. */
  933. if (num_possible_cpus() == 1)
  934. return;
  935. cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN;
  936. /* by default this will only continue through for large > 64 CPUs */
  937. if (cpu_extra <= __LOG_BUF_LEN / 2)
  938. return;
  939. pr_info("log_buf_len individual max cpu contribution: %d bytes\n",
  940. __LOG_CPU_MAX_BUF_LEN);
  941. pr_info("log_buf_len total cpu_extra contributions: %d bytes\n",
  942. cpu_extra);
  943. pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN);
  944. log_buf_len_update(cpu_extra + __LOG_BUF_LEN);
  945. }
  946. #else /* !CONFIG_SMP */
  947. static inline void log_buf_add_cpu(void) {}
  948. #endif /* CONFIG_SMP */
  949. static void __init set_percpu_data_ready(void)
  950. {
  951. __printk_percpu_data_ready = true;
  952. }
  953. static unsigned int __init add_to_rb(struct printk_ringbuffer *rb,
  954. struct printk_record *r)
  955. {
  956. struct prb_reserved_entry e;
  957. struct printk_record dest_r;
  958. prb_rec_init_wr(&dest_r, r->info->text_len);
  959. if (!prb_reserve(&e, rb, &dest_r))
  960. return 0;
  961. memcpy(&dest_r.text_buf[0], &r->text_buf[0], r->info->text_len);
  962. dest_r.info->text_len = r->info->text_len;
  963. dest_r.info->facility = r->info->facility;
  964. dest_r.info->level = r->info->level;
  965. dest_r.info->flags = r->info->flags;
  966. dest_r.info->ts_nsec = r->info->ts_nsec;
  967. dest_r.info->caller_id = r->info->caller_id;
  968. memcpy(&dest_r.info->dev_info, &r->info->dev_info, sizeof(dest_r.info->dev_info));
  969. prb_final_commit(&e);
  970. return prb_record_text_space(&e);
  971. }
  972. static char setup_text_buf[PRINTKRB_RECORD_MAX] __initdata;
  973. void __init setup_log_buf(int early)
  974. {
  975. struct printk_info *new_infos;
  976. unsigned int new_descs_count;
  977. struct prb_desc *new_descs;
  978. struct printk_info info;
  979. struct printk_record r;
  980. unsigned int text_size;
  981. size_t new_descs_size;
  982. size_t new_infos_size;
  983. unsigned long flags;
  984. char *new_log_buf;
  985. unsigned int free;
  986. u64 seq;
  987. /*
  988. * Some archs call setup_log_buf() multiple times - first is very
  989. * early, e.g. from setup_arch(), and second - when percpu_areas
  990. * are initialised.
  991. */
  992. if (!early)
  993. set_percpu_data_ready();
  994. if (log_buf != __log_buf)
  995. return;
  996. if (!early && !new_log_buf_len)
  997. log_buf_add_cpu();
  998. if (!new_log_buf_len)
  999. return;
  1000. new_descs_count = new_log_buf_len >> PRB_AVGBITS;
  1001. if (new_descs_count == 0) {
  1002. pr_err("new_log_buf_len: %lu too small\n", new_log_buf_len);
  1003. return;
  1004. }
  1005. new_log_buf = memblock_alloc(new_log_buf_len, LOG_ALIGN);
  1006. if (unlikely(!new_log_buf)) {
  1007. pr_err("log_buf_len: %lu text bytes not available\n",
  1008. new_log_buf_len);
  1009. return;
  1010. }
  1011. new_descs_size = new_descs_count * sizeof(struct prb_desc);
  1012. new_descs = memblock_alloc(new_descs_size, LOG_ALIGN);
  1013. if (unlikely(!new_descs)) {
  1014. pr_err("log_buf_len: %zu desc bytes not available\n",
  1015. new_descs_size);
  1016. goto err_free_log_buf;
  1017. }
  1018. new_infos_size = new_descs_count * sizeof(struct printk_info);
  1019. new_infos = memblock_alloc(new_infos_size, LOG_ALIGN);
  1020. if (unlikely(!new_infos)) {
  1021. pr_err("log_buf_len: %zu info bytes not available\n",
  1022. new_infos_size);
  1023. goto err_free_descs;
  1024. }
  1025. prb_rec_init_rd(&r, &info, &setup_text_buf[0], sizeof(setup_text_buf));
  1026. prb_init(&printk_rb_dynamic,
  1027. new_log_buf, ilog2(new_log_buf_len),
  1028. new_descs, ilog2(new_descs_count),
  1029. new_infos);
  1030. local_irq_save(flags);
  1031. log_buf_len = new_log_buf_len;
  1032. log_buf = new_log_buf;
  1033. new_log_buf_len = 0;
  1034. free = __LOG_BUF_LEN;
  1035. prb_for_each_record(0, &printk_rb_static, seq, &r) {
  1036. text_size = add_to_rb(&printk_rb_dynamic, &r);
  1037. if (text_size > free)
  1038. free = 0;
  1039. else
  1040. free -= text_size;
  1041. }
  1042. prb = &printk_rb_dynamic;
  1043. local_irq_restore(flags);
  1044. /*
  1045. * Copy any remaining messages that might have appeared from
  1046. * NMI context after copying but before switching to the
  1047. * dynamic buffer.
  1048. */
  1049. prb_for_each_record(seq, &printk_rb_static, seq, &r) {
  1050. text_size = add_to_rb(&printk_rb_dynamic, &r);
  1051. if (text_size > free)
  1052. free = 0;
  1053. else
  1054. free -= text_size;
  1055. }
  1056. if (seq != prb_next_seq(&printk_rb_static)) {
  1057. pr_err("dropped %llu messages\n",
  1058. prb_next_seq(&printk_rb_static) - seq);
  1059. }
  1060. pr_info("log_buf_len: %u bytes\n", log_buf_len);
  1061. pr_info("early log buf free: %u(%u%%)\n",
  1062. free, (free * 100) / __LOG_BUF_LEN);
  1063. return;
  1064. err_free_descs:
  1065. memblock_free(new_descs, new_descs_size);
  1066. err_free_log_buf:
  1067. memblock_free(new_log_buf, new_log_buf_len);
  1068. }
  1069. static bool __read_mostly ignore_loglevel;
  1070. static int __init ignore_loglevel_setup(char *str)
  1071. {
  1072. ignore_loglevel = true;
  1073. pr_info("debug: ignoring loglevel setting.\n");
  1074. return 0;
  1075. }
  1076. early_param("ignore_loglevel", ignore_loglevel_setup);
  1077. module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
  1078. MODULE_PARM_DESC(ignore_loglevel,
  1079. "ignore loglevel setting (prints all kernel messages to the console)");
  1080. static bool suppress_message_printing(int level)
  1081. {
  1082. return (level >= console_loglevel && !ignore_loglevel);
  1083. }
  1084. #ifdef CONFIG_BOOT_PRINTK_DELAY
  1085. static int boot_delay; /* msecs delay after each printk during bootup */
  1086. static unsigned long long loops_per_msec; /* based on boot_delay */
  1087. static int __init boot_delay_setup(char *str)
  1088. {
  1089. unsigned long lpj;
  1090. lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
  1091. loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
  1092. get_option(&str, &boot_delay);
  1093. if (boot_delay > 10 * 1000)
  1094. boot_delay = 0;
  1095. pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
  1096. "HZ: %d, loops_per_msec: %llu\n",
  1097. boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
  1098. return 0;
  1099. }
  1100. early_param("boot_delay", boot_delay_setup);
  1101. static void boot_delay_msec(int level)
  1102. {
  1103. unsigned long long k;
  1104. unsigned long timeout;
  1105. if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING)
  1106. || suppress_message_printing(level)) {
  1107. return;
  1108. }
  1109. k = (unsigned long long)loops_per_msec * boot_delay;
  1110. timeout = jiffies + msecs_to_jiffies(boot_delay);
  1111. while (k) {
  1112. k--;
  1113. cpu_relax();
  1114. /*
  1115. * use (volatile) jiffies to prevent
  1116. * compiler reduction; loop termination via jiffies
  1117. * is secondary and may or may not happen.
  1118. */
  1119. if (time_after(jiffies, timeout))
  1120. break;
  1121. touch_nmi_watchdog();
  1122. }
  1123. }
  1124. #else
  1125. static inline void boot_delay_msec(int level)
  1126. {
  1127. }
  1128. #endif
  1129. static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
  1130. module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
  1131. static size_t print_syslog(unsigned int level, char *buf)
  1132. {
  1133. return sprintf(buf, "<%u>", level);
  1134. }
  1135. static size_t print_time(u64 ts, char *buf)
  1136. {
  1137. unsigned long rem_nsec = do_div(ts, 1000000000);
  1138. return sprintf(buf, "[%5lu.%06lu]",
  1139. (unsigned long)ts, rem_nsec / 1000);
  1140. }
  1141. #ifdef CONFIG_PRINTK_CALLER
  1142. static size_t print_caller(u32 id, char *buf)
  1143. {
  1144. char caller[12];
  1145. snprintf(caller, sizeof(caller), "%c%u",
  1146. id & 0x80000000 ? 'C' : 'T', id & ~0x80000000);
  1147. return sprintf(buf, "[%6s]", caller);
  1148. }
  1149. #else
  1150. #define print_caller(id, buf) 0
  1151. #endif
  1152. static size_t info_print_prefix(const struct printk_info *info, bool syslog,
  1153. bool time, char *buf)
  1154. {
  1155. size_t len = 0;
  1156. if (syslog)
  1157. len = print_syslog((info->facility << 3) | info->level, buf);
  1158. if (time)
  1159. len += print_time(info->ts_nsec, buf + len);
  1160. len += print_caller(info->caller_id, buf + len);
  1161. if (IS_ENABLED(CONFIG_PRINTK_CALLER) || time) {
  1162. buf[len++] = ' ';
  1163. buf[len] = '\0';
  1164. }
  1165. return len;
  1166. }
  1167. /*
  1168. * Prepare the record for printing. The text is shifted within the given
  1169. * buffer to avoid a need for another one. The following operations are
  1170. * done:
  1171. *
  1172. * - Add prefix for each line.
  1173. * - Drop truncated lines that no longer fit into the buffer.
  1174. * - Add the trailing newline that has been removed in vprintk_store().
  1175. * - Add a string terminator.
  1176. *
  1177. * Since the produced string is always terminated, the maximum possible
  1178. * return value is @r->text_buf_size - 1;
  1179. *
  1180. * Return: The length of the updated/prepared text, including the added
  1181. * prefixes and the newline. The terminator is not counted. The dropped
  1182. * line(s) are not counted.
  1183. */
  1184. static size_t record_print_text(struct printk_record *r, bool syslog,
  1185. bool time)
  1186. {
  1187. size_t text_len = r->info->text_len;
  1188. size_t buf_size = r->text_buf_size;
  1189. char *text = r->text_buf;
  1190. char prefix[PRINTK_PREFIX_MAX];
  1191. bool truncated = false;
  1192. size_t prefix_len;
  1193. size_t line_len;
  1194. size_t len = 0;
  1195. char *next;
  1196. /*
  1197. * If the message was truncated because the buffer was not large
  1198. * enough, treat the available text as if it were the full text.
  1199. */
  1200. if (text_len > buf_size)
  1201. text_len = buf_size;
  1202. prefix_len = info_print_prefix(r->info, syslog, time, prefix);
  1203. /*
  1204. * @text_len: bytes of unprocessed text
  1205. * @line_len: bytes of current line _without_ newline
  1206. * @text: pointer to beginning of current line
  1207. * @len: number of bytes prepared in r->text_buf
  1208. */
  1209. for (;;) {
  1210. next = memchr(text, '\n', text_len);
  1211. if (next) {
  1212. line_len = next - text;
  1213. } else {
  1214. /* Drop truncated line(s). */
  1215. if (truncated)
  1216. break;
  1217. line_len = text_len;
  1218. }
  1219. /*
  1220. * Truncate the text if there is not enough space to add the
  1221. * prefix and a trailing newline and a terminator.
  1222. */
  1223. if (len + prefix_len + text_len + 1 + 1 > buf_size) {
  1224. /* Drop even the current line if no space. */
  1225. if (len + prefix_len + line_len + 1 + 1 > buf_size)
  1226. break;
  1227. text_len = buf_size - len - prefix_len - 1 - 1;
  1228. truncated = true;
  1229. }
  1230. memmove(text + prefix_len, text, text_len);
  1231. memcpy(text, prefix, prefix_len);
  1232. /*
  1233. * Increment the prepared length to include the text and
  1234. * prefix that were just moved+copied. Also increment for the
  1235. * newline at the end of this line. If this is the last line,
  1236. * there is no newline, but it will be added immediately below.
  1237. */
  1238. len += prefix_len + line_len + 1;
  1239. if (text_len == line_len) {
  1240. /*
  1241. * This is the last line. Add the trailing newline
  1242. * removed in vprintk_store().
  1243. */
  1244. text[prefix_len + line_len] = '\n';
  1245. break;
  1246. }
  1247. /*
  1248. * Advance beyond the added prefix and the related line with
  1249. * its newline.
  1250. */
  1251. text += prefix_len + line_len + 1;
  1252. /*
  1253. * The remaining text has only decreased by the line with its
  1254. * newline.
  1255. *
  1256. * Note that @text_len can become zero. It happens when @text
  1257. * ended with a newline (either due to truncation or the
  1258. * original string ending with "\n\n"). The loop is correctly
  1259. * repeated and (if not truncated) an empty line with a prefix
  1260. * will be prepared.
  1261. */
  1262. text_len -= line_len + 1;
  1263. }
  1264. /*
  1265. * If a buffer was provided, it will be terminated. Space for the
  1266. * string terminator is guaranteed to be available. The terminator is
  1267. * not counted in the return value.
  1268. */
  1269. if (buf_size > 0)
  1270. r->text_buf[len] = 0;
  1271. return len;
  1272. }
  1273. static size_t get_record_print_text_size(struct printk_info *info,
  1274. unsigned int line_count,
  1275. bool syslog, bool time)
  1276. {
  1277. char prefix[PRINTK_PREFIX_MAX];
  1278. size_t prefix_len;
  1279. prefix_len = info_print_prefix(info, syslog, time, prefix);
  1280. /*
  1281. * Each line will be preceded with a prefix. The intermediate
  1282. * newlines are already within the text, but a final trailing
  1283. * newline will be added.
  1284. */
  1285. return ((prefix_len * line_count) + info->text_len + 1);
  1286. }
  1287. /*
  1288. * Beginning with @start_seq, find the first record where it and all following
  1289. * records up to (but not including) @max_seq fit into @size.
  1290. *
  1291. * @max_seq is simply an upper bound and does not need to exist. If the caller
  1292. * does not require an upper bound, -1 can be used for @max_seq.
  1293. */
  1294. static u64 find_first_fitting_seq(u64 start_seq, u64 max_seq, size_t size,
  1295. bool syslog, bool time)
  1296. {
  1297. struct printk_info info;
  1298. unsigned int line_count;
  1299. size_t len = 0;
  1300. u64 seq;
  1301. /* Determine the size of the records up to @max_seq. */
  1302. prb_for_each_info(start_seq, prb, seq, &info, &line_count) {
  1303. if (info.seq >= max_seq)
  1304. break;
  1305. len += get_record_print_text_size(&info, line_count, syslog, time);
  1306. }
  1307. /*
  1308. * Adjust the upper bound for the next loop to avoid subtracting
  1309. * lengths that were never added.
  1310. */
  1311. if (seq < max_seq)
  1312. max_seq = seq;
  1313. /*
  1314. * Move first record forward until length fits into the buffer. Ignore
  1315. * newest messages that were not counted in the above cycle. Messages
  1316. * might appear and get lost in the meantime. This is a best effort
  1317. * that prevents an infinite loop that could occur with a retry.
  1318. */
  1319. prb_for_each_info(start_seq, prb, seq, &info, &line_count) {
  1320. if (len <= size || info.seq >= max_seq)
  1321. break;
  1322. len -= get_record_print_text_size(&info, line_count, syslog, time);
  1323. }
  1324. return seq;
  1325. }
  1326. /* The caller is responsible for making sure @size is greater than 0. */
  1327. static int syslog_print(char __user *buf, int size)
  1328. {
  1329. struct printk_info info;
  1330. struct printk_record r;
  1331. char *text;
  1332. int len = 0;
  1333. u64 seq;
  1334. text = kmalloc(PRINTK_MESSAGE_MAX, GFP_KERNEL);
  1335. if (!text)
  1336. return -ENOMEM;
  1337. prb_rec_init_rd(&r, &info, text, PRINTK_MESSAGE_MAX);
  1338. mutex_lock(&syslog_lock);
  1339. /*
  1340. * Wait for the @syslog_seq record to be available. @syslog_seq may
  1341. * change while waiting.
  1342. */
  1343. do {
  1344. seq = syslog_seq;
  1345. mutex_unlock(&syslog_lock);
  1346. /*
  1347. * Guarantee this task is visible on the waitqueue before
  1348. * checking the wake condition.
  1349. *
  1350. * The full memory barrier within set_current_state() of
  1351. * prepare_to_wait_event() pairs with the full memory barrier
  1352. * within wq_has_sleeper().
  1353. *
  1354. * This pairs with __wake_up_klogd:A.
  1355. */
  1356. len = wait_event_interruptible(log_wait,
  1357. prb_read_valid(prb, seq, NULL)); /* LMM(syslog_print:A) */
  1358. mutex_lock(&syslog_lock);
  1359. if (len)
  1360. goto out;
  1361. } while (syslog_seq != seq);
  1362. /*
  1363. * Copy records that fit into the buffer. The above cycle makes sure
  1364. * that the first record is always available.
  1365. */
  1366. do {
  1367. size_t n;
  1368. size_t skip;
  1369. int err;
  1370. if (!prb_read_valid(prb, syslog_seq, &r))
  1371. break;
  1372. if (r.info->seq != syslog_seq) {
  1373. /* message is gone, move to next valid one */
  1374. syslog_seq = r.info->seq;
  1375. syslog_partial = 0;
  1376. }
  1377. /*
  1378. * To keep reading/counting partial line consistent,
  1379. * use printk_time value as of the beginning of a line.
  1380. */
  1381. if (!syslog_partial)
  1382. syslog_time = printk_time;
  1383. skip = syslog_partial;
  1384. n = record_print_text(&r, true, syslog_time);
  1385. if (n - syslog_partial <= size) {
  1386. /* message fits into buffer, move forward */
  1387. syslog_seq = r.info->seq + 1;
  1388. n -= syslog_partial;
  1389. syslog_partial = 0;
  1390. } else if (!len){
  1391. /* partial read(), remember position */
  1392. n = size;
  1393. syslog_partial += n;
  1394. } else
  1395. n = 0;
  1396. if (!n)
  1397. break;
  1398. mutex_unlock(&syslog_lock);
  1399. err = copy_to_user(buf, text + skip, n);
  1400. mutex_lock(&syslog_lock);
  1401. if (err) {
  1402. if (!len)
  1403. len = -EFAULT;
  1404. break;
  1405. }
  1406. len += n;
  1407. size -= n;
  1408. buf += n;
  1409. } while (size);
  1410. out:
  1411. mutex_unlock(&syslog_lock);
  1412. kfree(text);
  1413. return len;
  1414. }
  1415. static int syslog_print_all(char __user *buf, int size, bool clear)
  1416. {
  1417. struct printk_info info;
  1418. struct printk_record r;
  1419. char *text;
  1420. int len = 0;
  1421. u64 seq;
  1422. bool time;
  1423. text = kmalloc(PRINTK_MESSAGE_MAX, GFP_KERNEL);
  1424. if (!text)
  1425. return -ENOMEM;
  1426. time = printk_time;
  1427. /*
  1428. * Find first record that fits, including all following records,
  1429. * into the user-provided buffer for this dump.
  1430. */
  1431. seq = find_first_fitting_seq(latched_seq_read_nolock(&clear_seq), -1,
  1432. size, true, time);
  1433. prb_rec_init_rd(&r, &info, text, PRINTK_MESSAGE_MAX);
  1434. prb_for_each_record(seq, prb, seq, &r) {
  1435. int textlen;
  1436. textlen = record_print_text(&r, true, time);
  1437. if (len + textlen > size) {
  1438. seq--;
  1439. break;
  1440. }
  1441. if (copy_to_user(buf + len, text, textlen))
  1442. len = -EFAULT;
  1443. else
  1444. len += textlen;
  1445. if (len < 0)
  1446. break;
  1447. }
  1448. if (clear) {
  1449. mutex_lock(&syslog_lock);
  1450. latched_seq_write(&clear_seq, seq);
  1451. mutex_unlock(&syslog_lock);
  1452. }
  1453. kfree(text);
  1454. return len;
  1455. }
  1456. static void syslog_clear(void)
  1457. {
  1458. mutex_lock(&syslog_lock);
  1459. latched_seq_write(&clear_seq, prb_next_seq(prb));
  1460. mutex_unlock(&syslog_lock);
  1461. }
  1462. int do_syslog(int type, char __user *buf, int len, int source)
  1463. {
  1464. struct printk_info info;
  1465. bool clear = false;
  1466. static int saved_console_loglevel = LOGLEVEL_DEFAULT;
  1467. int error;
  1468. error = check_syslog_permissions(type, source);
  1469. if (error)
  1470. return error;
  1471. switch (type) {
  1472. case SYSLOG_ACTION_CLOSE: /* Close log */
  1473. break;
  1474. case SYSLOG_ACTION_OPEN: /* Open log */
  1475. break;
  1476. case SYSLOG_ACTION_READ: /* Read from log */
  1477. if (!buf || len < 0)
  1478. return -EINVAL;
  1479. if (!len)
  1480. return 0;
  1481. if (!access_ok(buf, len))
  1482. return -EFAULT;
  1483. error = syslog_print(buf, len);
  1484. break;
  1485. /* Read/clear last kernel messages */
  1486. case SYSLOG_ACTION_READ_CLEAR:
  1487. clear = true;
  1488. fallthrough;
  1489. /* Read last kernel messages */
  1490. case SYSLOG_ACTION_READ_ALL:
  1491. if (!buf || len < 0)
  1492. return -EINVAL;
  1493. if (!len)
  1494. return 0;
  1495. if (!access_ok(buf, len))
  1496. return -EFAULT;
  1497. error = syslog_print_all(buf, len, clear);
  1498. break;
  1499. /* Clear ring buffer */
  1500. case SYSLOG_ACTION_CLEAR:
  1501. syslog_clear();
  1502. break;
  1503. /* Disable logging to console */
  1504. case SYSLOG_ACTION_CONSOLE_OFF:
  1505. if (saved_console_loglevel == LOGLEVEL_DEFAULT)
  1506. saved_console_loglevel = console_loglevel;
  1507. console_loglevel = minimum_console_loglevel;
  1508. break;
  1509. /* Enable logging to console */
  1510. case SYSLOG_ACTION_CONSOLE_ON:
  1511. if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
  1512. console_loglevel = saved_console_loglevel;
  1513. saved_console_loglevel = LOGLEVEL_DEFAULT;
  1514. }
  1515. break;
  1516. /* Set level of messages printed to console */
  1517. case SYSLOG_ACTION_CONSOLE_LEVEL:
  1518. if (len < 1 || len > 8)
  1519. return -EINVAL;
  1520. if (len < minimum_console_loglevel)
  1521. len = minimum_console_loglevel;
  1522. console_loglevel = len;
  1523. /* Implicitly re-enable logging to console */
  1524. saved_console_loglevel = LOGLEVEL_DEFAULT;
  1525. break;
  1526. /* Number of chars in the log buffer */
  1527. case SYSLOG_ACTION_SIZE_UNREAD:
  1528. mutex_lock(&syslog_lock);
  1529. if (!prb_read_valid_info(prb, syslog_seq, &info, NULL)) {
  1530. /* No unread messages. */
  1531. mutex_unlock(&syslog_lock);
  1532. return 0;
  1533. }
  1534. if (info.seq != syslog_seq) {
  1535. /* messages are gone, move to first one */
  1536. syslog_seq = info.seq;
  1537. syslog_partial = 0;
  1538. }
  1539. if (source == SYSLOG_FROM_PROC) {
  1540. /*
  1541. * Short-cut for poll(/"proc/kmsg") which simply checks
  1542. * for pending data, not the size; return the count of
  1543. * records, not the length.
  1544. */
  1545. error = prb_next_seq(prb) - syslog_seq;
  1546. } else {
  1547. bool time = syslog_partial ? syslog_time : printk_time;
  1548. unsigned int line_count;
  1549. u64 seq;
  1550. prb_for_each_info(syslog_seq, prb, seq, &info,
  1551. &line_count) {
  1552. error += get_record_print_text_size(&info, line_count,
  1553. true, time);
  1554. time = printk_time;
  1555. }
  1556. error -= syslog_partial;
  1557. }
  1558. mutex_unlock(&syslog_lock);
  1559. break;
  1560. /* Size of the log buffer */
  1561. case SYSLOG_ACTION_SIZE_BUFFER:
  1562. error = log_buf_len;
  1563. break;
  1564. default:
  1565. error = -EINVAL;
  1566. break;
  1567. }
  1568. return error;
  1569. }
  1570. SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
  1571. {
  1572. return do_syslog(type, buf, len, SYSLOG_FROM_READER);
  1573. }
  1574. /*
  1575. * Special console_lock variants that help to reduce the risk of soft-lockups.
  1576. * They allow to pass console_lock to another printk() call using a busy wait.
  1577. */
  1578. #ifdef CONFIG_LOCKDEP
  1579. static struct lockdep_map console_owner_dep_map = {
  1580. .name = "console_owner"
  1581. };
  1582. #endif
  1583. static DEFINE_RAW_SPINLOCK(console_owner_lock);
  1584. static struct task_struct *console_owner;
  1585. static bool console_waiter;
  1586. /**
  1587. * console_lock_spinning_enable - mark beginning of code where another
  1588. * thread might safely busy wait
  1589. *
  1590. * This basically converts console_lock into a spinlock. This marks
  1591. * the section where the console_lock owner can not sleep, because
  1592. * there may be a waiter spinning (like a spinlock). Also it must be
  1593. * ready to hand over the lock at the end of the section.
  1594. */
  1595. void console_lock_spinning_enable(void)
  1596. {
  1597. /*
  1598. * Do not use spinning in panic(). The panic CPU wants to keep the lock.
  1599. * Non-panic CPUs abandon the flush anyway.
  1600. *
  1601. * Just keep the lockdep annotation. The panic-CPU should avoid
  1602. * taking console_owner_lock because it might cause a deadlock.
  1603. * This looks like the easiest way how to prevent false lockdep
  1604. * reports without handling races a lockless way.
  1605. */
  1606. if (panic_in_progress())
  1607. goto lockdep;
  1608. raw_spin_lock(&console_owner_lock);
  1609. console_owner = current;
  1610. raw_spin_unlock(&console_owner_lock);
  1611. lockdep:
  1612. /* The waiter may spin on us after setting console_owner */
  1613. spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
  1614. }
  1615. /**
  1616. * console_lock_spinning_disable_and_check - mark end of code where another
  1617. * thread was able to busy wait and check if there is a waiter
  1618. * @cookie: cookie returned from console_srcu_read_lock()
  1619. *
  1620. * This is called at the end of the section where spinning is allowed.
  1621. * It has two functions. First, it is a signal that it is no longer
  1622. * safe to start busy waiting for the lock. Second, it checks if
  1623. * there is a busy waiter and passes the lock rights to her.
  1624. *
  1625. * Important: Callers lose both the console_lock and the SRCU read lock if
  1626. * there was a busy waiter. They must not touch items synchronized by
  1627. * console_lock or SRCU read lock in this case.
  1628. *
  1629. * Return: 1 if the lock rights were passed, 0 otherwise.
  1630. */
  1631. int console_lock_spinning_disable_and_check(int cookie)
  1632. {
  1633. int waiter;
  1634. /*
  1635. * Ignore spinning waiters during panic() because they might get stopped
  1636. * or blocked at any time,
  1637. *
  1638. * It is safe because nobody is allowed to start spinning during panic
  1639. * in the first place. If there has been a waiter then non panic CPUs
  1640. * might stay spinning. They would get stopped anyway. The panic context
  1641. * will never start spinning and an interrupted spin on panic CPU will
  1642. * never continue.
  1643. */
  1644. if (panic_in_progress()) {
  1645. /* Keep lockdep happy. */
  1646. spin_release(&console_owner_dep_map, _THIS_IP_);
  1647. return 0;
  1648. }
  1649. raw_spin_lock(&console_owner_lock);
  1650. waiter = READ_ONCE(console_waiter);
  1651. console_owner = NULL;
  1652. raw_spin_unlock(&console_owner_lock);
  1653. if (!waiter) {
  1654. spin_release(&console_owner_dep_map, _THIS_IP_);
  1655. return 0;
  1656. }
  1657. /* The waiter is now free to continue */
  1658. WRITE_ONCE(console_waiter, false);
  1659. spin_release(&console_owner_dep_map, _THIS_IP_);
  1660. /*
  1661. * Preserve lockdep lock ordering. Release the SRCU read lock before
  1662. * releasing the console_lock.
  1663. */
  1664. console_srcu_read_unlock(cookie);
  1665. /*
  1666. * Hand off console_lock to waiter. The waiter will perform
  1667. * the up(). After this, the waiter is the console_lock owner.
  1668. */
  1669. mutex_release(&console_lock_dep_map, _THIS_IP_);
  1670. return 1;
  1671. }
  1672. /**
  1673. * console_trylock_spinning - try to get console_lock by busy waiting
  1674. *
  1675. * This allows to busy wait for the console_lock when the current
  1676. * owner is running in specially marked sections. It means that
  1677. * the current owner is running and cannot reschedule until it
  1678. * is ready to lose the lock.
  1679. *
  1680. * Return: 1 if we got the lock, 0 othrewise
  1681. */
  1682. static int console_trylock_spinning(void)
  1683. {
  1684. struct task_struct *owner = NULL;
  1685. bool waiter;
  1686. bool spin = false;
  1687. unsigned long flags;
  1688. if (console_trylock())
  1689. return 1;
  1690. /*
  1691. * It's unsafe to spin once a panic has begun. If we are the
  1692. * panic CPU, we may have already halted the owner of the
  1693. * console_sem. If we are not the panic CPU, then we should
  1694. * avoid taking console_sem, so the panic CPU has a better
  1695. * chance of cleanly acquiring it later.
  1696. */
  1697. if (panic_in_progress())
  1698. return 0;
  1699. printk_safe_enter_irqsave(flags);
  1700. raw_spin_lock(&console_owner_lock);
  1701. owner = READ_ONCE(console_owner);
  1702. waiter = READ_ONCE(console_waiter);
  1703. if (!waiter && owner && owner != current) {
  1704. WRITE_ONCE(console_waiter, true);
  1705. spin = true;
  1706. }
  1707. raw_spin_unlock(&console_owner_lock);
  1708. /*
  1709. * If there is an active printk() writing to the
  1710. * consoles, instead of having it write our data too,
  1711. * see if we can offload that load from the active
  1712. * printer, and do some printing ourselves.
  1713. * Go into a spin only if there isn't already a waiter
  1714. * spinning, and there is an active printer, and
  1715. * that active printer isn't us (recursive printk?).
  1716. */
  1717. if (!spin) {
  1718. printk_safe_exit_irqrestore(flags);
  1719. return 0;
  1720. }
  1721. /* We spin waiting for the owner to release us */
  1722. spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
  1723. /* Owner will clear console_waiter on hand off */
  1724. while (READ_ONCE(console_waiter))
  1725. cpu_relax();
  1726. spin_release(&console_owner_dep_map, _THIS_IP_);
  1727. printk_safe_exit_irqrestore(flags);
  1728. /*
  1729. * The owner passed the console lock to us.
  1730. * Since we did not spin on console lock, annotate
  1731. * this as a trylock. Otherwise lockdep will
  1732. * complain.
  1733. */
  1734. mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_);
  1735. /*
  1736. * Update @console_may_schedule for trylock because the previous
  1737. * owner may have been schedulable.
  1738. */
  1739. console_may_schedule = 0;
  1740. return 1;
  1741. }
  1742. /*
  1743. * Recursion is tracked separately on each CPU. If NMIs are supported, an
  1744. * additional NMI context per CPU is also separately tracked. Until per-CPU
  1745. * is available, a separate "early tracking" is performed.
  1746. */
  1747. static DEFINE_PER_CPU(u8, printk_count);
  1748. static u8 printk_count_early;
  1749. #ifdef CONFIG_HAVE_NMI
  1750. static DEFINE_PER_CPU(u8, printk_count_nmi);
  1751. static u8 printk_count_nmi_early;
  1752. #endif
  1753. /*
  1754. * Recursion is limited to keep the output sane. printk() should not require
  1755. * more than 1 level of recursion (allowing, for example, printk() to trigger
  1756. * a WARN), but a higher value is used in case some printk-internal errors
  1757. * exist, such as the ringbuffer validation checks failing.
  1758. */
  1759. #define PRINTK_MAX_RECURSION 3
  1760. /*
  1761. * Return a pointer to the dedicated counter for the CPU+context of the
  1762. * caller.
  1763. */
  1764. static u8 *__printk_recursion_counter(void)
  1765. {
  1766. #ifdef CONFIG_HAVE_NMI
  1767. if (in_nmi()) {
  1768. if (printk_percpu_data_ready())
  1769. return this_cpu_ptr(&printk_count_nmi);
  1770. return &printk_count_nmi_early;
  1771. }
  1772. #endif
  1773. if (printk_percpu_data_ready())
  1774. return this_cpu_ptr(&printk_count);
  1775. return &printk_count_early;
  1776. }
  1777. /*
  1778. * Enter recursion tracking. Interrupts are disabled to simplify tracking.
  1779. * The caller must check the boolean return value to see if the recursion is
  1780. * allowed. On failure, interrupts are not disabled.
  1781. *
  1782. * @recursion_ptr must be a variable of type (u8 *) and is the same variable
  1783. * that is passed to printk_exit_irqrestore().
  1784. */
  1785. #define printk_enter_irqsave(recursion_ptr, flags) \
  1786. ({ \
  1787. bool success = true; \
  1788. \
  1789. typecheck(u8 *, recursion_ptr); \
  1790. local_irq_save(flags); \
  1791. (recursion_ptr) = __printk_recursion_counter(); \
  1792. if (*(recursion_ptr) > PRINTK_MAX_RECURSION) { \
  1793. local_irq_restore(flags); \
  1794. success = false; \
  1795. } else { \
  1796. (*(recursion_ptr))++; \
  1797. } \
  1798. success; \
  1799. })
  1800. /* Exit recursion tracking, restoring interrupts. */
  1801. #define printk_exit_irqrestore(recursion_ptr, flags) \
  1802. do { \
  1803. typecheck(u8 *, recursion_ptr); \
  1804. (*(recursion_ptr))--; \
  1805. local_irq_restore(flags); \
  1806. } while (0)
  1807. int printk_delay_msec __read_mostly;
  1808. static inline void printk_delay(int level)
  1809. {
  1810. boot_delay_msec(level);
  1811. if (unlikely(printk_delay_msec)) {
  1812. int m = printk_delay_msec;
  1813. while (m--) {
  1814. mdelay(1);
  1815. touch_nmi_watchdog();
  1816. }
  1817. }
  1818. }
  1819. static inline u32 printk_caller_id(void)
  1820. {
  1821. return in_task() ? task_pid_nr(current) :
  1822. 0x80000000 + smp_processor_id();
  1823. }
  1824. /**
  1825. * printk_parse_prefix - Parse level and control flags.
  1826. *
  1827. * @text: The terminated text message.
  1828. * @level: A pointer to the current level value, will be updated.
  1829. * @flags: A pointer to the current printk_info flags, will be updated.
  1830. *
  1831. * @level may be NULL if the caller is not interested in the parsed value.
  1832. * Otherwise the variable pointed to by @level must be set to
  1833. * LOGLEVEL_DEFAULT in order to be updated with the parsed value.
  1834. *
  1835. * @flags may be NULL if the caller is not interested in the parsed value.
  1836. * Otherwise the variable pointed to by @flags will be OR'd with the parsed
  1837. * value.
  1838. *
  1839. * Return: The length of the parsed level and control flags.
  1840. */
  1841. u16 printk_parse_prefix(const char *text, int *level,
  1842. enum printk_info_flags *flags)
  1843. {
  1844. u16 prefix_len = 0;
  1845. int kern_level;
  1846. while (*text) {
  1847. kern_level = printk_get_level(text);
  1848. if (!kern_level)
  1849. break;
  1850. switch (kern_level) {
  1851. case '0' ... '7':
  1852. if (level && *level == LOGLEVEL_DEFAULT)
  1853. *level = kern_level - '0';
  1854. break;
  1855. case 'c': /* KERN_CONT */
  1856. if (flags)
  1857. *flags |= LOG_CONT;
  1858. }
  1859. prefix_len += 2;
  1860. text += 2;
  1861. }
  1862. return prefix_len;
  1863. }
  1864. __printf(5, 0)
  1865. static u16 printk_sprint(char *text, u16 size, int facility,
  1866. enum printk_info_flags *flags, const char *fmt,
  1867. va_list args)
  1868. {
  1869. u16 text_len;
  1870. text_len = vscnprintf(text, size, fmt, args);
  1871. /* Mark and strip a trailing newline. */
  1872. if (text_len && text[text_len - 1] == '\n') {
  1873. text_len--;
  1874. *flags |= LOG_NEWLINE;
  1875. }
  1876. /* Strip log level and control flags. */
  1877. if (facility == 0) {
  1878. u16 prefix_len;
  1879. prefix_len = printk_parse_prefix(text, NULL, NULL);
  1880. if (prefix_len) {
  1881. text_len -= prefix_len;
  1882. memmove(text, text + prefix_len, text_len);
  1883. }
  1884. }
  1885. trace_console(text, text_len);
  1886. return text_len;
  1887. }
  1888. __printf(4, 0)
  1889. int vprintk_store(int facility, int level,
  1890. const struct dev_printk_info *dev_info,
  1891. const char *fmt, va_list args)
  1892. {
  1893. struct prb_reserved_entry e;
  1894. enum printk_info_flags flags = 0;
  1895. struct printk_record r;
  1896. unsigned long irqflags;
  1897. u16 trunc_msg_len = 0;
  1898. char prefix_buf[8];
  1899. u8 *recursion_ptr;
  1900. u16 reserve_size;
  1901. va_list args2;
  1902. u32 caller_id;
  1903. u16 text_len;
  1904. int ret = 0;
  1905. u64 ts_nsec;
  1906. if (!printk_enter_irqsave(recursion_ptr, irqflags))
  1907. return 0;
  1908. /*
  1909. * Since the duration of printk() can vary depending on the message
  1910. * and state of the ringbuffer, grab the timestamp now so that it is
  1911. * close to the call of printk(). This provides a more deterministic
  1912. * timestamp with respect to the caller.
  1913. */
  1914. ts_nsec = local_clock();
  1915. caller_id = printk_caller_id();
  1916. /*
  1917. * The sprintf needs to come first since the syslog prefix might be
  1918. * passed in as a parameter. An extra byte must be reserved so that
  1919. * later the vscnprintf() into the reserved buffer has room for the
  1920. * terminating '\0', which is not counted by vsnprintf().
  1921. */
  1922. va_copy(args2, args);
  1923. reserve_size = vsnprintf(&prefix_buf[0], sizeof(prefix_buf), fmt, args2) + 1;
  1924. va_end(args2);
  1925. if (reserve_size > PRINTKRB_RECORD_MAX)
  1926. reserve_size = PRINTKRB_RECORD_MAX;
  1927. /* Extract log level or control flags. */
  1928. if (facility == 0)
  1929. printk_parse_prefix(&prefix_buf[0], &level, &flags);
  1930. if (level == LOGLEVEL_DEFAULT)
  1931. level = default_message_loglevel;
  1932. if (dev_info)
  1933. flags |= LOG_NEWLINE;
  1934. if (flags & LOG_CONT) {
  1935. prb_rec_init_wr(&r, reserve_size);
  1936. if (prb_reserve_in_last(&e, prb, &r, caller_id, PRINTKRB_RECORD_MAX)) {
  1937. text_len = printk_sprint(&r.text_buf[r.info->text_len], reserve_size,
  1938. facility, &flags, fmt, args);
  1939. r.info->text_len += text_len;
  1940. if (flags & LOG_NEWLINE) {
  1941. r.info->flags |= LOG_NEWLINE;
  1942. prb_final_commit(&e);
  1943. } else {
  1944. prb_commit(&e);
  1945. }
  1946. ret = text_len;
  1947. goto out;
  1948. }
  1949. }
  1950. /*
  1951. * Explicitly initialize the record before every prb_reserve() call.
  1952. * prb_reserve_in_last() and prb_reserve() purposely invalidate the
  1953. * structure when they fail.
  1954. */
  1955. prb_rec_init_wr(&r, reserve_size);
  1956. if (!prb_reserve(&e, prb, &r)) {
  1957. /* truncate the message if it is too long for empty buffer */
  1958. truncate_msg(&reserve_size, &trunc_msg_len);
  1959. prb_rec_init_wr(&r, reserve_size + trunc_msg_len);
  1960. if (!prb_reserve(&e, prb, &r))
  1961. goto out;
  1962. }
  1963. /* fill message */
  1964. text_len = printk_sprint(&r.text_buf[0], reserve_size, facility, &flags, fmt, args);
  1965. if (trunc_msg_len)
  1966. memcpy(&r.text_buf[text_len], trunc_msg, trunc_msg_len);
  1967. r.info->text_len = text_len + trunc_msg_len;
  1968. r.info->facility = facility;
  1969. r.info->level = level & 7;
  1970. r.info->flags = flags & 0x1f;
  1971. r.info->ts_nsec = ts_nsec;
  1972. r.info->caller_id = caller_id;
  1973. if (dev_info)
  1974. memcpy(&r.info->dev_info, dev_info, sizeof(r.info->dev_info));
  1975. /* A message without a trailing newline can be continued. */
  1976. if (!(flags & LOG_NEWLINE))
  1977. prb_commit(&e);
  1978. else
  1979. prb_final_commit(&e);
  1980. ret = text_len + trunc_msg_len;
  1981. out:
  1982. printk_exit_irqrestore(recursion_ptr, irqflags);
  1983. return ret;
  1984. }
  1985. /*
  1986. * This acts as a one-way switch to allow legacy consoles to print from
  1987. * the printk() caller context on a panic CPU. It also attempts to flush
  1988. * the legacy consoles in this context.
  1989. */
  1990. void printk_legacy_allow_panic_sync(void)
  1991. {
  1992. struct console_flush_type ft;
  1993. legacy_allow_panic_sync = true;
  1994. printk_get_console_flush_type(&ft);
  1995. if (ft.legacy_direct) {
  1996. if (console_trylock())
  1997. console_unlock();
  1998. }
  1999. }
  2000. asmlinkage int vprintk_emit(int facility, int level,
  2001. const struct dev_printk_info *dev_info,
  2002. const char *fmt, va_list args)
  2003. {
  2004. struct console_flush_type ft;
  2005. int printed_len;
  2006. /* Suppress unimportant messages after panic happens */
  2007. if (unlikely(suppress_printk))
  2008. return 0;
  2009. /*
  2010. * The messages on the panic CPU are the most important. If
  2011. * non-panic CPUs are generating any messages, they will be
  2012. * silently dropped.
  2013. */
  2014. if (other_cpu_in_panic() && !panic_triggering_all_cpu_backtrace)
  2015. return 0;
  2016. printk_get_console_flush_type(&ft);
  2017. /* If called from the scheduler, we can not call up(). */
  2018. if (level == LOGLEVEL_SCHED) {
  2019. level = LOGLEVEL_DEFAULT;
  2020. ft.legacy_offload |= ft.legacy_direct;
  2021. ft.legacy_direct = false;
  2022. }
  2023. printk_delay(level);
  2024. printed_len = vprintk_store(facility, level, dev_info, fmt, args);
  2025. if (ft.nbcon_atomic)
  2026. nbcon_atomic_flush_pending();
  2027. if (ft.nbcon_offload)
  2028. nbcon_kthreads_wake();
  2029. if (ft.legacy_direct) {
  2030. /*
  2031. * The caller may be holding system-critical or
  2032. * timing-sensitive locks. Disable preemption during
  2033. * printing of all remaining records to all consoles so that
  2034. * this context can return as soon as possible. Hopefully
  2035. * another printk() caller will take over the printing.
  2036. */
  2037. preempt_disable();
  2038. /*
  2039. * Try to acquire and then immediately release the console
  2040. * semaphore. The release will print out buffers. With the
  2041. * spinning variant, this context tries to take over the
  2042. * printing from another printing context.
  2043. */
  2044. if (console_trylock_spinning())
  2045. console_unlock();
  2046. preempt_enable();
  2047. }
  2048. if (ft.legacy_offload)
  2049. defer_console_output();
  2050. else
  2051. wake_up_klogd();
  2052. return printed_len;
  2053. }
  2054. EXPORT_SYMBOL(vprintk_emit);
  2055. int vprintk_default(const char *fmt, va_list args)
  2056. {
  2057. return vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
  2058. }
  2059. EXPORT_SYMBOL_GPL(vprintk_default);
  2060. asmlinkage __visible int _printk(const char *fmt, ...)
  2061. {
  2062. va_list args;
  2063. int r;
  2064. va_start(args, fmt);
  2065. r = vprintk(fmt, args);
  2066. va_end(args);
  2067. return r;
  2068. }
  2069. EXPORT_SYMBOL(_printk);
  2070. static bool pr_flush(int timeout_ms, bool reset_on_progress);
  2071. static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress);
  2072. #else /* CONFIG_PRINTK */
  2073. #define printk_time false
  2074. #define prb_read_valid(rb, seq, r) false
  2075. #define prb_first_valid_seq(rb) 0
  2076. #define prb_next_seq(rb) 0
  2077. static u64 syslog_seq;
  2078. static bool pr_flush(int timeout_ms, bool reset_on_progress) { return true; }
  2079. static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; }
  2080. #endif /* CONFIG_PRINTK */
  2081. #ifdef CONFIG_EARLY_PRINTK
  2082. struct console *early_console;
  2083. asmlinkage __visible void early_printk(const char *fmt, ...)
  2084. {
  2085. va_list ap;
  2086. char buf[512];
  2087. int n;
  2088. if (!early_console)
  2089. return;
  2090. va_start(ap, fmt);
  2091. n = vscnprintf(buf, sizeof(buf), fmt, ap);
  2092. va_end(ap);
  2093. early_console->write(early_console, buf, n);
  2094. }
  2095. #endif
  2096. static void set_user_specified(struct console_cmdline *c, bool user_specified)
  2097. {
  2098. if (!user_specified)
  2099. return;
  2100. /*
  2101. * @c console was defined by the user on the command line.
  2102. * Do not clear when added twice also by SPCR or the device tree.
  2103. */
  2104. c->user_specified = true;
  2105. /* At least one console defined by the user on the command line. */
  2106. console_set_on_cmdline = 1;
  2107. }
  2108. static int __add_preferred_console(const char *name, const short idx,
  2109. const char *devname, char *options,
  2110. char *brl_options, bool user_specified)
  2111. {
  2112. struct console_cmdline *c;
  2113. int i;
  2114. if (!name && !devname)
  2115. return -EINVAL;
  2116. /*
  2117. * We use a signed short index for struct console for device drivers to
  2118. * indicate a not yet assigned index or port. However, a negative index
  2119. * value is not valid when the console name and index are defined on
  2120. * the command line.
  2121. */
  2122. if (name && idx < 0)
  2123. return -EINVAL;
  2124. /*
  2125. * See if this tty is not yet registered, and
  2126. * if we have a slot free.
  2127. */
  2128. for (i = 0, c = console_cmdline;
  2129. i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]);
  2130. i++, c++) {
  2131. if ((name && strcmp(c->name, name) == 0 && c->index == idx) ||
  2132. (devname && strcmp(c->devname, devname) == 0)) {
  2133. if (!brl_options)
  2134. preferred_console = i;
  2135. set_user_specified(c, user_specified);
  2136. return 0;
  2137. }
  2138. }
  2139. if (i == MAX_CMDLINECONSOLES)
  2140. return -E2BIG;
  2141. if (!brl_options)
  2142. preferred_console = i;
  2143. if (name)
  2144. strscpy(c->name, name);
  2145. if (devname)
  2146. strscpy(c->devname, devname);
  2147. c->options = options;
  2148. set_user_specified(c, user_specified);
  2149. braille_set_options(c, brl_options);
  2150. c->index = idx;
  2151. return 0;
  2152. }
  2153. static int __init console_msg_format_setup(char *str)
  2154. {
  2155. if (!strcmp(str, "syslog"))
  2156. console_msg_format = MSG_FORMAT_SYSLOG;
  2157. if (!strcmp(str, "default"))
  2158. console_msg_format = MSG_FORMAT_DEFAULT;
  2159. return 1;
  2160. }
  2161. __setup("console_msg_format=", console_msg_format_setup);
  2162. /*
  2163. * Set up a console. Called via do_early_param() in init/main.c
  2164. * for each "console=" parameter in the boot command line.
  2165. */
  2166. static int __init console_setup(char *str)
  2167. {
  2168. static_assert(sizeof(console_cmdline[0].devname) >= sizeof(console_cmdline[0].name) + 4);
  2169. char buf[sizeof(console_cmdline[0].devname)];
  2170. char *brl_options = NULL;
  2171. char *ttyname = NULL;
  2172. char *devname = NULL;
  2173. char *options;
  2174. char *s;
  2175. int idx;
  2176. /*
  2177. * console="" or console=null have been suggested as a way to
  2178. * disable console output. Use ttynull that has been created
  2179. * for exactly this purpose.
  2180. */
  2181. if (str[0] == 0 || strcmp(str, "null") == 0) {
  2182. __add_preferred_console("ttynull", 0, NULL, NULL, NULL, true);
  2183. return 1;
  2184. }
  2185. if (_braille_console_setup(&str, &brl_options))
  2186. return 1;
  2187. /* For a DEVNAME:0.0 style console the character device is unknown early */
  2188. if (strchr(str, ':'))
  2189. devname = buf;
  2190. else
  2191. ttyname = buf;
  2192. /*
  2193. * Decode str into name, index, options.
  2194. */
  2195. if (ttyname && isdigit(str[0]))
  2196. scnprintf(buf, sizeof(buf), "ttyS%s", str);
  2197. else
  2198. strscpy(buf, str);
  2199. options = strchr(str, ',');
  2200. if (options)
  2201. *(options++) = 0;
  2202. #ifdef __sparc__
  2203. if (!strcmp(str, "ttya"))
  2204. strscpy(buf, "ttyS0");
  2205. if (!strcmp(str, "ttyb"))
  2206. strscpy(buf, "ttyS1");
  2207. #endif
  2208. for (s = buf; *s; s++)
  2209. if ((ttyname && isdigit(*s)) || *s == ',')
  2210. break;
  2211. /* @idx will get defined when devname matches. */
  2212. if (devname)
  2213. idx = -1;
  2214. else
  2215. idx = simple_strtoul(s, NULL, 10);
  2216. *s = 0;
  2217. __add_preferred_console(ttyname, idx, devname, options, brl_options, true);
  2218. return 1;
  2219. }
  2220. __setup("console=", console_setup);
  2221. /**
  2222. * add_preferred_console - add a device to the list of preferred consoles.
  2223. * @name: device name
  2224. * @idx: device index
  2225. * @options: options for this console
  2226. *
  2227. * The last preferred console added will be used for kernel messages
  2228. * and stdin/out/err for init. Normally this is used by console_setup
  2229. * above to handle user-supplied console arguments; however it can also
  2230. * be used by arch-specific code either to override the user or more
  2231. * commonly to provide a default console (ie from PROM variables) when
  2232. * the user has not supplied one.
  2233. */
  2234. int add_preferred_console(const char *name, const short idx, char *options)
  2235. {
  2236. return __add_preferred_console(name, idx, NULL, options, NULL, false);
  2237. }
  2238. /**
  2239. * match_devname_and_update_preferred_console - Update a preferred console
  2240. * when matching devname is found.
  2241. * @devname: DEVNAME:0.0 style device name
  2242. * @name: Name of the corresponding console driver, e.g. "ttyS"
  2243. * @idx: Console index, e.g. port number.
  2244. *
  2245. * The function checks whether a device with the given @devname is
  2246. * preferred via the console=DEVNAME:0.0 command line option.
  2247. * It fills the missing console driver name and console index
  2248. * so that a later register_console() call could find (match)
  2249. * and enable this device.
  2250. *
  2251. * It might be used when a driver subsystem initializes particular
  2252. * devices with already known DEVNAME:0.0 style names. And it
  2253. * could predict which console driver name and index this device
  2254. * would later get associated with.
  2255. *
  2256. * Return: 0 on success, negative error code on failure.
  2257. */
  2258. int match_devname_and_update_preferred_console(const char *devname,
  2259. const char *name,
  2260. const short idx)
  2261. {
  2262. struct console_cmdline *c = console_cmdline;
  2263. int i;
  2264. if (!devname || !strlen(devname) || !name || !strlen(name) || idx < 0)
  2265. return -EINVAL;
  2266. for (i = 0; i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]);
  2267. i++, c++) {
  2268. if (!strcmp(devname, c->devname)) {
  2269. pr_info("associate the preferred console \"%s\" with \"%s%d\"\n",
  2270. devname, name, idx);
  2271. strscpy(c->name, name);
  2272. c->index = idx;
  2273. return 0;
  2274. }
  2275. }
  2276. return -ENOENT;
  2277. }
  2278. EXPORT_SYMBOL_GPL(match_devname_and_update_preferred_console);
  2279. bool console_suspend_enabled = true;
  2280. EXPORT_SYMBOL(console_suspend_enabled);
  2281. static int __init console_suspend_disable(char *str)
  2282. {
  2283. console_suspend_enabled = false;
  2284. return 1;
  2285. }
  2286. __setup("no_console_suspend", console_suspend_disable);
  2287. module_param_named(console_suspend, console_suspend_enabled,
  2288. bool, S_IRUGO | S_IWUSR);
  2289. MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
  2290. " and hibernate operations");
  2291. static bool printk_console_no_auto_verbose;
  2292. void console_verbose(void)
  2293. {
  2294. if (console_loglevel && !printk_console_no_auto_verbose)
  2295. console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
  2296. }
  2297. EXPORT_SYMBOL_GPL(console_verbose);
  2298. module_param_named(console_no_auto_verbose, printk_console_no_auto_verbose, bool, 0644);
  2299. MODULE_PARM_DESC(console_no_auto_verbose, "Disable console loglevel raise to highest on oops/panic/etc");
  2300. /**
  2301. * suspend_console - suspend the console subsystem
  2302. *
  2303. * This disables printk() while we go into suspend states
  2304. */
  2305. void suspend_console(void)
  2306. {
  2307. struct console *con;
  2308. if (!console_suspend_enabled)
  2309. return;
  2310. pr_info("Suspending console(s) (use no_console_suspend to debug)\n");
  2311. pr_flush(1000, true);
  2312. console_list_lock();
  2313. for_each_console(con)
  2314. console_srcu_write_flags(con, con->flags | CON_SUSPENDED);
  2315. console_list_unlock();
  2316. /*
  2317. * Ensure that all SRCU list walks have completed. All printing
  2318. * contexts must be able to see that they are suspended so that it
  2319. * is guaranteed that all printing has stopped when this function
  2320. * completes.
  2321. */
  2322. synchronize_srcu(&console_srcu);
  2323. }
  2324. void resume_console(void)
  2325. {
  2326. struct console_flush_type ft;
  2327. struct console *con;
  2328. if (!console_suspend_enabled)
  2329. return;
  2330. console_list_lock();
  2331. for_each_console(con)
  2332. console_srcu_write_flags(con, con->flags & ~CON_SUSPENDED);
  2333. console_list_unlock();
  2334. /*
  2335. * Ensure that all SRCU list walks have completed. All printing
  2336. * contexts must be able to see they are no longer suspended so
  2337. * that they are guaranteed to wake up and resume printing.
  2338. */
  2339. synchronize_srcu(&console_srcu);
  2340. printk_get_console_flush_type(&ft);
  2341. if (ft.nbcon_offload)
  2342. nbcon_kthreads_wake();
  2343. if (ft.legacy_offload)
  2344. defer_console_output();
  2345. pr_flush(1000, true);
  2346. }
  2347. /**
  2348. * console_cpu_notify - print deferred console messages after CPU hotplug
  2349. * @cpu: unused
  2350. *
  2351. * If printk() is called from a CPU that is not online yet, the messages
  2352. * will be printed on the console only if there are CON_ANYTIME consoles.
  2353. * This function is called when a new CPU comes online (or fails to come
  2354. * up) or goes offline.
  2355. */
  2356. static int console_cpu_notify(unsigned int cpu)
  2357. {
  2358. struct console_flush_type ft;
  2359. if (!cpuhp_tasks_frozen) {
  2360. printk_get_console_flush_type(&ft);
  2361. if (ft.nbcon_atomic)
  2362. nbcon_atomic_flush_pending();
  2363. if (ft.legacy_direct) {
  2364. if (console_trylock())
  2365. console_unlock();
  2366. }
  2367. }
  2368. return 0;
  2369. }
  2370. /**
  2371. * console_lock - block the console subsystem from printing
  2372. *
  2373. * Acquires a lock which guarantees that no consoles will
  2374. * be in or enter their write() callback.
  2375. *
  2376. * Can sleep, returns nothing.
  2377. */
  2378. void console_lock(void)
  2379. {
  2380. might_sleep();
  2381. /* On panic, the console_lock must be left to the panic cpu. */
  2382. while (other_cpu_in_panic())
  2383. msleep(1000);
  2384. down_console_sem();
  2385. console_locked = 1;
  2386. console_may_schedule = 1;
  2387. }
  2388. EXPORT_SYMBOL(console_lock);
  2389. /**
  2390. * console_trylock - try to block the console subsystem from printing
  2391. *
  2392. * Try to acquire a lock which guarantees that no consoles will
  2393. * be in or enter their write() callback.
  2394. *
  2395. * returns 1 on success, and 0 on failure to acquire the lock.
  2396. */
  2397. int console_trylock(void)
  2398. {
  2399. /* On panic, the console_lock must be left to the panic cpu. */
  2400. if (other_cpu_in_panic())
  2401. return 0;
  2402. if (down_trylock_console_sem())
  2403. return 0;
  2404. console_locked = 1;
  2405. console_may_schedule = 0;
  2406. return 1;
  2407. }
  2408. EXPORT_SYMBOL(console_trylock);
  2409. int is_console_locked(void)
  2410. {
  2411. return console_locked;
  2412. }
  2413. EXPORT_SYMBOL(is_console_locked);
  2414. static void __console_unlock(void)
  2415. {
  2416. console_locked = 0;
  2417. up_console_sem();
  2418. }
  2419. #ifdef CONFIG_PRINTK
  2420. /*
  2421. * Prepend the message in @pmsg->pbufs->outbuf. This is achieved by shifting
  2422. * the existing message over and inserting the scratchbuf message.
  2423. *
  2424. * @pmsg is the original printk message.
  2425. * @fmt is the printf format of the message which will prepend the existing one.
  2426. *
  2427. * If there is not enough space in @pmsg->pbufs->outbuf, the existing
  2428. * message text will be sufficiently truncated.
  2429. *
  2430. * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated.
  2431. */
  2432. __printf(2, 3)
  2433. static void console_prepend_message(struct printk_message *pmsg, const char *fmt, ...)
  2434. {
  2435. struct printk_buffers *pbufs = pmsg->pbufs;
  2436. const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf);
  2437. const size_t outbuf_sz = sizeof(pbufs->outbuf);
  2438. char *scratchbuf = &pbufs->scratchbuf[0];
  2439. char *outbuf = &pbufs->outbuf[0];
  2440. va_list args;
  2441. size_t len;
  2442. va_start(args, fmt);
  2443. len = vscnprintf(scratchbuf, scratchbuf_sz, fmt, args);
  2444. va_end(args);
  2445. /*
  2446. * Make sure outbuf is sufficiently large before prepending.
  2447. * Keep at least the prefix when the message must be truncated.
  2448. * It is a rather theoretical problem when someone tries to
  2449. * use a minimalist buffer.
  2450. */
  2451. if (WARN_ON_ONCE(len + PRINTK_PREFIX_MAX >= outbuf_sz))
  2452. return;
  2453. if (pmsg->outbuf_len + len >= outbuf_sz) {
  2454. /* Truncate the message, but keep it terminated. */
  2455. pmsg->outbuf_len = outbuf_sz - (len + 1);
  2456. outbuf[pmsg->outbuf_len] = 0;
  2457. }
  2458. memmove(outbuf + len, outbuf, pmsg->outbuf_len + 1);
  2459. memcpy(outbuf, scratchbuf, len);
  2460. pmsg->outbuf_len += len;
  2461. }
  2462. /*
  2463. * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message".
  2464. * @pmsg->outbuf_len is updated appropriately.
  2465. *
  2466. * @pmsg is the printk message to prepend.
  2467. *
  2468. * @dropped is the dropped count to report in the dropped message.
  2469. */
  2470. void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
  2471. {
  2472. console_prepend_message(pmsg, "** %lu printk messages dropped **\n", dropped);
  2473. }
  2474. /*
  2475. * Prepend the message in @pmsg->pbufs->outbuf with a "replay message".
  2476. * @pmsg->outbuf_len is updated appropriately.
  2477. *
  2478. * @pmsg is the printk message to prepend.
  2479. */
  2480. void console_prepend_replay(struct printk_message *pmsg)
  2481. {
  2482. console_prepend_message(pmsg, "** replaying previous printk message **\n");
  2483. }
  2484. /*
  2485. * Read and format the specified record (or a later record if the specified
  2486. * record is not available).
  2487. *
  2488. * @pmsg will contain the formatted result. @pmsg->pbufs must point to a
  2489. * struct printk_buffers.
  2490. *
  2491. * @seq is the record to read and format. If it is not available, the next
  2492. * valid record is read.
  2493. *
  2494. * @is_extended specifies if the message should be formatted for extended
  2495. * console output.
  2496. *
  2497. * @may_supress specifies if records may be skipped based on loglevel.
  2498. *
  2499. * Returns false if no record is available. Otherwise true and all fields
  2500. * of @pmsg are valid. (See the documentation of struct printk_message
  2501. * for information about the @pmsg fields.)
  2502. */
  2503. bool printk_get_next_message(struct printk_message *pmsg, u64 seq,
  2504. bool is_extended, bool may_suppress)
  2505. {
  2506. struct printk_buffers *pbufs = pmsg->pbufs;
  2507. const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf);
  2508. const size_t outbuf_sz = sizeof(pbufs->outbuf);
  2509. char *scratchbuf = &pbufs->scratchbuf[0];
  2510. char *outbuf = &pbufs->outbuf[0];
  2511. struct printk_info info;
  2512. struct printk_record r;
  2513. size_t len = 0;
  2514. /*
  2515. * Formatting extended messages requires a separate buffer, so use the
  2516. * scratch buffer to read in the ringbuffer text.
  2517. *
  2518. * Formatting normal messages is done in-place, so read the ringbuffer
  2519. * text directly into the output buffer.
  2520. */
  2521. if (is_extended)
  2522. prb_rec_init_rd(&r, &info, scratchbuf, scratchbuf_sz);
  2523. else
  2524. prb_rec_init_rd(&r, &info, outbuf, outbuf_sz);
  2525. if (!prb_read_valid(prb, seq, &r))
  2526. return false;
  2527. pmsg->seq = r.info->seq;
  2528. pmsg->dropped = r.info->seq - seq;
  2529. /* Skip record that has level above the console loglevel. */
  2530. if (may_suppress && suppress_message_printing(r.info->level))
  2531. goto out;
  2532. if (is_extended) {
  2533. len = info_print_ext_header(outbuf, outbuf_sz, r.info);
  2534. len += msg_print_ext_body(outbuf + len, outbuf_sz - len,
  2535. &r.text_buf[0], r.info->text_len, &r.info->dev_info);
  2536. } else {
  2537. len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time);
  2538. }
  2539. out:
  2540. pmsg->outbuf_len = len;
  2541. return true;
  2542. }
  2543. /*
  2544. * Legacy console printing from printk() caller context does not respect
  2545. * raw_spinlock/spinlock nesting. For !PREEMPT_RT the lockdep warning is a
  2546. * false positive. For PREEMPT_RT the false positive condition does not
  2547. * occur.
  2548. *
  2549. * This map is used to temporarily establish LD_WAIT_SLEEP context for the
  2550. * console write() callback when legacy printing to avoid false positive
  2551. * lockdep complaints, thus allowing lockdep to continue to function for
  2552. * real issues.
  2553. */
  2554. #ifdef CONFIG_PREEMPT_RT
  2555. static inline void printk_legacy_allow_spinlock_enter(void) { }
  2556. static inline void printk_legacy_allow_spinlock_exit(void) { }
  2557. #else
  2558. static DEFINE_WAIT_OVERRIDE_MAP(printk_legacy_map, LD_WAIT_SLEEP);
  2559. static inline void printk_legacy_allow_spinlock_enter(void)
  2560. {
  2561. lock_map_acquire_try(&printk_legacy_map);
  2562. }
  2563. static inline void printk_legacy_allow_spinlock_exit(void)
  2564. {
  2565. lock_map_release(&printk_legacy_map);
  2566. }
  2567. #endif /* CONFIG_PREEMPT_RT */
  2568. /*
  2569. * Used as the printk buffers for non-panic, serialized console printing.
  2570. * This is for legacy (!CON_NBCON) as well as all boot (CON_BOOT) consoles.
  2571. * Its usage requires the console_lock held.
  2572. */
  2573. struct printk_buffers printk_shared_pbufs;
  2574. /*
  2575. * Print one record for the given console. The record printed is whatever
  2576. * record is the next available record for the given console.
  2577. *
  2578. * @handover will be set to true if a printk waiter has taken over the
  2579. * console_lock, in which case the caller is no longer holding both the
  2580. * console_lock and the SRCU read lock. Otherwise it is set to false.
  2581. *
  2582. * @cookie is the cookie from the SRCU read lock.
  2583. *
  2584. * Returns false if the given console has no next record to print, otherwise
  2585. * true.
  2586. *
  2587. * Requires the console_lock and the SRCU read lock.
  2588. */
  2589. static bool console_emit_next_record(struct console *con, bool *handover, int cookie)
  2590. {
  2591. bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED;
  2592. char *outbuf = &printk_shared_pbufs.outbuf[0];
  2593. struct printk_message pmsg = {
  2594. .pbufs = &printk_shared_pbufs,
  2595. };
  2596. unsigned long flags;
  2597. *handover = false;
  2598. if (!printk_get_next_message(&pmsg, con->seq, is_extended, true))
  2599. return false;
  2600. con->dropped += pmsg.dropped;
  2601. /* Skip messages of formatted length 0. */
  2602. if (pmsg.outbuf_len == 0) {
  2603. con->seq = pmsg.seq + 1;
  2604. goto skip;
  2605. }
  2606. if (con->dropped && !is_extended) {
  2607. console_prepend_dropped(&pmsg, con->dropped);
  2608. con->dropped = 0;
  2609. }
  2610. /* Write everything out to the hardware. */
  2611. if (force_legacy_kthread() && !panic_in_progress()) {
  2612. /*
  2613. * With forced threading this function is in a task context
  2614. * (either legacy kthread or get_init_console_seq()). There
  2615. * is no need for concern about printk reentrance, handovers,
  2616. * or lockdep complaints.
  2617. */
  2618. con->write(con, outbuf, pmsg.outbuf_len);
  2619. con->seq = pmsg.seq + 1;
  2620. } else {
  2621. /*
  2622. * While actively printing out messages, if another printk()
  2623. * were to occur on another CPU, it may wait for this one to
  2624. * finish. This task can not be preempted if there is a
  2625. * waiter waiting to take over.
  2626. *
  2627. * Interrupts are disabled because the hand over to a waiter
  2628. * must not be interrupted until the hand over is completed
  2629. * (@console_waiter is cleared).
  2630. */
  2631. printk_safe_enter_irqsave(flags);
  2632. console_lock_spinning_enable();
  2633. /* Do not trace print latency. */
  2634. stop_critical_timings();
  2635. printk_legacy_allow_spinlock_enter();
  2636. con->write(con, outbuf, pmsg.outbuf_len);
  2637. printk_legacy_allow_spinlock_exit();
  2638. start_critical_timings();
  2639. con->seq = pmsg.seq + 1;
  2640. *handover = console_lock_spinning_disable_and_check(cookie);
  2641. printk_safe_exit_irqrestore(flags);
  2642. }
  2643. skip:
  2644. return true;
  2645. }
  2646. #else
  2647. static bool console_emit_next_record(struct console *con, bool *handover, int cookie)
  2648. {
  2649. *handover = false;
  2650. return false;
  2651. }
  2652. static inline void printk_kthreads_check_locked(void) { }
  2653. #endif /* CONFIG_PRINTK */
  2654. /*
  2655. * Print out all remaining records to all consoles.
  2656. *
  2657. * @do_cond_resched is set by the caller. It can be true only in schedulable
  2658. * context.
  2659. *
  2660. * @next_seq is set to the sequence number after the last available record.
  2661. * The value is valid only when this function returns true. It means that all
  2662. * usable consoles are completely flushed.
  2663. *
  2664. * @handover will be set to true if a printk waiter has taken over the
  2665. * console_lock, in which case the caller is no longer holding the
  2666. * console_lock. Otherwise it is set to false.
  2667. *
  2668. * Returns true when there was at least one usable console and all messages
  2669. * were flushed to all usable consoles. A returned false informs the caller
  2670. * that everything was not flushed (either there were no usable consoles or
  2671. * another context has taken over printing or it is a panic situation and this
  2672. * is not the panic CPU). Regardless the reason, the caller should assume it
  2673. * is not useful to immediately try again.
  2674. *
  2675. * Requires the console_lock.
  2676. */
  2677. static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover)
  2678. {
  2679. struct console_flush_type ft;
  2680. bool any_usable = false;
  2681. struct console *con;
  2682. bool any_progress;
  2683. int cookie;
  2684. *next_seq = 0;
  2685. *handover = false;
  2686. do {
  2687. any_progress = false;
  2688. printk_get_console_flush_type(&ft);
  2689. cookie = console_srcu_read_lock();
  2690. for_each_console_srcu(con) {
  2691. short flags = console_srcu_read_flags(con);
  2692. u64 printk_seq;
  2693. bool progress;
  2694. /*
  2695. * console_flush_all() is only responsible for nbcon
  2696. * consoles when the nbcon consoles cannot print via
  2697. * their atomic or threaded flushing.
  2698. */
  2699. if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload))
  2700. continue;
  2701. if (!console_is_usable(con, flags, !do_cond_resched))
  2702. continue;
  2703. any_usable = true;
  2704. if (flags & CON_NBCON) {
  2705. progress = nbcon_legacy_emit_next_record(con, handover, cookie,
  2706. !do_cond_resched);
  2707. printk_seq = nbcon_seq_read(con);
  2708. } else {
  2709. progress = console_emit_next_record(con, handover, cookie);
  2710. printk_seq = con->seq;
  2711. }
  2712. /*
  2713. * If a handover has occurred, the SRCU read lock
  2714. * is already released.
  2715. */
  2716. if (*handover)
  2717. return false;
  2718. /* Track the next of the highest seq flushed. */
  2719. if (printk_seq > *next_seq)
  2720. *next_seq = printk_seq;
  2721. if (!progress)
  2722. continue;
  2723. any_progress = true;
  2724. /* Allow panic_cpu to take over the consoles safely. */
  2725. if (other_cpu_in_panic())
  2726. goto abandon;
  2727. if (do_cond_resched)
  2728. cond_resched();
  2729. }
  2730. console_srcu_read_unlock(cookie);
  2731. } while (any_progress);
  2732. return any_usable;
  2733. abandon:
  2734. console_srcu_read_unlock(cookie);
  2735. return false;
  2736. }
  2737. static void __console_flush_and_unlock(void)
  2738. {
  2739. bool do_cond_resched;
  2740. bool handover;
  2741. bool flushed;
  2742. u64 next_seq;
  2743. /*
  2744. * Console drivers are called with interrupts disabled, so
  2745. * @console_may_schedule should be cleared before; however, we may
  2746. * end up dumping a lot of lines, for example, if called from
  2747. * console registration path, and should invoke cond_resched()
  2748. * between lines if allowable. Not doing so can cause a very long
  2749. * scheduling stall on a slow console leading to RCU stall and
  2750. * softlockup warnings which exacerbate the issue with more
  2751. * messages practically incapacitating the system. Therefore, create
  2752. * a local to use for the printing loop.
  2753. */
  2754. do_cond_resched = console_may_schedule;
  2755. do {
  2756. console_may_schedule = 0;
  2757. flushed = console_flush_all(do_cond_resched, &next_seq, &handover);
  2758. if (!handover)
  2759. __console_unlock();
  2760. /*
  2761. * Abort if there was a failure to flush all messages to all
  2762. * usable consoles. Either it is not possible to flush (in
  2763. * which case it would be an infinite loop of retrying) or
  2764. * another context has taken over printing.
  2765. */
  2766. if (!flushed)
  2767. break;
  2768. /*
  2769. * Some context may have added new records after
  2770. * console_flush_all() but before unlocking the console.
  2771. * Re-check if there is a new record to flush. If the trylock
  2772. * fails, another context is already handling the printing.
  2773. */
  2774. } while (prb_read_valid(prb, next_seq, NULL) && console_trylock());
  2775. }
  2776. /**
  2777. * console_unlock - unblock the legacy console subsystem from printing
  2778. *
  2779. * Releases the console_lock which the caller holds to block printing of
  2780. * the legacy console subsystem.
  2781. *
  2782. * While the console_lock was held, console output may have been buffered
  2783. * by printk(). If this is the case, console_unlock() emits the output on
  2784. * legacy consoles prior to releasing the lock.
  2785. *
  2786. * console_unlock(); may be called from any context.
  2787. */
  2788. void console_unlock(void)
  2789. {
  2790. struct console_flush_type ft;
  2791. printk_get_console_flush_type(&ft);
  2792. if (ft.legacy_direct)
  2793. __console_flush_and_unlock();
  2794. else
  2795. __console_unlock();
  2796. }
  2797. EXPORT_SYMBOL(console_unlock);
  2798. /**
  2799. * console_conditional_schedule - yield the CPU if required
  2800. *
  2801. * If the console code is currently allowed to sleep, and
  2802. * if this CPU should yield the CPU to another task, do
  2803. * so here.
  2804. *
  2805. * Must be called within console_lock();.
  2806. */
  2807. void __sched console_conditional_schedule(void)
  2808. {
  2809. if (console_may_schedule)
  2810. cond_resched();
  2811. }
  2812. EXPORT_SYMBOL(console_conditional_schedule);
  2813. void console_unblank(void)
  2814. {
  2815. bool found_unblank = false;
  2816. struct console *c;
  2817. int cookie;
  2818. /*
  2819. * First check if there are any consoles implementing the unblank()
  2820. * callback. If not, there is no reason to continue and take the
  2821. * console lock, which in particular can be dangerous if
  2822. * @oops_in_progress is set.
  2823. */
  2824. cookie = console_srcu_read_lock();
  2825. for_each_console_srcu(c) {
  2826. if ((console_srcu_read_flags(c) & CON_ENABLED) && c->unblank) {
  2827. found_unblank = true;
  2828. break;
  2829. }
  2830. }
  2831. console_srcu_read_unlock(cookie);
  2832. if (!found_unblank)
  2833. return;
  2834. /*
  2835. * Stop console printing because the unblank() callback may
  2836. * assume the console is not within its write() callback.
  2837. *
  2838. * If @oops_in_progress is set, this may be an atomic context.
  2839. * In that case, attempt a trylock as best-effort.
  2840. */
  2841. if (oops_in_progress) {
  2842. /* Semaphores are not NMI-safe. */
  2843. if (in_nmi())
  2844. return;
  2845. /*
  2846. * Attempting to trylock the console lock can deadlock
  2847. * if another CPU was stopped while modifying the
  2848. * semaphore. "Hope and pray" that this is not the
  2849. * current situation.
  2850. */
  2851. if (down_trylock_console_sem() != 0)
  2852. return;
  2853. } else
  2854. console_lock();
  2855. console_locked = 1;
  2856. console_may_schedule = 0;
  2857. cookie = console_srcu_read_lock();
  2858. for_each_console_srcu(c) {
  2859. if ((console_srcu_read_flags(c) & CON_ENABLED) && c->unblank)
  2860. c->unblank();
  2861. }
  2862. console_srcu_read_unlock(cookie);
  2863. console_unlock();
  2864. if (!oops_in_progress)
  2865. pr_flush(1000, true);
  2866. }
  2867. /*
  2868. * Rewind all consoles to the oldest available record.
  2869. *
  2870. * IMPORTANT: The function is safe only when called under
  2871. * console_lock(). It is not enforced because
  2872. * it is used as a best effort in panic().
  2873. */
  2874. static void __console_rewind_all(void)
  2875. {
  2876. struct console *c;
  2877. short flags;
  2878. int cookie;
  2879. u64 seq;
  2880. seq = prb_first_valid_seq(prb);
  2881. cookie = console_srcu_read_lock();
  2882. for_each_console_srcu(c) {
  2883. flags = console_srcu_read_flags(c);
  2884. if (flags & CON_NBCON) {
  2885. nbcon_seq_force(c, seq);
  2886. } else {
  2887. /*
  2888. * This assignment is safe only when called under
  2889. * console_lock(). On panic, legacy consoles are
  2890. * only best effort.
  2891. */
  2892. c->seq = seq;
  2893. }
  2894. }
  2895. console_srcu_read_unlock(cookie);
  2896. }
  2897. /**
  2898. * console_flush_on_panic - flush console content on panic
  2899. * @mode: flush all messages in buffer or just the pending ones
  2900. *
  2901. * Immediately output all pending messages no matter what.
  2902. */
  2903. void console_flush_on_panic(enum con_flush_mode mode)
  2904. {
  2905. struct console_flush_type ft;
  2906. bool handover;
  2907. u64 next_seq;
  2908. /*
  2909. * Ignore the console lock and flush out the messages. Attempting a
  2910. * trylock would not be useful because:
  2911. *
  2912. * - if it is contended, it must be ignored anyway
  2913. * - console_lock() and console_trylock() block and fail
  2914. * respectively in panic for non-panic CPUs
  2915. * - semaphores are not NMI-safe
  2916. */
  2917. /*
  2918. * If another context is holding the console lock,
  2919. * @console_may_schedule might be set. Clear it so that
  2920. * this context does not call cond_resched() while flushing.
  2921. */
  2922. console_may_schedule = 0;
  2923. if (mode == CONSOLE_REPLAY_ALL)
  2924. __console_rewind_all();
  2925. printk_get_console_flush_type(&ft);
  2926. if (ft.nbcon_atomic)
  2927. nbcon_atomic_flush_pending();
  2928. /* Flush legacy consoles once allowed, even when dangerous. */
  2929. if (legacy_allow_panic_sync)
  2930. console_flush_all(false, &next_seq, &handover);
  2931. }
  2932. /*
  2933. * Return the console tty driver structure and its associated index
  2934. */
  2935. struct tty_driver *console_device(int *index)
  2936. {
  2937. struct console *c;
  2938. struct tty_driver *driver = NULL;
  2939. int cookie;
  2940. /*
  2941. * Take console_lock to serialize device() callback with
  2942. * other console operations. For example, fg_console is
  2943. * modified under console_lock when switching vt.
  2944. */
  2945. console_lock();
  2946. cookie = console_srcu_read_lock();
  2947. for_each_console_srcu(c) {
  2948. if (!c->device)
  2949. continue;
  2950. driver = c->device(c, index);
  2951. if (driver)
  2952. break;
  2953. }
  2954. console_srcu_read_unlock(cookie);
  2955. console_unlock();
  2956. return driver;
  2957. }
  2958. /*
  2959. * Prevent further output on the passed console device so that (for example)
  2960. * serial drivers can disable console output before suspending a port, and can
  2961. * re-enable output afterwards.
  2962. */
  2963. void console_stop(struct console *console)
  2964. {
  2965. __pr_flush(console, 1000, true);
  2966. console_list_lock();
  2967. console_srcu_write_flags(console, console->flags & ~CON_ENABLED);
  2968. console_list_unlock();
  2969. /*
  2970. * Ensure that all SRCU list walks have completed. All contexts must
  2971. * be able to see that this console is disabled so that (for example)
  2972. * the caller can suspend the port without risk of another context
  2973. * using the port.
  2974. */
  2975. synchronize_srcu(&console_srcu);
  2976. }
  2977. EXPORT_SYMBOL(console_stop);
  2978. void console_start(struct console *console)
  2979. {
  2980. struct console_flush_type ft;
  2981. bool is_nbcon;
  2982. console_list_lock();
  2983. console_srcu_write_flags(console, console->flags | CON_ENABLED);
  2984. is_nbcon = console->flags & CON_NBCON;
  2985. console_list_unlock();
  2986. /*
  2987. * Ensure that all SRCU list walks have completed. The related
  2988. * printing context must be able to see it is enabled so that
  2989. * it is guaranteed to wake up and resume printing.
  2990. */
  2991. synchronize_srcu(&console_srcu);
  2992. printk_get_console_flush_type(&ft);
  2993. if (is_nbcon && ft.nbcon_offload)
  2994. nbcon_kthread_wake(console);
  2995. else if (ft.legacy_offload)
  2996. defer_console_output();
  2997. __pr_flush(console, 1000, true);
  2998. }
  2999. EXPORT_SYMBOL(console_start);
  3000. #ifdef CONFIG_PRINTK
  3001. static int unregister_console_locked(struct console *console);
  3002. /* True when system boot is far enough to create printer threads. */
  3003. static bool printk_kthreads_ready __ro_after_init;
  3004. static struct task_struct *printk_legacy_kthread;
  3005. static bool legacy_kthread_should_wakeup(void)
  3006. {
  3007. struct console_flush_type ft;
  3008. struct console *con;
  3009. bool ret = false;
  3010. int cookie;
  3011. if (kthread_should_stop())
  3012. return true;
  3013. printk_get_console_flush_type(&ft);
  3014. cookie = console_srcu_read_lock();
  3015. for_each_console_srcu(con) {
  3016. short flags = console_srcu_read_flags(con);
  3017. u64 printk_seq;
  3018. /*
  3019. * The legacy printer thread is only responsible for nbcon
  3020. * consoles when the nbcon consoles cannot print via their
  3021. * atomic or threaded flushing.
  3022. */
  3023. if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload))
  3024. continue;
  3025. if (!console_is_usable(con, flags, false))
  3026. continue;
  3027. if (flags & CON_NBCON) {
  3028. printk_seq = nbcon_seq_read(con);
  3029. } else {
  3030. /*
  3031. * It is safe to read @seq because only this
  3032. * thread context updates @seq.
  3033. */
  3034. printk_seq = con->seq;
  3035. }
  3036. if (prb_read_valid(prb, printk_seq, NULL)) {
  3037. ret = true;
  3038. break;
  3039. }
  3040. }
  3041. console_srcu_read_unlock(cookie);
  3042. return ret;
  3043. }
  3044. static int legacy_kthread_func(void *unused)
  3045. {
  3046. for (;;) {
  3047. wait_event_interruptible(legacy_wait, legacy_kthread_should_wakeup());
  3048. if (kthread_should_stop())
  3049. break;
  3050. console_lock();
  3051. __console_flush_and_unlock();
  3052. }
  3053. return 0;
  3054. }
  3055. static bool legacy_kthread_create(void)
  3056. {
  3057. struct task_struct *kt;
  3058. lockdep_assert_console_list_lock_held();
  3059. kt = kthread_run(legacy_kthread_func, NULL, "pr/legacy");
  3060. if (WARN_ON(IS_ERR(kt))) {
  3061. pr_err("failed to start legacy printing thread\n");
  3062. return false;
  3063. }
  3064. printk_legacy_kthread = kt;
  3065. /*
  3066. * It is important that console printing threads are scheduled
  3067. * shortly after a printk call and with generous runtime budgets.
  3068. */
  3069. sched_set_normal(printk_legacy_kthread, -20);
  3070. return true;
  3071. }
  3072. /**
  3073. * printk_kthreads_shutdown - shutdown all threaded printers
  3074. *
  3075. * On system shutdown all threaded printers are stopped. This allows printk
  3076. * to transition back to atomic printing, thus providing a robust mechanism
  3077. * for the final shutdown/reboot messages to be output.
  3078. */
  3079. static void printk_kthreads_shutdown(void)
  3080. {
  3081. struct console *con;
  3082. console_list_lock();
  3083. if (printk_kthreads_running) {
  3084. printk_kthreads_running = false;
  3085. for_each_console(con) {
  3086. if (con->flags & CON_NBCON)
  3087. nbcon_kthread_stop(con);
  3088. }
  3089. /*
  3090. * The threads may have been stopped while printing a
  3091. * backlog. Flush any records left over.
  3092. */
  3093. nbcon_atomic_flush_pending();
  3094. }
  3095. console_list_unlock();
  3096. }
  3097. static struct syscore_ops printk_syscore_ops = {
  3098. .shutdown = printk_kthreads_shutdown,
  3099. };
  3100. /*
  3101. * If appropriate, start nbcon kthreads and set @printk_kthreads_running.
  3102. * If any kthreads fail to start, those consoles are unregistered.
  3103. *
  3104. * Must be called under console_list_lock().
  3105. */
  3106. static void printk_kthreads_check_locked(void)
  3107. {
  3108. struct hlist_node *tmp;
  3109. struct console *con;
  3110. lockdep_assert_console_list_lock_held();
  3111. if (!printk_kthreads_ready)
  3112. return;
  3113. if (have_legacy_console || have_boot_console) {
  3114. if (!printk_legacy_kthread &&
  3115. force_legacy_kthread() &&
  3116. !legacy_kthread_create()) {
  3117. /*
  3118. * All legacy consoles must be unregistered. If there
  3119. * are any nbcon consoles, they will set up their own
  3120. * kthread.
  3121. */
  3122. hlist_for_each_entry_safe(con, tmp, &console_list, node) {
  3123. if (con->flags & CON_NBCON)
  3124. continue;
  3125. unregister_console_locked(con);
  3126. }
  3127. }
  3128. } else if (printk_legacy_kthread) {
  3129. kthread_stop(printk_legacy_kthread);
  3130. printk_legacy_kthread = NULL;
  3131. }
  3132. /*
  3133. * Printer threads cannot be started as long as any boot console is
  3134. * registered because there is no way to synchronize the hardware
  3135. * registers between boot console code and regular console code.
  3136. * It can only be known that there will be no new boot consoles when
  3137. * an nbcon console is registered.
  3138. */
  3139. if (have_boot_console || !have_nbcon_console) {
  3140. /* Clear flag in case all nbcon consoles unregistered. */
  3141. printk_kthreads_running = false;
  3142. return;
  3143. }
  3144. if (printk_kthreads_running)
  3145. return;
  3146. hlist_for_each_entry_safe(con, tmp, &console_list, node) {
  3147. if (!(con->flags & CON_NBCON))
  3148. continue;
  3149. if (!nbcon_kthread_create(con))
  3150. unregister_console_locked(con);
  3151. }
  3152. printk_kthreads_running = true;
  3153. }
  3154. static int __init printk_set_kthreads_ready(void)
  3155. {
  3156. register_syscore_ops(&printk_syscore_ops);
  3157. console_list_lock();
  3158. printk_kthreads_ready = true;
  3159. printk_kthreads_check_locked();
  3160. console_list_unlock();
  3161. return 0;
  3162. }
  3163. early_initcall(printk_set_kthreads_ready);
  3164. #endif /* CONFIG_PRINTK */
  3165. static int __read_mostly keep_bootcon;
  3166. static int __init keep_bootcon_setup(char *str)
  3167. {
  3168. keep_bootcon = 1;
  3169. pr_info("debug: skip boot console de-registration.\n");
  3170. return 0;
  3171. }
  3172. early_param("keep_bootcon", keep_bootcon_setup);
  3173. static int console_call_setup(struct console *newcon, char *options)
  3174. {
  3175. int err;
  3176. if (!newcon->setup)
  3177. return 0;
  3178. /* Synchronize with possible boot console. */
  3179. console_lock();
  3180. err = newcon->setup(newcon, options);
  3181. console_unlock();
  3182. return err;
  3183. }
  3184. /*
  3185. * This is called by register_console() to try to match
  3186. * the newly registered console with any of the ones selected
  3187. * by either the command line or add_preferred_console() and
  3188. * setup/enable it.
  3189. *
  3190. * Care need to be taken with consoles that are statically
  3191. * enabled such as netconsole
  3192. */
  3193. static int try_enable_preferred_console(struct console *newcon,
  3194. bool user_specified)
  3195. {
  3196. struct console_cmdline *c;
  3197. int i, err;
  3198. for (i = 0, c = console_cmdline;
  3199. i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]);
  3200. i++, c++) {
  3201. /* Console not yet initialized? */
  3202. if (!c->name[0])
  3203. continue;
  3204. if (c->user_specified != user_specified)
  3205. continue;
  3206. if (!newcon->match ||
  3207. newcon->match(newcon, c->name, c->index, c->options) != 0) {
  3208. /* default matching */
  3209. BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
  3210. if (strcmp(c->name, newcon->name) != 0)
  3211. continue;
  3212. if (newcon->index >= 0 &&
  3213. newcon->index != c->index)
  3214. continue;
  3215. if (newcon->index < 0)
  3216. newcon->index = c->index;
  3217. if (_braille_register_console(newcon, c))
  3218. return 0;
  3219. err = console_call_setup(newcon, c->options);
  3220. if (err)
  3221. return err;
  3222. }
  3223. newcon->flags |= CON_ENABLED;
  3224. if (i == preferred_console)
  3225. newcon->flags |= CON_CONSDEV;
  3226. return 0;
  3227. }
  3228. /*
  3229. * Some consoles, such as pstore and netconsole, can be enabled even
  3230. * without matching. Accept the pre-enabled consoles only when match()
  3231. * and setup() had a chance to be called.
  3232. */
  3233. if (newcon->flags & CON_ENABLED && c->user_specified == user_specified)
  3234. return 0;
  3235. return -ENOENT;
  3236. }
  3237. /* Try to enable the console unconditionally */
  3238. static void try_enable_default_console(struct console *newcon)
  3239. {
  3240. if (newcon->index < 0)
  3241. newcon->index = 0;
  3242. if (console_call_setup(newcon, NULL) != 0)
  3243. return;
  3244. newcon->flags |= CON_ENABLED;
  3245. if (newcon->device)
  3246. newcon->flags |= CON_CONSDEV;
  3247. }
  3248. /* Return the starting sequence number for a newly registered console. */
  3249. static u64 get_init_console_seq(struct console *newcon, bool bootcon_registered)
  3250. {
  3251. struct console *con;
  3252. bool handover;
  3253. u64 init_seq;
  3254. if (newcon->flags & (CON_PRINTBUFFER | CON_BOOT)) {
  3255. /* Get a consistent copy of @syslog_seq. */
  3256. mutex_lock(&syslog_lock);
  3257. init_seq = syslog_seq;
  3258. mutex_unlock(&syslog_lock);
  3259. } else {
  3260. /* Begin with next message added to ringbuffer. */
  3261. init_seq = prb_next_seq(prb);
  3262. /*
  3263. * If any enabled boot consoles are due to be unregistered
  3264. * shortly, some may not be caught up and may be the same
  3265. * device as @newcon. Since it is not known which boot console
  3266. * is the same device, flush all consoles and, if necessary,
  3267. * start with the message of the enabled boot console that is
  3268. * the furthest behind.
  3269. */
  3270. if (bootcon_registered && !keep_bootcon) {
  3271. /*
  3272. * Hold the console_lock to stop console printing and
  3273. * guarantee safe access to console->seq.
  3274. */
  3275. console_lock();
  3276. /*
  3277. * Flush all consoles and set the console to start at
  3278. * the next unprinted sequence number.
  3279. */
  3280. if (!console_flush_all(true, &init_seq, &handover)) {
  3281. /*
  3282. * Flushing failed. Just choose the lowest
  3283. * sequence of the enabled boot consoles.
  3284. */
  3285. /*
  3286. * If there was a handover, this context no
  3287. * longer holds the console_lock.
  3288. */
  3289. if (handover)
  3290. console_lock();
  3291. init_seq = prb_next_seq(prb);
  3292. for_each_console(con) {
  3293. u64 seq;
  3294. if (!(con->flags & CON_BOOT) ||
  3295. !(con->flags & CON_ENABLED)) {
  3296. continue;
  3297. }
  3298. if (con->flags & CON_NBCON)
  3299. seq = nbcon_seq_read(con);
  3300. else
  3301. seq = con->seq;
  3302. if (seq < init_seq)
  3303. init_seq = seq;
  3304. }
  3305. }
  3306. console_unlock();
  3307. }
  3308. }
  3309. return init_seq;
  3310. }
  3311. #define console_first() \
  3312. hlist_entry(console_list.first, struct console, node)
  3313. static int unregister_console_locked(struct console *console);
  3314. /*
  3315. * The console driver calls this routine during kernel initialization
  3316. * to register the console printing procedure with printk() and to
  3317. * print any messages that were printed by the kernel before the
  3318. * console driver was initialized.
  3319. *
  3320. * This can happen pretty early during the boot process (because of
  3321. * early_printk) - sometimes before setup_arch() completes - be careful
  3322. * of what kernel features are used - they may not be initialised yet.
  3323. *
  3324. * There are two types of consoles - bootconsoles (early_printk) and
  3325. * "real" consoles (everything which is not a bootconsole) which are
  3326. * handled differently.
  3327. * - Any number of bootconsoles can be registered at any time.
  3328. * - As soon as a "real" console is registered, all bootconsoles
  3329. * will be unregistered automatically.
  3330. * - Once a "real" console is registered, any attempt to register a
  3331. * bootconsoles will be rejected
  3332. */
  3333. void register_console(struct console *newcon)
  3334. {
  3335. bool use_device_lock = (newcon->flags & CON_NBCON) && newcon->write_atomic;
  3336. bool bootcon_registered = false;
  3337. bool realcon_registered = false;
  3338. struct console *con;
  3339. unsigned long flags;
  3340. u64 init_seq;
  3341. int err;
  3342. console_list_lock();
  3343. for_each_console(con) {
  3344. if (WARN(con == newcon, "console '%s%d' already registered\n",
  3345. con->name, con->index)) {
  3346. goto unlock;
  3347. }
  3348. if (con->flags & CON_BOOT)
  3349. bootcon_registered = true;
  3350. else
  3351. realcon_registered = true;
  3352. }
  3353. /* Do not register boot consoles when there already is a real one. */
  3354. if ((newcon->flags & CON_BOOT) && realcon_registered) {
  3355. pr_info("Too late to register bootconsole %s%d\n",
  3356. newcon->name, newcon->index);
  3357. goto unlock;
  3358. }
  3359. if (newcon->flags & CON_NBCON) {
  3360. /*
  3361. * Ensure the nbcon console buffers can be allocated
  3362. * before modifying any global data.
  3363. */
  3364. if (!nbcon_alloc(newcon))
  3365. goto unlock;
  3366. }
  3367. /*
  3368. * See if we want to enable this console driver by default.
  3369. *
  3370. * Nope when a console is preferred by the command line, device
  3371. * tree, or SPCR.
  3372. *
  3373. * The first real console with tty binding (driver) wins. More
  3374. * consoles might get enabled before the right one is found.
  3375. *
  3376. * Note that a console with tty binding will have CON_CONSDEV
  3377. * flag set and will be first in the list.
  3378. */
  3379. if (preferred_console < 0) {
  3380. if (hlist_empty(&console_list) || !console_first()->device ||
  3381. console_first()->flags & CON_BOOT) {
  3382. try_enable_default_console(newcon);
  3383. }
  3384. }
  3385. /* See if this console matches one we selected on the command line */
  3386. err = try_enable_preferred_console(newcon, true);
  3387. /* If not, try to match against the platform default(s) */
  3388. if (err == -ENOENT)
  3389. err = try_enable_preferred_console(newcon, false);
  3390. /* printk() messages are not printed to the Braille console. */
  3391. if (err || newcon->flags & CON_BRL) {
  3392. if (newcon->flags & CON_NBCON)
  3393. nbcon_free(newcon);
  3394. goto unlock;
  3395. }
  3396. /*
  3397. * If we have a bootconsole, and are switching to a real console,
  3398. * don't print everything out again, since when the boot console, and
  3399. * the real console are the same physical device, it's annoying to
  3400. * see the beginning boot messages twice
  3401. */
  3402. if (bootcon_registered &&
  3403. ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
  3404. newcon->flags &= ~CON_PRINTBUFFER;
  3405. }
  3406. newcon->dropped = 0;
  3407. init_seq = get_init_console_seq(newcon, bootcon_registered);
  3408. if (newcon->flags & CON_NBCON) {
  3409. have_nbcon_console = true;
  3410. nbcon_seq_force(newcon, init_seq);
  3411. } else {
  3412. have_legacy_console = true;
  3413. newcon->seq = init_seq;
  3414. }
  3415. if (newcon->flags & CON_BOOT)
  3416. have_boot_console = true;
  3417. /*
  3418. * If another context is actively using the hardware of this new
  3419. * console, it will not be aware of the nbcon synchronization. This
  3420. * is a risk that two contexts could access the hardware
  3421. * simultaneously if this new console is used for atomic printing
  3422. * and the other context is still using the hardware.
  3423. *
  3424. * Use the driver synchronization to ensure that the hardware is not
  3425. * in use while this new console transitions to being registered.
  3426. */
  3427. if (use_device_lock)
  3428. newcon->device_lock(newcon, &flags);
  3429. /*
  3430. * Put this console in the list - keep the
  3431. * preferred driver at the head of the list.
  3432. */
  3433. if (hlist_empty(&console_list)) {
  3434. /* Ensure CON_CONSDEV is always set for the head. */
  3435. newcon->flags |= CON_CONSDEV;
  3436. hlist_add_head_rcu(&newcon->node, &console_list);
  3437. } else if (newcon->flags & CON_CONSDEV) {
  3438. /* Only the new head can have CON_CONSDEV set. */
  3439. console_srcu_write_flags(console_first(), console_first()->flags & ~CON_CONSDEV);
  3440. hlist_add_head_rcu(&newcon->node, &console_list);
  3441. } else {
  3442. hlist_add_behind_rcu(&newcon->node, console_list.first);
  3443. }
  3444. /*
  3445. * No need to synchronize SRCU here! The caller does not rely
  3446. * on all contexts being able to see the new console before
  3447. * register_console() completes.
  3448. */
  3449. /* This new console is now registered. */
  3450. if (use_device_lock)
  3451. newcon->device_unlock(newcon, flags);
  3452. console_sysfs_notify();
  3453. /*
  3454. * By unregistering the bootconsoles after we enable the real console
  3455. * we get the "console xxx enabled" message on all the consoles -
  3456. * boot consoles, real consoles, etc - this is to ensure that end
  3457. * users know there might be something in the kernel's log buffer that
  3458. * went to the bootconsole (that they do not see on the real console)
  3459. */
  3460. con_printk(KERN_INFO, newcon, "enabled\n");
  3461. if (bootcon_registered &&
  3462. ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
  3463. !keep_bootcon) {
  3464. struct hlist_node *tmp;
  3465. hlist_for_each_entry_safe(con, tmp, &console_list, node) {
  3466. if (con->flags & CON_BOOT)
  3467. unregister_console_locked(con);
  3468. }
  3469. }
  3470. /* Changed console list, may require printer threads to start/stop. */
  3471. printk_kthreads_check_locked();
  3472. unlock:
  3473. console_list_unlock();
  3474. }
  3475. EXPORT_SYMBOL(register_console);
  3476. /* Must be called under console_list_lock(). */
  3477. static int unregister_console_locked(struct console *console)
  3478. {
  3479. bool use_device_lock = (console->flags & CON_NBCON) && console->write_atomic;
  3480. bool found_legacy_con = false;
  3481. bool found_nbcon_con = false;
  3482. bool found_boot_con = false;
  3483. unsigned long flags;
  3484. struct console *c;
  3485. int res;
  3486. lockdep_assert_console_list_lock_held();
  3487. con_printk(KERN_INFO, console, "disabled\n");
  3488. res = _braille_unregister_console(console);
  3489. if (res < 0)
  3490. return res;
  3491. if (res > 0)
  3492. return 0;
  3493. if (!console_is_registered_locked(console))
  3494. res = -ENODEV;
  3495. else if (console_is_usable(console, console->flags, true))
  3496. __pr_flush(console, 1000, true);
  3497. /* Disable it unconditionally */
  3498. console_srcu_write_flags(console, console->flags & ~CON_ENABLED);
  3499. if (res < 0)
  3500. return res;
  3501. /*
  3502. * Use the driver synchronization to ensure that the hardware is not
  3503. * in use while this console transitions to being unregistered.
  3504. */
  3505. if (use_device_lock)
  3506. console->device_lock(console, &flags);
  3507. hlist_del_init_rcu(&console->node);
  3508. if (use_device_lock)
  3509. console->device_unlock(console, flags);
  3510. /*
  3511. * <HISTORICAL>
  3512. * If this isn't the last console and it has CON_CONSDEV set, we
  3513. * need to set it on the next preferred console.
  3514. * </HISTORICAL>
  3515. *
  3516. * The above makes no sense as there is no guarantee that the next
  3517. * console has any device attached. Oh well....
  3518. */
  3519. if (!hlist_empty(&console_list) && console->flags & CON_CONSDEV)
  3520. console_srcu_write_flags(console_first(), console_first()->flags | CON_CONSDEV);
  3521. /*
  3522. * Ensure that all SRCU list walks have completed. All contexts
  3523. * must not be able to see this console in the list so that any
  3524. * exit/cleanup routines can be performed safely.
  3525. */
  3526. synchronize_srcu(&console_srcu);
  3527. if (console->flags & CON_NBCON)
  3528. nbcon_free(console);
  3529. console_sysfs_notify();
  3530. if (console->exit)
  3531. res = console->exit(console);
  3532. /*
  3533. * With this console gone, the global flags tracking registered
  3534. * console types may have changed. Update them.
  3535. */
  3536. for_each_console(c) {
  3537. if (c->flags & CON_BOOT)
  3538. found_boot_con = true;
  3539. if (c->flags & CON_NBCON)
  3540. found_nbcon_con = true;
  3541. else
  3542. found_legacy_con = true;
  3543. }
  3544. if (!found_boot_con)
  3545. have_boot_console = found_boot_con;
  3546. if (!found_legacy_con)
  3547. have_legacy_console = found_legacy_con;
  3548. if (!found_nbcon_con)
  3549. have_nbcon_console = found_nbcon_con;
  3550. /* Changed console list, may require printer threads to start/stop. */
  3551. printk_kthreads_check_locked();
  3552. return res;
  3553. }
  3554. int unregister_console(struct console *console)
  3555. {
  3556. int res;
  3557. console_list_lock();
  3558. res = unregister_console_locked(console);
  3559. console_list_unlock();
  3560. return res;
  3561. }
  3562. EXPORT_SYMBOL(unregister_console);
  3563. /**
  3564. * console_force_preferred_locked - force a registered console preferred
  3565. * @con: The registered console to force preferred.
  3566. *
  3567. * Must be called under console_list_lock().
  3568. */
  3569. void console_force_preferred_locked(struct console *con)
  3570. {
  3571. struct console *cur_pref_con;
  3572. if (!console_is_registered_locked(con))
  3573. return;
  3574. cur_pref_con = console_first();
  3575. /* Already preferred? */
  3576. if (cur_pref_con == con)
  3577. return;
  3578. /*
  3579. * Delete, but do not re-initialize the entry. This allows the console
  3580. * to continue to appear registered (via any hlist_unhashed_lockless()
  3581. * checks), even though it was briefly removed from the console list.
  3582. */
  3583. hlist_del_rcu(&con->node);
  3584. /*
  3585. * Ensure that all SRCU list walks have completed so that the console
  3586. * can be added to the beginning of the console list and its forward
  3587. * list pointer can be re-initialized.
  3588. */
  3589. synchronize_srcu(&console_srcu);
  3590. con->flags |= CON_CONSDEV;
  3591. WARN_ON(!con->device);
  3592. /* Only the new head can have CON_CONSDEV set. */
  3593. console_srcu_write_flags(cur_pref_con, cur_pref_con->flags & ~CON_CONSDEV);
  3594. hlist_add_head_rcu(&con->node, &console_list);
  3595. }
  3596. EXPORT_SYMBOL(console_force_preferred_locked);
  3597. /*
  3598. * Initialize the console device. This is called *early*, so
  3599. * we can't necessarily depend on lots of kernel help here.
  3600. * Just do some early initializations, and do the complex setup
  3601. * later.
  3602. */
  3603. void __init console_init(void)
  3604. {
  3605. int ret;
  3606. initcall_t call;
  3607. initcall_entry_t *ce;
  3608. /* Setup the default TTY line discipline. */
  3609. n_tty_init();
  3610. /*
  3611. * set up the console device so that later boot sequences can
  3612. * inform about problems etc..
  3613. */
  3614. ce = __con_initcall_start;
  3615. trace_initcall_level("console");
  3616. while (ce < __con_initcall_end) {
  3617. call = initcall_from_entry(ce);
  3618. trace_initcall_start(call);
  3619. ret = call();
  3620. trace_initcall_finish(call, ret);
  3621. ce++;
  3622. }
  3623. }
  3624. /*
  3625. * Some boot consoles access data that is in the init section and which will
  3626. * be discarded after the initcalls have been run. To make sure that no code
  3627. * will access this data, unregister the boot consoles in a late initcall.
  3628. *
  3629. * If for some reason, such as deferred probe or the driver being a loadable
  3630. * module, the real console hasn't registered yet at this point, there will
  3631. * be a brief interval in which no messages are logged to the console, which
  3632. * makes it difficult to diagnose problems that occur during this time.
  3633. *
  3634. * To mitigate this problem somewhat, only unregister consoles whose memory
  3635. * intersects with the init section. Note that all other boot consoles will
  3636. * get unregistered when the real preferred console is registered.
  3637. */
  3638. static int __init printk_late_init(void)
  3639. {
  3640. struct hlist_node *tmp;
  3641. struct console *con;
  3642. int ret;
  3643. console_list_lock();
  3644. hlist_for_each_entry_safe(con, tmp, &console_list, node) {
  3645. if (!(con->flags & CON_BOOT))
  3646. continue;
  3647. /* Check addresses that might be used for enabled consoles. */
  3648. if (init_section_intersects(con, sizeof(*con)) ||
  3649. init_section_contains(con->write, 0) ||
  3650. init_section_contains(con->read, 0) ||
  3651. init_section_contains(con->device, 0) ||
  3652. init_section_contains(con->unblank, 0) ||
  3653. init_section_contains(con->data, 0)) {
  3654. /*
  3655. * Please, consider moving the reported consoles out
  3656. * of the init section.
  3657. */
  3658. pr_warn("bootconsole [%s%d] uses init memory and must be disabled even before the real one is ready\n",
  3659. con->name, con->index);
  3660. unregister_console_locked(con);
  3661. }
  3662. }
  3663. console_list_unlock();
  3664. ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL,
  3665. console_cpu_notify);
  3666. WARN_ON(ret < 0);
  3667. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "printk:online",
  3668. console_cpu_notify, NULL);
  3669. WARN_ON(ret < 0);
  3670. printk_sysctl_init();
  3671. return 0;
  3672. }
  3673. late_initcall(printk_late_init);
  3674. #if defined CONFIG_PRINTK
  3675. /* If @con is specified, only wait for that console. Otherwise wait for all. */
  3676. static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress)
  3677. {
  3678. unsigned long timeout_jiffies = msecs_to_jiffies(timeout_ms);
  3679. unsigned long remaining_jiffies = timeout_jiffies;
  3680. struct console_flush_type ft;
  3681. struct console *c;
  3682. u64 last_diff = 0;
  3683. u64 printk_seq;
  3684. short flags;
  3685. int cookie;
  3686. u64 diff;
  3687. u64 seq;
  3688. /* Sorry, pr_flush() will not work this early. */
  3689. if (system_state < SYSTEM_SCHEDULING)
  3690. return false;
  3691. might_sleep();
  3692. seq = prb_next_reserve_seq(prb);
  3693. /* Flush the consoles so that records up to @seq are printed. */
  3694. printk_get_console_flush_type(&ft);
  3695. if (ft.nbcon_atomic)
  3696. nbcon_atomic_flush_pending();
  3697. if (ft.legacy_direct) {
  3698. console_lock();
  3699. console_unlock();
  3700. }
  3701. for (;;) {
  3702. unsigned long begin_jiffies;
  3703. unsigned long slept_jiffies;
  3704. diff = 0;
  3705. /*
  3706. * Hold the console_lock to guarantee safe access to
  3707. * console->seq. Releasing console_lock flushes more
  3708. * records in case @seq is still not printed on all
  3709. * usable consoles.
  3710. *
  3711. * Holding the console_lock is not necessary if there
  3712. * are no legacy or boot consoles. However, such a
  3713. * console could register at any time. Always hold the
  3714. * console_lock as a precaution rather than
  3715. * synchronizing against register_console().
  3716. */
  3717. console_lock();
  3718. cookie = console_srcu_read_lock();
  3719. for_each_console_srcu(c) {
  3720. if (con && con != c)
  3721. continue;
  3722. flags = console_srcu_read_flags(c);
  3723. /*
  3724. * If consoles are not usable, it cannot be expected
  3725. * that they make forward progress, so only increment
  3726. * @diff for usable consoles.
  3727. */
  3728. if (!console_is_usable(c, flags, true) &&
  3729. !console_is_usable(c, flags, false)) {
  3730. continue;
  3731. }
  3732. if (flags & CON_NBCON) {
  3733. printk_seq = nbcon_seq_read(c);
  3734. } else {
  3735. printk_seq = c->seq;
  3736. }
  3737. if (printk_seq < seq)
  3738. diff += seq - printk_seq;
  3739. }
  3740. console_srcu_read_unlock(cookie);
  3741. if (diff != last_diff && reset_on_progress)
  3742. remaining_jiffies = timeout_jiffies;
  3743. console_unlock();
  3744. /* Note: @diff is 0 if there are no usable consoles. */
  3745. if (diff == 0 || remaining_jiffies == 0)
  3746. break;
  3747. /* msleep(1) might sleep much longer. Check time by jiffies. */
  3748. begin_jiffies = jiffies;
  3749. msleep(1);
  3750. slept_jiffies = jiffies - begin_jiffies;
  3751. remaining_jiffies -= min(slept_jiffies, remaining_jiffies);
  3752. last_diff = diff;
  3753. }
  3754. return (diff == 0);
  3755. }
  3756. /**
  3757. * pr_flush() - Wait for printing threads to catch up.
  3758. *
  3759. * @timeout_ms: The maximum time (in ms) to wait.
  3760. * @reset_on_progress: Reset the timeout if forward progress is seen.
  3761. *
  3762. * A value of 0 for @timeout_ms means no waiting will occur. A value of -1
  3763. * represents infinite waiting.
  3764. *
  3765. * If @reset_on_progress is true, the timeout will be reset whenever any
  3766. * printer has been seen to make some forward progress.
  3767. *
  3768. * Context: Process context. May sleep while acquiring console lock.
  3769. * Return: true if all usable printers are caught up.
  3770. */
  3771. static bool pr_flush(int timeout_ms, bool reset_on_progress)
  3772. {
  3773. return __pr_flush(NULL, timeout_ms, reset_on_progress);
  3774. }
  3775. /*
  3776. * Delayed printk version, for scheduler-internal messages:
  3777. */
  3778. #define PRINTK_PENDING_WAKEUP 0x01
  3779. #define PRINTK_PENDING_OUTPUT 0x02
  3780. static DEFINE_PER_CPU(int, printk_pending);
  3781. static void wake_up_klogd_work_func(struct irq_work *irq_work)
  3782. {
  3783. int pending = this_cpu_xchg(printk_pending, 0);
  3784. if (pending & PRINTK_PENDING_OUTPUT) {
  3785. if (force_legacy_kthread()) {
  3786. if (printk_legacy_kthread)
  3787. wake_up_interruptible(&legacy_wait);
  3788. } else {
  3789. if (console_trylock())
  3790. console_unlock();
  3791. }
  3792. }
  3793. if (pending & PRINTK_PENDING_WAKEUP)
  3794. wake_up_interruptible(&log_wait);
  3795. }
  3796. static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) =
  3797. IRQ_WORK_INIT_LAZY(wake_up_klogd_work_func);
  3798. static void __wake_up_klogd(int val)
  3799. {
  3800. if (!printk_percpu_data_ready())
  3801. return;
  3802. preempt_disable();
  3803. /*
  3804. * Guarantee any new records can be seen by tasks preparing to wait
  3805. * before this context checks if the wait queue is empty.
  3806. *
  3807. * The full memory barrier within wq_has_sleeper() pairs with the full
  3808. * memory barrier within set_current_state() of
  3809. * prepare_to_wait_event(), which is called after ___wait_event() adds
  3810. * the waiter but before it has checked the wait condition.
  3811. *
  3812. * This pairs with devkmsg_read:A and syslog_print:A.
  3813. */
  3814. if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */
  3815. (val & PRINTK_PENDING_OUTPUT)) {
  3816. this_cpu_or(printk_pending, val);
  3817. irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
  3818. }
  3819. preempt_enable();
  3820. }
  3821. /**
  3822. * wake_up_klogd - Wake kernel logging daemon
  3823. *
  3824. * Use this function when new records have been added to the ringbuffer
  3825. * and the console printing of those records has already occurred or is
  3826. * known to be handled by some other context. This function will only
  3827. * wake the logging daemon.
  3828. *
  3829. * Context: Any context.
  3830. */
  3831. void wake_up_klogd(void)
  3832. {
  3833. __wake_up_klogd(PRINTK_PENDING_WAKEUP);
  3834. }
  3835. /**
  3836. * defer_console_output - Wake kernel logging daemon and trigger
  3837. * console printing in a deferred context
  3838. *
  3839. * Use this function when new records have been added to the ringbuffer,
  3840. * this context is responsible for console printing those records, but
  3841. * the current context is not allowed to perform the console printing.
  3842. * Trigger an irq_work context to perform the console printing. This
  3843. * function also wakes the logging daemon.
  3844. *
  3845. * Context: Any context.
  3846. */
  3847. void defer_console_output(void)
  3848. {
  3849. /*
  3850. * New messages may have been added directly to the ringbuffer
  3851. * using vprintk_store(), so wake any waiters as well.
  3852. */
  3853. __wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT);
  3854. }
  3855. void printk_trigger_flush(void)
  3856. {
  3857. defer_console_output();
  3858. }
  3859. int vprintk_deferred(const char *fmt, va_list args)
  3860. {
  3861. return vprintk_emit(0, LOGLEVEL_SCHED, NULL, fmt, args);
  3862. }
  3863. int _printk_deferred(const char *fmt, ...)
  3864. {
  3865. va_list args;
  3866. int r;
  3867. va_start(args, fmt);
  3868. r = vprintk_deferred(fmt, args);
  3869. va_end(args);
  3870. return r;
  3871. }
  3872. /*
  3873. * printk rate limiting, lifted from the networking subsystem.
  3874. *
  3875. * This enforces a rate limit: not more than 10 kernel messages
  3876. * every 5s to make a denial-of-service attack impossible.
  3877. */
  3878. DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
  3879. int __printk_ratelimit(const char *func)
  3880. {
  3881. return ___ratelimit(&printk_ratelimit_state, func);
  3882. }
  3883. EXPORT_SYMBOL(__printk_ratelimit);
  3884. /**
  3885. * printk_timed_ratelimit - caller-controlled printk ratelimiting
  3886. * @caller_jiffies: pointer to caller's state
  3887. * @interval_msecs: minimum interval between prints
  3888. *
  3889. * printk_timed_ratelimit() returns true if more than @interval_msecs
  3890. * milliseconds have elapsed since the last time printk_timed_ratelimit()
  3891. * returned true.
  3892. */
  3893. bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  3894. unsigned int interval_msecs)
  3895. {
  3896. unsigned long elapsed = jiffies - *caller_jiffies;
  3897. if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs))
  3898. return false;
  3899. *caller_jiffies = jiffies;
  3900. return true;
  3901. }
  3902. EXPORT_SYMBOL(printk_timed_ratelimit);
  3903. static DEFINE_SPINLOCK(dump_list_lock);
  3904. static LIST_HEAD(dump_list);
  3905. /**
  3906. * kmsg_dump_register - register a kernel log dumper.
  3907. * @dumper: pointer to the kmsg_dumper structure
  3908. *
  3909. * Adds a kernel log dumper to the system. The dump callback in the
  3910. * structure will be called when the kernel oopses or panics and must be
  3911. * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
  3912. */
  3913. int kmsg_dump_register(struct kmsg_dumper *dumper)
  3914. {
  3915. unsigned long flags;
  3916. int err = -EBUSY;
  3917. /* The dump callback needs to be set */
  3918. if (!dumper->dump)
  3919. return -EINVAL;
  3920. spin_lock_irqsave(&dump_list_lock, flags);
  3921. /* Don't allow registering multiple times */
  3922. if (!dumper->registered) {
  3923. dumper->registered = 1;
  3924. list_add_tail_rcu(&dumper->list, &dump_list);
  3925. err = 0;
  3926. }
  3927. spin_unlock_irqrestore(&dump_list_lock, flags);
  3928. return err;
  3929. }
  3930. EXPORT_SYMBOL_GPL(kmsg_dump_register);
  3931. /**
  3932. * kmsg_dump_unregister - unregister a kmsg dumper.
  3933. * @dumper: pointer to the kmsg_dumper structure
  3934. *
  3935. * Removes a dump device from the system. Returns zero on success and
  3936. * %-EINVAL otherwise.
  3937. */
  3938. int kmsg_dump_unregister(struct kmsg_dumper *dumper)
  3939. {
  3940. unsigned long flags;
  3941. int err = -EINVAL;
  3942. spin_lock_irqsave(&dump_list_lock, flags);
  3943. if (dumper->registered) {
  3944. dumper->registered = 0;
  3945. list_del_rcu(&dumper->list);
  3946. err = 0;
  3947. }
  3948. spin_unlock_irqrestore(&dump_list_lock, flags);
  3949. synchronize_rcu();
  3950. return err;
  3951. }
  3952. EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
  3953. static bool always_kmsg_dump;
  3954. module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
  3955. const char *kmsg_dump_reason_str(enum kmsg_dump_reason reason)
  3956. {
  3957. switch (reason) {
  3958. case KMSG_DUMP_PANIC:
  3959. return "Panic";
  3960. case KMSG_DUMP_OOPS:
  3961. return "Oops";
  3962. case KMSG_DUMP_EMERG:
  3963. return "Emergency";
  3964. case KMSG_DUMP_SHUTDOWN:
  3965. return "Shutdown";
  3966. default:
  3967. return "Unknown";
  3968. }
  3969. }
  3970. EXPORT_SYMBOL_GPL(kmsg_dump_reason_str);
  3971. /**
  3972. * kmsg_dump_desc - dump kernel log to kernel message dumpers.
  3973. * @reason: the reason (oops, panic etc) for dumping
  3974. * @desc: a short string to describe what caused the panic or oops. Can be NULL
  3975. * if no additional description is available.
  3976. *
  3977. * Call each of the registered dumper's dump() callback, which can
  3978. * retrieve the kmsg records with kmsg_dump_get_line() or
  3979. * kmsg_dump_get_buffer().
  3980. */
  3981. void kmsg_dump_desc(enum kmsg_dump_reason reason, const char *desc)
  3982. {
  3983. struct kmsg_dumper *dumper;
  3984. struct kmsg_dump_detail detail = {
  3985. .reason = reason,
  3986. .description = desc};
  3987. rcu_read_lock();
  3988. list_for_each_entry_rcu(dumper, &dump_list, list) {
  3989. enum kmsg_dump_reason max_reason = dumper->max_reason;
  3990. /*
  3991. * If client has not provided a specific max_reason, default
  3992. * to KMSG_DUMP_OOPS, unless always_kmsg_dump was set.
  3993. */
  3994. if (max_reason == KMSG_DUMP_UNDEF) {
  3995. max_reason = always_kmsg_dump ? KMSG_DUMP_MAX :
  3996. KMSG_DUMP_OOPS;
  3997. }
  3998. if (reason > max_reason)
  3999. continue;
  4000. /* invoke dumper which will iterate over records */
  4001. dumper->dump(dumper, &detail);
  4002. }
  4003. rcu_read_unlock();
  4004. }
  4005. /**
  4006. * kmsg_dump_get_line - retrieve one kmsg log line
  4007. * @iter: kmsg dump iterator
  4008. * @syslog: include the "<4>" prefixes
  4009. * @line: buffer to copy the line to
  4010. * @size: maximum size of the buffer
  4011. * @len: length of line placed into buffer
  4012. *
  4013. * Start at the beginning of the kmsg buffer, with the oldest kmsg
  4014. * record, and copy one record into the provided buffer.
  4015. *
  4016. * Consecutive calls will return the next available record moving
  4017. * towards the end of the buffer with the youngest messages.
  4018. *
  4019. * A return value of FALSE indicates that there are no more records to
  4020. * read.
  4021. */
  4022. bool kmsg_dump_get_line(struct kmsg_dump_iter *iter, bool syslog,
  4023. char *line, size_t size, size_t *len)
  4024. {
  4025. u64 min_seq = latched_seq_read_nolock(&clear_seq);
  4026. struct printk_info info;
  4027. unsigned int line_count;
  4028. struct printk_record r;
  4029. size_t l = 0;
  4030. bool ret = false;
  4031. if (iter->cur_seq < min_seq)
  4032. iter->cur_seq = min_seq;
  4033. prb_rec_init_rd(&r, &info, line, size);
  4034. /* Read text or count text lines? */
  4035. if (line) {
  4036. if (!prb_read_valid(prb, iter->cur_seq, &r))
  4037. goto out;
  4038. l = record_print_text(&r, syslog, printk_time);
  4039. } else {
  4040. if (!prb_read_valid_info(prb, iter->cur_seq,
  4041. &info, &line_count)) {
  4042. goto out;
  4043. }
  4044. l = get_record_print_text_size(&info, line_count, syslog,
  4045. printk_time);
  4046. }
  4047. iter->cur_seq = r.info->seq + 1;
  4048. ret = true;
  4049. out:
  4050. if (len)
  4051. *len = l;
  4052. return ret;
  4053. }
  4054. EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
  4055. /**
  4056. * kmsg_dump_get_buffer - copy kmsg log lines
  4057. * @iter: kmsg dump iterator
  4058. * @syslog: include the "<4>" prefixes
  4059. * @buf: buffer to copy the line to
  4060. * @size: maximum size of the buffer
  4061. * @len_out: length of line placed into buffer
  4062. *
  4063. * Start at the end of the kmsg buffer and fill the provided buffer
  4064. * with as many of the *youngest* kmsg records that fit into it.
  4065. * If the buffer is large enough, all available kmsg records will be
  4066. * copied with a single call.
  4067. *
  4068. * Consecutive calls will fill the buffer with the next block of
  4069. * available older records, not including the earlier retrieved ones.
  4070. *
  4071. * A return value of FALSE indicates that there are no more records to
  4072. * read.
  4073. */
  4074. bool kmsg_dump_get_buffer(struct kmsg_dump_iter *iter, bool syslog,
  4075. char *buf, size_t size, size_t *len_out)
  4076. {
  4077. u64 min_seq = latched_seq_read_nolock(&clear_seq);
  4078. struct printk_info info;
  4079. struct printk_record r;
  4080. u64 seq;
  4081. u64 next_seq;
  4082. size_t len = 0;
  4083. bool ret = false;
  4084. bool time = printk_time;
  4085. if (!buf || !size)
  4086. goto out;
  4087. if (iter->cur_seq < min_seq)
  4088. iter->cur_seq = min_seq;
  4089. if (prb_read_valid_info(prb, iter->cur_seq, &info, NULL)) {
  4090. if (info.seq != iter->cur_seq) {
  4091. /* messages are gone, move to first available one */
  4092. iter->cur_seq = info.seq;
  4093. }
  4094. }
  4095. /* last entry */
  4096. if (iter->cur_seq >= iter->next_seq)
  4097. goto out;
  4098. /*
  4099. * Find first record that fits, including all following records,
  4100. * into the user-provided buffer for this dump. Pass in size-1
  4101. * because this function (by way of record_print_text()) will
  4102. * not write more than size-1 bytes of text into @buf.
  4103. */
  4104. seq = find_first_fitting_seq(iter->cur_seq, iter->next_seq,
  4105. size - 1, syslog, time);
  4106. /*
  4107. * Next kmsg_dump_get_buffer() invocation will dump block of
  4108. * older records stored right before this one.
  4109. */
  4110. next_seq = seq;
  4111. prb_rec_init_rd(&r, &info, buf, size);
  4112. prb_for_each_record(seq, prb, seq, &r) {
  4113. if (r.info->seq >= iter->next_seq)
  4114. break;
  4115. len += record_print_text(&r, syslog, time);
  4116. /* Adjust record to store to remaining buffer space. */
  4117. prb_rec_init_rd(&r, &info, buf + len, size - len);
  4118. }
  4119. iter->next_seq = next_seq;
  4120. ret = true;
  4121. out:
  4122. if (len_out)
  4123. *len_out = len;
  4124. return ret;
  4125. }
  4126. EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
  4127. /**
  4128. * kmsg_dump_rewind - reset the iterator
  4129. * @iter: kmsg dump iterator
  4130. *
  4131. * Reset the dumper's iterator so that kmsg_dump_get_line() and
  4132. * kmsg_dump_get_buffer() can be called again and used multiple
  4133. * times within the same dumper.dump() callback.
  4134. */
  4135. void kmsg_dump_rewind(struct kmsg_dump_iter *iter)
  4136. {
  4137. iter->cur_seq = latched_seq_read_nolock(&clear_seq);
  4138. iter->next_seq = prb_next_seq(prb);
  4139. }
  4140. EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
  4141. /**
  4142. * console_try_replay_all - try to replay kernel log on consoles
  4143. *
  4144. * Try to obtain lock on console subsystem and replay all
  4145. * available records in printk buffer on the consoles.
  4146. * Does nothing if lock is not obtained.
  4147. *
  4148. * Context: Any, except for NMI.
  4149. */
  4150. void console_try_replay_all(void)
  4151. {
  4152. struct console_flush_type ft;
  4153. printk_get_console_flush_type(&ft);
  4154. if (console_trylock()) {
  4155. __console_rewind_all();
  4156. if (ft.nbcon_atomic)
  4157. nbcon_atomic_flush_pending();
  4158. if (ft.nbcon_offload)
  4159. nbcon_kthreads_wake();
  4160. if (ft.legacy_offload)
  4161. defer_console_output();
  4162. /* Consoles are flushed as part of console_unlock(). */
  4163. console_unlock();
  4164. }
  4165. }
  4166. #endif
  4167. #ifdef CONFIG_SMP
  4168. static atomic_t printk_cpu_sync_owner = ATOMIC_INIT(-1);
  4169. static atomic_t printk_cpu_sync_nested = ATOMIC_INIT(0);
  4170. bool is_printk_cpu_sync_owner(void)
  4171. {
  4172. return (atomic_read(&printk_cpu_sync_owner) == raw_smp_processor_id());
  4173. }
  4174. /**
  4175. * __printk_cpu_sync_wait() - Busy wait until the printk cpu-reentrant
  4176. * spinning lock is not owned by any CPU.
  4177. *
  4178. * Context: Any context.
  4179. */
  4180. void __printk_cpu_sync_wait(void)
  4181. {
  4182. do {
  4183. cpu_relax();
  4184. } while (atomic_read(&printk_cpu_sync_owner) != -1);
  4185. }
  4186. EXPORT_SYMBOL(__printk_cpu_sync_wait);
  4187. /**
  4188. * __printk_cpu_sync_try_get() - Try to acquire the printk cpu-reentrant
  4189. * spinning lock.
  4190. *
  4191. * If no processor has the lock, the calling processor takes the lock and
  4192. * becomes the owner. If the calling processor is already the owner of the
  4193. * lock, this function succeeds immediately.
  4194. *
  4195. * Context: Any context. Expects interrupts to be disabled.
  4196. * Return: 1 on success, otherwise 0.
  4197. */
  4198. int __printk_cpu_sync_try_get(void)
  4199. {
  4200. int cpu;
  4201. int old;
  4202. cpu = smp_processor_id();
  4203. /*
  4204. * Guarantee loads and stores from this CPU when it is the lock owner
  4205. * are _not_ visible to the previous lock owner. This pairs with
  4206. * __printk_cpu_sync_put:B.
  4207. *
  4208. * Memory barrier involvement:
  4209. *
  4210. * If __printk_cpu_sync_try_get:A reads from __printk_cpu_sync_put:B,
  4211. * then __printk_cpu_sync_put:A can never read from
  4212. * __printk_cpu_sync_try_get:B.
  4213. *
  4214. * Relies on:
  4215. *
  4216. * RELEASE from __printk_cpu_sync_put:A to __printk_cpu_sync_put:B
  4217. * of the previous CPU
  4218. * matching
  4219. * ACQUIRE from __printk_cpu_sync_try_get:A to
  4220. * __printk_cpu_sync_try_get:B of this CPU
  4221. */
  4222. old = atomic_cmpxchg_acquire(&printk_cpu_sync_owner, -1,
  4223. cpu); /* LMM(__printk_cpu_sync_try_get:A) */
  4224. if (old == -1) {
  4225. /*
  4226. * This CPU is now the owner and begins loading/storing
  4227. * data: LMM(__printk_cpu_sync_try_get:B)
  4228. */
  4229. return 1;
  4230. } else if (old == cpu) {
  4231. /* This CPU is already the owner. */
  4232. atomic_inc(&printk_cpu_sync_nested);
  4233. return 1;
  4234. }
  4235. return 0;
  4236. }
  4237. EXPORT_SYMBOL(__printk_cpu_sync_try_get);
  4238. /**
  4239. * __printk_cpu_sync_put() - Release the printk cpu-reentrant spinning lock.
  4240. *
  4241. * The calling processor must be the owner of the lock.
  4242. *
  4243. * Context: Any context. Expects interrupts to be disabled.
  4244. */
  4245. void __printk_cpu_sync_put(void)
  4246. {
  4247. if (atomic_read(&printk_cpu_sync_nested)) {
  4248. atomic_dec(&printk_cpu_sync_nested);
  4249. return;
  4250. }
  4251. /*
  4252. * This CPU is finished loading/storing data:
  4253. * LMM(__printk_cpu_sync_put:A)
  4254. */
  4255. /*
  4256. * Guarantee loads and stores from this CPU when it was the
  4257. * lock owner are visible to the next lock owner. This pairs
  4258. * with __printk_cpu_sync_try_get:A.
  4259. *
  4260. * Memory barrier involvement:
  4261. *
  4262. * If __printk_cpu_sync_try_get:A reads from __printk_cpu_sync_put:B,
  4263. * then __printk_cpu_sync_try_get:B reads from __printk_cpu_sync_put:A.
  4264. *
  4265. * Relies on:
  4266. *
  4267. * RELEASE from __printk_cpu_sync_put:A to __printk_cpu_sync_put:B
  4268. * of this CPU
  4269. * matching
  4270. * ACQUIRE from __printk_cpu_sync_try_get:A to
  4271. * __printk_cpu_sync_try_get:B of the next CPU
  4272. */
  4273. atomic_set_release(&printk_cpu_sync_owner,
  4274. -1); /* LMM(__printk_cpu_sync_put:B) */
  4275. }
  4276. EXPORT_SYMBOL(__printk_cpu_sync_put);
  4277. #endif /* CONFIG_SMP */