printk.c 70 KB

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