printk.c 57 KB

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