printk.c 85 KB

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