printk.c 72 KB

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