printk.c 70 KB

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