printk.c 70 KB

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