printk.c 70 KB

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