printk.c 84 KB

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