printk.c 72 KB

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