ring_buffer.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209
  1. /*
  2. * Generic ring buffer
  3. *
  4. * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
  5. */
  6. #include <linux/ring_buffer.h>
  7. #include <linux/trace_clock.h>
  8. #include <linux/ftrace_irq.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/hardirq.h>
  13. #include <linux/module.h>
  14. #include <linux/percpu.h>
  15. #include <linux/mutex.h>
  16. #include <linux/init.h>
  17. #include <linux/hash.h>
  18. #include <linux/list.h>
  19. #include <linux/cpu.h>
  20. #include <linux/fs.h>
  21. #include "trace.h"
  22. /*
  23. * The ring buffer header is special. We must manually up keep it.
  24. */
  25. int ring_buffer_print_entry_header(struct trace_seq *s)
  26. {
  27. int ret;
  28. ret = trace_seq_printf(s, "# compressed entry header\n");
  29. ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
  30. ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
  31. ret = trace_seq_printf(s, "\tarray : 32 bits\n");
  32. ret = trace_seq_printf(s, "\n");
  33. ret = trace_seq_printf(s, "\tpadding : type == %d\n",
  34. RINGBUF_TYPE_PADDING);
  35. ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
  36. RINGBUF_TYPE_TIME_EXTEND);
  37. ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
  38. RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
  39. return ret;
  40. }
  41. /*
  42. * The ring buffer is made up of a list of pages. A separate list of pages is
  43. * allocated for each CPU. A writer may only write to a buffer that is
  44. * associated with the CPU it is currently executing on. A reader may read
  45. * from any per cpu buffer.
  46. *
  47. * The reader is special. For each per cpu buffer, the reader has its own
  48. * reader page. When a reader has read the entire reader page, this reader
  49. * page is swapped with another page in the ring buffer.
  50. *
  51. * Now, as long as the writer is off the reader page, the reader can do what
  52. * ever it wants with that page. The writer will never write to that page
  53. * again (as long as it is out of the ring buffer).
  54. *
  55. * Here's some silly ASCII art.
  56. *
  57. * +------+
  58. * |reader| RING BUFFER
  59. * |page |
  60. * +------+ +---+ +---+ +---+
  61. * | |-->| |-->| |
  62. * +---+ +---+ +---+
  63. * ^ |
  64. * | |
  65. * +---------------+
  66. *
  67. *
  68. * +------+
  69. * |reader| RING BUFFER
  70. * |page |------------------v
  71. * +------+ +---+ +---+ +---+
  72. * | |-->| |-->| |
  73. * +---+ +---+ +---+
  74. * ^ |
  75. * | |
  76. * +---------------+
  77. *
  78. *
  79. * +------+
  80. * |reader| RING BUFFER
  81. * |page |------------------v
  82. * +------+ +---+ +---+ +---+
  83. * ^ | |-->| |-->| |
  84. * | +---+ +---+ +---+
  85. * | |
  86. * | |
  87. * +------------------------------+
  88. *
  89. *
  90. * +------+
  91. * |buffer| RING BUFFER
  92. * |page |------------------v
  93. * +------+ +---+ +---+ +---+
  94. * ^ | | | |-->| |
  95. * | New +---+ +---+ +---+
  96. * | Reader------^ |
  97. * | page |
  98. * +------------------------------+
  99. *
  100. *
  101. * After we make this swap, the reader can hand this page off to the splice
  102. * code and be done with it. It can even allocate a new page if it needs to
  103. * and swap that into the ring buffer.
  104. *
  105. * We will be using cmpxchg soon to make all this lockless.
  106. *
  107. */
  108. /*
  109. * A fast way to enable or disable all ring buffers is to
  110. * call tracing_on or tracing_off. Turning off the ring buffers
  111. * prevents all ring buffers from being recorded to.
  112. * Turning this switch on, makes it OK to write to the
  113. * ring buffer, if the ring buffer is enabled itself.
  114. *
  115. * There's three layers that must be on in order to write
  116. * to the ring buffer.
  117. *
  118. * 1) This global flag must be set.
  119. * 2) The ring buffer must be enabled for recording.
  120. * 3) The per cpu buffer must be enabled for recording.
  121. *
  122. * In case of an anomaly, this global flag has a bit set that
  123. * will permantly disable all ring buffers.
  124. */
  125. /*
  126. * Global flag to disable all recording to ring buffers
  127. * This has two bits: ON, DISABLED
  128. *
  129. * ON DISABLED
  130. * ---- ----------
  131. * 0 0 : ring buffers are off
  132. * 1 0 : ring buffers are on
  133. * X 1 : ring buffers are permanently disabled
  134. */
  135. enum {
  136. RB_BUFFERS_ON_BIT = 0,
  137. RB_BUFFERS_DISABLED_BIT = 1,
  138. };
  139. enum {
  140. RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
  141. RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
  142. };
  143. static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
  144. #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
  145. /**
  146. * tracing_on - enable all tracing buffers
  147. *
  148. * This function enables all tracing buffers that may have been
  149. * disabled with tracing_off.
  150. */
  151. void tracing_on(void)
  152. {
  153. set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  154. }
  155. EXPORT_SYMBOL_GPL(tracing_on);
  156. /**
  157. * tracing_off - turn off all tracing buffers
  158. *
  159. * This function stops all tracing buffers from recording data.
  160. * It does not disable any overhead the tracers themselves may
  161. * be causing. This function simply causes all recording to
  162. * the ring buffers to fail.
  163. */
  164. void tracing_off(void)
  165. {
  166. clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  167. }
  168. EXPORT_SYMBOL_GPL(tracing_off);
  169. /**
  170. * tracing_off_permanent - permanently disable ring buffers
  171. *
  172. * This function, once called, will disable all ring buffers
  173. * permanently.
  174. */
  175. void tracing_off_permanent(void)
  176. {
  177. set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
  178. }
  179. /**
  180. * tracing_is_on - show state of ring buffers enabled
  181. */
  182. int tracing_is_on(void)
  183. {
  184. return ring_buffer_flags == RB_BUFFERS_ON;
  185. }
  186. EXPORT_SYMBOL_GPL(tracing_is_on);
  187. #include "trace.h"
  188. #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
  189. #define RB_ALIGNMENT 4U
  190. #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  191. #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
  192. /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
  193. #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
  194. enum {
  195. RB_LEN_TIME_EXTEND = 8,
  196. RB_LEN_TIME_STAMP = 16,
  197. };
  198. static inline int rb_null_event(struct ring_buffer_event *event)
  199. {
  200. return event->type_len == RINGBUF_TYPE_PADDING
  201. && event->time_delta == 0;
  202. }
  203. static inline int rb_discarded_event(struct ring_buffer_event *event)
  204. {
  205. return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
  206. }
  207. static void rb_event_set_padding(struct ring_buffer_event *event)
  208. {
  209. event->type_len = RINGBUF_TYPE_PADDING;
  210. event->time_delta = 0;
  211. }
  212. static unsigned
  213. rb_event_data_length(struct ring_buffer_event *event)
  214. {
  215. unsigned length;
  216. if (event->type_len)
  217. length = event->type_len * RB_ALIGNMENT;
  218. else
  219. length = event->array[0];
  220. return length + RB_EVNT_HDR_SIZE;
  221. }
  222. /* inline for ring buffer fast paths */
  223. static unsigned
  224. rb_event_length(struct ring_buffer_event *event)
  225. {
  226. switch (event->type_len) {
  227. case RINGBUF_TYPE_PADDING:
  228. if (rb_null_event(event))
  229. /* undefined */
  230. return -1;
  231. return event->array[0] + RB_EVNT_HDR_SIZE;
  232. case RINGBUF_TYPE_TIME_EXTEND:
  233. return RB_LEN_TIME_EXTEND;
  234. case RINGBUF_TYPE_TIME_STAMP:
  235. return RB_LEN_TIME_STAMP;
  236. case RINGBUF_TYPE_DATA:
  237. return rb_event_data_length(event);
  238. default:
  239. BUG();
  240. }
  241. /* not hit */
  242. return 0;
  243. }
  244. /**
  245. * ring_buffer_event_length - return the length of the event
  246. * @event: the event to get the length of
  247. */
  248. unsigned ring_buffer_event_length(struct ring_buffer_event *event)
  249. {
  250. unsigned length = rb_event_length(event);
  251. if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  252. return length;
  253. length -= RB_EVNT_HDR_SIZE;
  254. if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
  255. length -= sizeof(event->array[0]);
  256. return length;
  257. }
  258. EXPORT_SYMBOL_GPL(ring_buffer_event_length);
  259. /* inline for ring buffer fast paths */
  260. static void *
  261. rb_event_data(struct ring_buffer_event *event)
  262. {
  263. BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
  264. /* If length is in len field, then array[0] has the data */
  265. if (event->type_len)
  266. return (void *)&event->array[0];
  267. /* Otherwise length is in array[0] and array[1] has the data */
  268. return (void *)&event->array[1];
  269. }
  270. /**
  271. * ring_buffer_event_data - return the data of the event
  272. * @event: the event to get the data from
  273. */
  274. void *ring_buffer_event_data(struct ring_buffer_event *event)
  275. {
  276. return rb_event_data(event);
  277. }
  278. EXPORT_SYMBOL_GPL(ring_buffer_event_data);
  279. #define for_each_buffer_cpu(buffer, cpu) \
  280. for_each_cpu(cpu, buffer->cpumask)
  281. #define TS_SHIFT 27
  282. #define TS_MASK ((1ULL << TS_SHIFT) - 1)
  283. #define TS_DELTA_TEST (~TS_MASK)
  284. struct buffer_data_page {
  285. u64 time_stamp; /* page time stamp */
  286. local_t commit; /* write committed index */
  287. unsigned char data[]; /* data of buffer page */
  288. };
  289. struct buffer_page {
  290. struct list_head list; /* list of buffer pages */
  291. local_t write; /* index for next write */
  292. unsigned read; /* index for next read */
  293. local_t entries; /* entries on this page */
  294. struct buffer_data_page *page; /* Actual data page */
  295. };
  296. static void rb_init_page(struct buffer_data_page *bpage)
  297. {
  298. local_set(&bpage->commit, 0);
  299. }
  300. /**
  301. * ring_buffer_page_len - the size of data on the page.
  302. * @page: The page to read
  303. *
  304. * Returns the amount of data on the page, including buffer page header.
  305. */
  306. size_t ring_buffer_page_len(void *page)
  307. {
  308. return local_read(&((struct buffer_data_page *)page)->commit)
  309. + BUF_PAGE_HDR_SIZE;
  310. }
  311. /*
  312. * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
  313. * this issue out.
  314. */
  315. static void free_buffer_page(struct buffer_page *bpage)
  316. {
  317. free_page((unsigned long)bpage->page);
  318. kfree(bpage);
  319. }
  320. /*
  321. * We need to fit the time_stamp delta into 27 bits.
  322. */
  323. static inline int test_time_stamp(u64 delta)
  324. {
  325. if (delta & TS_DELTA_TEST)
  326. return 1;
  327. return 0;
  328. }
  329. #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
  330. /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
  331. #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
  332. /* Max number of timestamps that can fit on a page */
  333. #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
  334. int ring_buffer_print_page_header(struct trace_seq *s)
  335. {
  336. struct buffer_data_page field;
  337. int ret;
  338. ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
  339. "offset:0;\tsize:%u;\n",
  340. (unsigned int)sizeof(field.time_stamp));
  341. ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
  342. "offset:%u;\tsize:%u;\n",
  343. (unsigned int)offsetof(typeof(field), commit),
  344. (unsigned int)sizeof(field.commit));
  345. ret = trace_seq_printf(s, "\tfield: char data;\t"
  346. "offset:%u;\tsize:%u;\n",
  347. (unsigned int)offsetof(typeof(field), data),
  348. (unsigned int)BUF_PAGE_SIZE);
  349. return ret;
  350. }
  351. /*
  352. * head_page == tail_page && head == tail then buffer is empty.
  353. */
  354. struct ring_buffer_per_cpu {
  355. int cpu;
  356. struct ring_buffer *buffer;
  357. spinlock_t reader_lock; /* serialize readers */
  358. raw_spinlock_t lock;
  359. struct lock_class_key lock_key;
  360. struct list_head pages;
  361. struct buffer_page *head_page; /* read from head */
  362. struct buffer_page *tail_page; /* write to tail */
  363. struct buffer_page *commit_page; /* committed pages */
  364. struct buffer_page *reader_page;
  365. unsigned long nmi_dropped;
  366. unsigned long commit_overrun;
  367. unsigned long overrun;
  368. unsigned long read;
  369. local_t entries;
  370. local_t committing;
  371. local_t commits;
  372. u64 write_stamp;
  373. u64 read_stamp;
  374. atomic_t record_disabled;
  375. };
  376. struct ring_buffer {
  377. unsigned pages;
  378. unsigned flags;
  379. int cpus;
  380. atomic_t record_disabled;
  381. cpumask_var_t cpumask;
  382. struct lock_class_key *reader_lock_key;
  383. struct mutex mutex;
  384. struct ring_buffer_per_cpu **buffers;
  385. #ifdef CONFIG_HOTPLUG_CPU
  386. struct notifier_block cpu_notify;
  387. #endif
  388. u64 (*clock)(void);
  389. };
  390. struct ring_buffer_iter {
  391. struct ring_buffer_per_cpu *cpu_buffer;
  392. unsigned long head;
  393. struct buffer_page *head_page;
  394. u64 read_stamp;
  395. };
  396. /* buffer may be either ring_buffer or ring_buffer_per_cpu */
  397. #define RB_WARN_ON(buffer, cond) \
  398. ({ \
  399. int _____ret = unlikely(cond); \
  400. if (_____ret) { \
  401. atomic_inc(&buffer->record_disabled); \
  402. WARN_ON(1); \
  403. } \
  404. _____ret; \
  405. })
  406. /* Up this if you want to test the TIME_EXTENTS and normalization */
  407. #define DEBUG_SHIFT 0
  408. static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
  409. {
  410. /* shift to debug/test normalization and TIME_EXTENTS */
  411. return buffer->clock() << DEBUG_SHIFT;
  412. }
  413. u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
  414. {
  415. u64 time;
  416. preempt_disable_notrace();
  417. time = rb_time_stamp(buffer, cpu);
  418. preempt_enable_no_resched_notrace();
  419. return time;
  420. }
  421. EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
  422. void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
  423. int cpu, u64 *ts)
  424. {
  425. /* Just stupid testing the normalize function and deltas */
  426. *ts >>= DEBUG_SHIFT;
  427. }
  428. EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
  429. /**
  430. * check_pages - integrity check of buffer pages
  431. * @cpu_buffer: CPU buffer with pages to test
  432. *
  433. * As a safety measure we check to make sure the data pages have not
  434. * been corrupted.
  435. */
  436. static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  437. {
  438. struct list_head *head = &cpu_buffer->pages;
  439. struct buffer_page *bpage, *tmp;
  440. if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
  441. return -1;
  442. if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
  443. return -1;
  444. list_for_each_entry_safe(bpage, tmp, head, list) {
  445. if (RB_WARN_ON(cpu_buffer,
  446. bpage->list.next->prev != &bpage->list))
  447. return -1;
  448. if (RB_WARN_ON(cpu_buffer,
  449. bpage->list.prev->next != &bpage->list))
  450. return -1;
  451. }
  452. return 0;
  453. }
  454. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  455. unsigned nr_pages)
  456. {
  457. struct list_head *head = &cpu_buffer->pages;
  458. struct buffer_page *bpage, *tmp;
  459. unsigned long addr;
  460. LIST_HEAD(pages);
  461. unsigned i;
  462. for (i = 0; i < nr_pages; i++) {
  463. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  464. GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
  465. if (!bpage)
  466. goto free_pages;
  467. list_add(&bpage->list, &pages);
  468. addr = __get_free_page(GFP_KERNEL);
  469. if (!addr)
  470. goto free_pages;
  471. bpage->page = (void *)addr;
  472. rb_init_page(bpage->page);
  473. }
  474. list_splice(&pages, head);
  475. rb_check_pages(cpu_buffer);
  476. return 0;
  477. free_pages:
  478. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  479. list_del_init(&bpage->list);
  480. free_buffer_page(bpage);
  481. }
  482. return -ENOMEM;
  483. }
  484. static struct ring_buffer_per_cpu *
  485. rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
  486. {
  487. struct ring_buffer_per_cpu *cpu_buffer;
  488. struct buffer_page *bpage;
  489. unsigned long addr;
  490. int ret;
  491. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  492. GFP_KERNEL, cpu_to_node(cpu));
  493. if (!cpu_buffer)
  494. return NULL;
  495. cpu_buffer->cpu = cpu;
  496. cpu_buffer->buffer = buffer;
  497. spin_lock_init(&cpu_buffer->reader_lock);
  498. lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
  499. cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  500. INIT_LIST_HEAD(&cpu_buffer->pages);
  501. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  502. GFP_KERNEL, cpu_to_node(cpu));
  503. if (!bpage)
  504. goto fail_free_buffer;
  505. cpu_buffer->reader_page = bpage;
  506. addr = __get_free_page(GFP_KERNEL);
  507. if (!addr)
  508. goto fail_free_reader;
  509. bpage->page = (void *)addr;
  510. rb_init_page(bpage->page);
  511. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  512. ret = rb_allocate_pages(cpu_buffer, buffer->pages);
  513. if (ret < 0)
  514. goto fail_free_reader;
  515. cpu_buffer->head_page
  516. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  517. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  518. return cpu_buffer;
  519. fail_free_reader:
  520. free_buffer_page(cpu_buffer->reader_page);
  521. fail_free_buffer:
  522. kfree(cpu_buffer);
  523. return NULL;
  524. }
  525. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  526. {
  527. struct list_head *head = &cpu_buffer->pages;
  528. struct buffer_page *bpage, *tmp;
  529. free_buffer_page(cpu_buffer->reader_page);
  530. list_for_each_entry_safe(bpage, tmp, head, list) {
  531. list_del_init(&bpage->list);
  532. free_buffer_page(bpage);
  533. }
  534. kfree(cpu_buffer);
  535. }
  536. #ifdef CONFIG_HOTPLUG_CPU
  537. static int rb_cpu_notify(struct notifier_block *self,
  538. unsigned long action, void *hcpu);
  539. #endif
  540. /**
  541. * ring_buffer_alloc - allocate a new ring_buffer
  542. * @size: the size in bytes per cpu that is needed.
  543. * @flags: attributes to set for the ring buffer.
  544. *
  545. * Currently the only flag that is available is the RB_FL_OVERWRITE
  546. * flag. This flag means that the buffer will overwrite old data
  547. * when the buffer wraps. If this flag is not set, the buffer will
  548. * drop data when the tail hits the head.
  549. */
  550. struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
  551. struct lock_class_key *key)
  552. {
  553. struct ring_buffer *buffer;
  554. int bsize;
  555. int cpu;
  556. /* keep it in its own cache line */
  557. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  558. GFP_KERNEL);
  559. if (!buffer)
  560. return NULL;
  561. if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  562. goto fail_free_buffer;
  563. buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  564. buffer->flags = flags;
  565. buffer->clock = trace_clock_local;
  566. buffer->reader_lock_key = key;
  567. /* need at least two pages */
  568. if (buffer->pages < 2)
  569. buffer->pages = 2;
  570. /*
  571. * In case of non-hotplug cpu, if the ring-buffer is allocated
  572. * in early initcall, it will not be notified of secondary cpus.
  573. * In that off case, we need to allocate for all possible cpus.
  574. */
  575. #ifdef CONFIG_HOTPLUG_CPU
  576. get_online_cpus();
  577. cpumask_copy(buffer->cpumask, cpu_online_mask);
  578. #else
  579. cpumask_copy(buffer->cpumask, cpu_possible_mask);
  580. #endif
  581. buffer->cpus = nr_cpu_ids;
  582. bsize = sizeof(void *) * nr_cpu_ids;
  583. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  584. GFP_KERNEL);
  585. if (!buffer->buffers)
  586. goto fail_free_cpumask;
  587. for_each_buffer_cpu(buffer, cpu) {
  588. buffer->buffers[cpu] =
  589. rb_allocate_cpu_buffer(buffer, cpu);
  590. if (!buffer->buffers[cpu])
  591. goto fail_free_buffers;
  592. }
  593. #ifdef CONFIG_HOTPLUG_CPU
  594. buffer->cpu_notify.notifier_call = rb_cpu_notify;
  595. buffer->cpu_notify.priority = 0;
  596. register_cpu_notifier(&buffer->cpu_notify);
  597. #endif
  598. put_online_cpus();
  599. mutex_init(&buffer->mutex);
  600. return buffer;
  601. fail_free_buffers:
  602. for_each_buffer_cpu(buffer, cpu) {
  603. if (buffer->buffers[cpu])
  604. rb_free_cpu_buffer(buffer->buffers[cpu]);
  605. }
  606. kfree(buffer->buffers);
  607. fail_free_cpumask:
  608. free_cpumask_var(buffer->cpumask);
  609. put_online_cpus();
  610. fail_free_buffer:
  611. kfree(buffer);
  612. return NULL;
  613. }
  614. EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
  615. /**
  616. * ring_buffer_free - free a ring buffer.
  617. * @buffer: the buffer to free.
  618. */
  619. void
  620. ring_buffer_free(struct ring_buffer *buffer)
  621. {
  622. int cpu;
  623. get_online_cpus();
  624. #ifdef CONFIG_HOTPLUG_CPU
  625. unregister_cpu_notifier(&buffer->cpu_notify);
  626. #endif
  627. for_each_buffer_cpu(buffer, cpu)
  628. rb_free_cpu_buffer(buffer->buffers[cpu]);
  629. put_online_cpus();
  630. free_cpumask_var(buffer->cpumask);
  631. kfree(buffer);
  632. }
  633. EXPORT_SYMBOL_GPL(ring_buffer_free);
  634. void ring_buffer_set_clock(struct ring_buffer *buffer,
  635. u64 (*clock)(void))
  636. {
  637. buffer->clock = clock;
  638. }
  639. static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
  640. static void
  641. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
  642. {
  643. struct buffer_page *bpage;
  644. struct list_head *p;
  645. unsigned i;
  646. atomic_inc(&cpu_buffer->record_disabled);
  647. synchronize_sched();
  648. for (i = 0; i < nr_pages; i++) {
  649. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  650. return;
  651. p = cpu_buffer->pages.next;
  652. bpage = list_entry(p, struct buffer_page, list);
  653. list_del_init(&bpage->list);
  654. free_buffer_page(bpage);
  655. }
  656. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  657. return;
  658. rb_reset_cpu(cpu_buffer);
  659. rb_check_pages(cpu_buffer);
  660. atomic_dec(&cpu_buffer->record_disabled);
  661. }
  662. static void
  663. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
  664. struct list_head *pages, unsigned nr_pages)
  665. {
  666. struct buffer_page *bpage;
  667. struct list_head *p;
  668. unsigned i;
  669. atomic_inc(&cpu_buffer->record_disabled);
  670. synchronize_sched();
  671. for (i = 0; i < nr_pages; i++) {
  672. if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
  673. return;
  674. p = pages->next;
  675. bpage = list_entry(p, struct buffer_page, list);
  676. list_del_init(&bpage->list);
  677. list_add_tail(&bpage->list, &cpu_buffer->pages);
  678. }
  679. rb_reset_cpu(cpu_buffer);
  680. rb_check_pages(cpu_buffer);
  681. atomic_dec(&cpu_buffer->record_disabled);
  682. }
  683. /**
  684. * ring_buffer_resize - resize the ring buffer
  685. * @buffer: the buffer to resize.
  686. * @size: the new size.
  687. *
  688. * The tracer is responsible for making sure that the buffer is
  689. * not being used while changing the size.
  690. * Note: We may be able to change the above requirement by using
  691. * RCU synchronizations.
  692. *
  693. * Minimum size is 2 * BUF_PAGE_SIZE.
  694. *
  695. * Returns -1 on failure.
  696. */
  697. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
  698. {
  699. struct ring_buffer_per_cpu *cpu_buffer;
  700. unsigned nr_pages, rm_pages, new_pages;
  701. struct buffer_page *bpage, *tmp;
  702. unsigned long buffer_size;
  703. unsigned long addr;
  704. LIST_HEAD(pages);
  705. int i, cpu;
  706. /*
  707. * Always succeed at resizing a non-existent buffer:
  708. */
  709. if (!buffer)
  710. return size;
  711. size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  712. size *= BUF_PAGE_SIZE;
  713. buffer_size = buffer->pages * BUF_PAGE_SIZE;
  714. /* we need a minimum of two pages */
  715. if (size < BUF_PAGE_SIZE * 2)
  716. size = BUF_PAGE_SIZE * 2;
  717. if (size == buffer_size)
  718. return size;
  719. mutex_lock(&buffer->mutex);
  720. get_online_cpus();
  721. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  722. if (size < buffer_size) {
  723. /* easy case, just free pages */
  724. if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
  725. goto out_fail;
  726. rm_pages = buffer->pages - nr_pages;
  727. for_each_buffer_cpu(buffer, cpu) {
  728. cpu_buffer = buffer->buffers[cpu];
  729. rb_remove_pages(cpu_buffer, rm_pages);
  730. }
  731. goto out;
  732. }
  733. /*
  734. * This is a bit more difficult. We only want to add pages
  735. * when we can allocate enough for all CPUs. We do this
  736. * by allocating all the pages and storing them on a local
  737. * link list. If we succeed in our allocation, then we
  738. * add these pages to the cpu_buffers. Otherwise we just free
  739. * them all and return -ENOMEM;
  740. */
  741. if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
  742. goto out_fail;
  743. new_pages = nr_pages - buffer->pages;
  744. for_each_buffer_cpu(buffer, cpu) {
  745. for (i = 0; i < new_pages; i++) {
  746. bpage = kzalloc_node(ALIGN(sizeof(*bpage),
  747. cache_line_size()),
  748. GFP_KERNEL, cpu_to_node(cpu));
  749. if (!bpage)
  750. goto free_pages;
  751. list_add(&bpage->list, &pages);
  752. addr = __get_free_page(GFP_KERNEL);
  753. if (!addr)
  754. goto free_pages;
  755. bpage->page = (void *)addr;
  756. rb_init_page(bpage->page);
  757. }
  758. }
  759. for_each_buffer_cpu(buffer, cpu) {
  760. cpu_buffer = buffer->buffers[cpu];
  761. rb_insert_pages(cpu_buffer, &pages, new_pages);
  762. }
  763. if (RB_WARN_ON(buffer, !list_empty(&pages)))
  764. goto out_fail;
  765. out:
  766. buffer->pages = nr_pages;
  767. put_online_cpus();
  768. mutex_unlock(&buffer->mutex);
  769. return size;
  770. free_pages:
  771. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  772. list_del_init(&bpage->list);
  773. free_buffer_page(bpage);
  774. }
  775. put_online_cpus();
  776. mutex_unlock(&buffer->mutex);
  777. return -ENOMEM;
  778. /*
  779. * Something went totally wrong, and we are too paranoid
  780. * to even clean up the mess.
  781. */
  782. out_fail:
  783. put_online_cpus();
  784. mutex_unlock(&buffer->mutex);
  785. return -1;
  786. }
  787. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  788. static inline void *
  789. __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
  790. {
  791. return bpage->data + index;
  792. }
  793. static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  794. {
  795. return bpage->page->data + index;
  796. }
  797. static inline struct ring_buffer_event *
  798. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  799. {
  800. return __rb_page_index(cpu_buffer->reader_page,
  801. cpu_buffer->reader_page->read);
  802. }
  803. static inline struct ring_buffer_event *
  804. rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
  805. {
  806. return __rb_page_index(cpu_buffer->head_page,
  807. cpu_buffer->head_page->read);
  808. }
  809. static inline struct ring_buffer_event *
  810. rb_iter_head_event(struct ring_buffer_iter *iter)
  811. {
  812. return __rb_page_index(iter->head_page, iter->head);
  813. }
  814. static inline unsigned rb_page_write(struct buffer_page *bpage)
  815. {
  816. return local_read(&bpage->write);
  817. }
  818. static inline unsigned rb_page_commit(struct buffer_page *bpage)
  819. {
  820. return local_read(&bpage->page->commit);
  821. }
  822. /* Size is determined by what has been commited */
  823. static inline unsigned rb_page_size(struct buffer_page *bpage)
  824. {
  825. return rb_page_commit(bpage);
  826. }
  827. static inline unsigned
  828. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  829. {
  830. return rb_page_commit(cpu_buffer->commit_page);
  831. }
  832. static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
  833. {
  834. return rb_page_commit(cpu_buffer->head_page);
  835. }
  836. static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
  837. struct buffer_page **bpage)
  838. {
  839. struct list_head *p = (*bpage)->list.next;
  840. if (p == &cpu_buffer->pages)
  841. p = p->next;
  842. *bpage = list_entry(p, struct buffer_page, list);
  843. }
  844. static inline unsigned
  845. rb_event_index(struct ring_buffer_event *event)
  846. {
  847. unsigned long addr = (unsigned long)event;
  848. return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
  849. }
  850. static inline int
  851. rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
  852. struct ring_buffer_event *event)
  853. {
  854. unsigned long addr = (unsigned long)event;
  855. unsigned long index;
  856. index = rb_event_index(event);
  857. addr &= PAGE_MASK;
  858. return cpu_buffer->commit_page->page == (void *)addr &&
  859. rb_commit_index(cpu_buffer) == index;
  860. }
  861. static void
  862. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  863. {
  864. /*
  865. * We only race with interrupts and NMIs on this CPU.
  866. * If we own the commit event, then we can commit
  867. * all others that interrupted us, since the interruptions
  868. * are in stack format (they finish before they come
  869. * back to us). This allows us to do a simple loop to
  870. * assign the commit to the tail.
  871. */
  872. again:
  873. while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
  874. cpu_buffer->commit_page->page->commit =
  875. cpu_buffer->commit_page->write;
  876. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  877. cpu_buffer->write_stamp =
  878. cpu_buffer->commit_page->page->time_stamp;
  879. /* add barrier to keep gcc from optimizing too much */
  880. barrier();
  881. }
  882. while (rb_commit_index(cpu_buffer) !=
  883. rb_page_write(cpu_buffer->commit_page)) {
  884. cpu_buffer->commit_page->page->commit =
  885. cpu_buffer->commit_page->write;
  886. barrier();
  887. }
  888. /* again, keep gcc from optimizing */
  889. barrier();
  890. /*
  891. * If an interrupt came in just after the first while loop
  892. * and pushed the tail page forward, we will be left with
  893. * a dangling commit that will never go forward.
  894. */
  895. if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
  896. goto again;
  897. }
  898. static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  899. {
  900. cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
  901. cpu_buffer->reader_page->read = 0;
  902. }
  903. static void rb_inc_iter(struct ring_buffer_iter *iter)
  904. {
  905. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  906. /*
  907. * The iterator could be on the reader page (it starts there).
  908. * But the head could have moved, since the reader was
  909. * found. Check for this case and assign the iterator
  910. * to the head page instead of next.
  911. */
  912. if (iter->head_page == cpu_buffer->reader_page)
  913. iter->head_page = cpu_buffer->head_page;
  914. else
  915. rb_inc_page(cpu_buffer, &iter->head_page);
  916. iter->read_stamp = iter->head_page->page->time_stamp;
  917. iter->head = 0;
  918. }
  919. /**
  920. * ring_buffer_update_event - update event type and data
  921. * @event: the even to update
  922. * @type: the type of event
  923. * @length: the size of the event field in the ring buffer
  924. *
  925. * Update the type and data fields of the event. The length
  926. * is the actual size that is written to the ring buffer,
  927. * and with this, we can determine what to place into the
  928. * data field.
  929. */
  930. static void
  931. rb_update_event(struct ring_buffer_event *event,
  932. unsigned type, unsigned length)
  933. {
  934. event->type_len = type;
  935. switch (type) {
  936. case RINGBUF_TYPE_PADDING:
  937. case RINGBUF_TYPE_TIME_EXTEND:
  938. case RINGBUF_TYPE_TIME_STAMP:
  939. break;
  940. case 0:
  941. length -= RB_EVNT_HDR_SIZE;
  942. if (length > RB_MAX_SMALL_DATA)
  943. event->array[0] = length;
  944. else
  945. event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  946. break;
  947. default:
  948. BUG();
  949. }
  950. }
  951. static unsigned rb_calculate_event_length(unsigned length)
  952. {
  953. struct ring_buffer_event event; /* Used only for sizeof array */
  954. /* zero length can cause confusions */
  955. if (!length)
  956. length = 1;
  957. if (length > RB_MAX_SMALL_DATA)
  958. length += sizeof(event.array[0]);
  959. length += RB_EVNT_HDR_SIZE;
  960. length = ALIGN(length, RB_ALIGNMENT);
  961. return length;
  962. }
  963. static inline void
  964. rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
  965. struct buffer_page *tail_page,
  966. unsigned long tail, unsigned long length)
  967. {
  968. struct ring_buffer_event *event;
  969. /*
  970. * Only the event that crossed the page boundary
  971. * must fill the old tail_page with padding.
  972. */
  973. if (tail >= BUF_PAGE_SIZE) {
  974. local_sub(length, &tail_page->write);
  975. return;
  976. }
  977. event = __rb_page_index(tail_page, tail);
  978. /*
  979. * If this event is bigger than the minimum size, then
  980. * we need to be careful that we don't subtract the
  981. * write counter enough to allow another writer to slip
  982. * in on this page.
  983. * We put in a discarded commit instead, to make sure
  984. * that this space is not used again.
  985. *
  986. * If we are less than the minimum size, we don't need to
  987. * worry about it.
  988. */
  989. if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
  990. /* No room for any events */
  991. /* Mark the rest of the page with padding */
  992. rb_event_set_padding(event);
  993. /* Set the write back to the previous setting */
  994. local_sub(length, &tail_page->write);
  995. return;
  996. }
  997. /* Put in a discarded event */
  998. event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
  999. event->type_len = RINGBUF_TYPE_PADDING;
  1000. /* time delta must be non zero */
  1001. event->time_delta = 1;
  1002. /* Account for this as an entry */
  1003. local_inc(&tail_page->entries);
  1004. local_inc(&cpu_buffer->entries);
  1005. /* Set write to end of buffer */
  1006. length = (tail + length) - BUF_PAGE_SIZE;
  1007. local_sub(length, &tail_page->write);
  1008. }
  1009. static struct ring_buffer_event *
  1010. rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
  1011. unsigned long length, unsigned long tail,
  1012. struct buffer_page *commit_page,
  1013. struct buffer_page *tail_page, u64 *ts)
  1014. {
  1015. struct buffer_page *next_page, *head_page, *reader_page;
  1016. struct ring_buffer *buffer = cpu_buffer->buffer;
  1017. bool lock_taken = false;
  1018. unsigned long flags;
  1019. next_page = tail_page;
  1020. local_irq_save(flags);
  1021. /*
  1022. * Since the write to the buffer is still not
  1023. * fully lockless, we must be careful with NMIs.
  1024. * The locks in the writers are taken when a write
  1025. * crosses to a new page. The locks protect against
  1026. * races with the readers (this will soon be fixed
  1027. * with a lockless solution).
  1028. *
  1029. * Because we can not protect against NMIs, and we
  1030. * want to keep traces reentrant, we need to manage
  1031. * what happens when we are in an NMI.
  1032. *
  1033. * NMIs can happen after we take the lock.
  1034. * If we are in an NMI, only take the lock
  1035. * if it is not already taken. Otherwise
  1036. * simply fail.
  1037. */
  1038. if (unlikely(in_nmi())) {
  1039. if (!__raw_spin_trylock(&cpu_buffer->lock)) {
  1040. cpu_buffer->nmi_dropped++;
  1041. goto out_reset;
  1042. }
  1043. } else
  1044. __raw_spin_lock(&cpu_buffer->lock);
  1045. lock_taken = true;
  1046. rb_inc_page(cpu_buffer, &next_page);
  1047. head_page = cpu_buffer->head_page;
  1048. reader_page = cpu_buffer->reader_page;
  1049. /* we grabbed the lock before incrementing */
  1050. if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
  1051. goto out_reset;
  1052. /*
  1053. * If for some reason, we had an interrupt storm that made
  1054. * it all the way around the buffer, bail, and warn
  1055. * about it.
  1056. */
  1057. if (unlikely(next_page == commit_page)) {
  1058. cpu_buffer->commit_overrun++;
  1059. goto out_reset;
  1060. }
  1061. if (next_page == head_page) {
  1062. if (!(buffer->flags & RB_FL_OVERWRITE))
  1063. goto out_reset;
  1064. /* tail_page has not moved yet? */
  1065. if (tail_page == cpu_buffer->tail_page) {
  1066. /* count overflows */
  1067. cpu_buffer->overrun +=
  1068. local_read(&head_page->entries);
  1069. rb_inc_page(cpu_buffer, &head_page);
  1070. cpu_buffer->head_page = head_page;
  1071. cpu_buffer->head_page->read = 0;
  1072. }
  1073. }
  1074. /*
  1075. * If the tail page is still the same as what we think
  1076. * it is, then it is up to us to update the tail
  1077. * pointer.
  1078. */
  1079. if (tail_page == cpu_buffer->tail_page) {
  1080. local_set(&next_page->write, 0);
  1081. local_set(&next_page->entries, 0);
  1082. local_set(&next_page->page->commit, 0);
  1083. cpu_buffer->tail_page = next_page;
  1084. /* reread the time stamp */
  1085. *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
  1086. cpu_buffer->tail_page->page->time_stamp = *ts;
  1087. }
  1088. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1089. __raw_spin_unlock(&cpu_buffer->lock);
  1090. local_irq_restore(flags);
  1091. /* fail and let the caller try again */
  1092. return ERR_PTR(-EAGAIN);
  1093. out_reset:
  1094. /* reset write */
  1095. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1096. if (likely(lock_taken))
  1097. __raw_spin_unlock(&cpu_buffer->lock);
  1098. local_irq_restore(flags);
  1099. return NULL;
  1100. }
  1101. static struct ring_buffer_event *
  1102. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  1103. unsigned type, unsigned long length, u64 *ts)
  1104. {
  1105. struct buffer_page *tail_page, *commit_page;
  1106. struct ring_buffer_event *event;
  1107. unsigned long tail, write;
  1108. commit_page = cpu_buffer->commit_page;
  1109. /* we just need to protect against interrupts */
  1110. barrier();
  1111. tail_page = cpu_buffer->tail_page;
  1112. write = local_add_return(length, &tail_page->write);
  1113. tail = write - length;
  1114. /* See if we shot pass the end of this buffer page */
  1115. if (write > BUF_PAGE_SIZE)
  1116. return rb_move_tail(cpu_buffer, length, tail,
  1117. commit_page, tail_page, ts);
  1118. /* We reserved something on the buffer */
  1119. event = __rb_page_index(tail_page, tail);
  1120. rb_update_event(event, type, length);
  1121. /* The passed in type is zero for DATA */
  1122. if (likely(!type))
  1123. local_inc(&tail_page->entries);
  1124. /*
  1125. * If this is the first commit on the page, then update
  1126. * its timestamp.
  1127. */
  1128. if (!tail)
  1129. tail_page->page->time_stamp = *ts;
  1130. return event;
  1131. }
  1132. static inline int
  1133. rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
  1134. struct ring_buffer_event *event)
  1135. {
  1136. unsigned long new_index, old_index;
  1137. struct buffer_page *bpage;
  1138. unsigned long index;
  1139. unsigned long addr;
  1140. new_index = rb_event_index(event);
  1141. old_index = new_index + rb_event_length(event);
  1142. addr = (unsigned long)event;
  1143. addr &= PAGE_MASK;
  1144. bpage = cpu_buffer->tail_page;
  1145. if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
  1146. /*
  1147. * This is on the tail page. It is possible that
  1148. * a write could come in and move the tail page
  1149. * and write to the next page. That is fine
  1150. * because we just shorten what is on this page.
  1151. */
  1152. index = local_cmpxchg(&bpage->write, old_index, new_index);
  1153. if (index == old_index)
  1154. return 1;
  1155. }
  1156. /* could not discard */
  1157. return 0;
  1158. }
  1159. static int
  1160. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1161. u64 *ts, u64 *delta)
  1162. {
  1163. struct ring_buffer_event *event;
  1164. static int once;
  1165. int ret;
  1166. if (unlikely(*delta > (1ULL << 59) && !once++)) {
  1167. printk(KERN_WARNING "Delta way too big! %llu"
  1168. " ts=%llu write stamp = %llu\n",
  1169. (unsigned long long)*delta,
  1170. (unsigned long long)*ts,
  1171. (unsigned long long)cpu_buffer->write_stamp);
  1172. WARN_ON(1);
  1173. }
  1174. /*
  1175. * The delta is too big, we to add a
  1176. * new timestamp.
  1177. */
  1178. event = __rb_reserve_next(cpu_buffer,
  1179. RINGBUF_TYPE_TIME_EXTEND,
  1180. RB_LEN_TIME_EXTEND,
  1181. ts);
  1182. if (!event)
  1183. return -EBUSY;
  1184. if (PTR_ERR(event) == -EAGAIN)
  1185. return -EAGAIN;
  1186. /* Only a commited time event can update the write stamp */
  1187. if (rb_event_is_commit(cpu_buffer, event)) {
  1188. /*
  1189. * If this is the first on the page, then it was
  1190. * updated with the page itself. Try to discard it
  1191. * and if we can't just make it zero.
  1192. */
  1193. if (rb_event_index(event)) {
  1194. event->time_delta = *delta & TS_MASK;
  1195. event->array[0] = *delta >> TS_SHIFT;
  1196. } else {
  1197. /* try to discard, since we do not need this */
  1198. if (!rb_try_to_discard(cpu_buffer, event)) {
  1199. /* nope, just zero it */
  1200. event->time_delta = 0;
  1201. event->array[0] = 0;
  1202. }
  1203. }
  1204. cpu_buffer->write_stamp = *ts;
  1205. /* let the caller know this was the commit */
  1206. ret = 1;
  1207. } else {
  1208. /* Try to discard the event */
  1209. if (!rb_try_to_discard(cpu_buffer, event)) {
  1210. /* Darn, this is just wasted space */
  1211. event->time_delta = 0;
  1212. event->array[0] = 0;
  1213. }
  1214. ret = 0;
  1215. }
  1216. *delta = 0;
  1217. return ret;
  1218. }
  1219. static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1220. {
  1221. local_inc(&cpu_buffer->committing);
  1222. local_inc(&cpu_buffer->commits);
  1223. }
  1224. static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1225. {
  1226. unsigned long commits;
  1227. if (RB_WARN_ON(cpu_buffer,
  1228. !local_read(&cpu_buffer->committing)))
  1229. return;
  1230. again:
  1231. commits = local_read(&cpu_buffer->commits);
  1232. /* synchronize with interrupts */
  1233. barrier();
  1234. if (local_read(&cpu_buffer->committing) == 1)
  1235. rb_set_commit_to_write(cpu_buffer);
  1236. local_dec(&cpu_buffer->committing);
  1237. /* synchronize with interrupts */
  1238. barrier();
  1239. /*
  1240. * Need to account for interrupts coming in between the
  1241. * updating of the commit page and the clearing of the
  1242. * committing counter.
  1243. */
  1244. if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
  1245. !local_read(&cpu_buffer->committing)) {
  1246. local_inc(&cpu_buffer->committing);
  1247. goto again;
  1248. }
  1249. }
  1250. static struct ring_buffer_event *
  1251. rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
  1252. unsigned long length)
  1253. {
  1254. struct ring_buffer_event *event;
  1255. u64 ts, delta = 0;
  1256. int commit = 0;
  1257. int nr_loops = 0;
  1258. rb_start_commit(cpu_buffer);
  1259. length = rb_calculate_event_length(length);
  1260. again:
  1261. /*
  1262. * We allow for interrupts to reenter here and do a trace.
  1263. * If one does, it will cause this original code to loop
  1264. * back here. Even with heavy interrupts happening, this
  1265. * should only happen a few times in a row. If this happens
  1266. * 1000 times in a row, there must be either an interrupt
  1267. * storm or we have something buggy.
  1268. * Bail!
  1269. */
  1270. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  1271. goto out_fail;
  1272. ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
  1273. /*
  1274. * Only the first commit can update the timestamp.
  1275. * Yes there is a race here. If an interrupt comes in
  1276. * just after the conditional and it traces too, then it
  1277. * will also check the deltas. More than one timestamp may
  1278. * also be made. But only the entry that did the actual
  1279. * commit will be something other than zero.
  1280. */
  1281. if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
  1282. rb_page_write(cpu_buffer->tail_page) ==
  1283. rb_commit_index(cpu_buffer))) {
  1284. u64 diff;
  1285. diff = ts - cpu_buffer->write_stamp;
  1286. /* make sure this diff is calculated here */
  1287. barrier();
  1288. /* Did the write stamp get updated already? */
  1289. if (unlikely(ts < cpu_buffer->write_stamp))
  1290. goto get_event;
  1291. delta = diff;
  1292. if (unlikely(test_time_stamp(delta))) {
  1293. commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
  1294. if (commit == -EBUSY)
  1295. goto out_fail;
  1296. if (commit == -EAGAIN)
  1297. goto again;
  1298. RB_WARN_ON(cpu_buffer, commit < 0);
  1299. }
  1300. }
  1301. get_event:
  1302. event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
  1303. if (unlikely(PTR_ERR(event) == -EAGAIN))
  1304. goto again;
  1305. if (!event)
  1306. goto out_fail;
  1307. if (!rb_event_is_commit(cpu_buffer, event))
  1308. delta = 0;
  1309. event->time_delta = delta;
  1310. return event;
  1311. out_fail:
  1312. rb_end_commit(cpu_buffer);
  1313. return NULL;
  1314. }
  1315. #define TRACE_RECURSIVE_DEPTH 16
  1316. static int trace_recursive_lock(void)
  1317. {
  1318. current->trace_recursion++;
  1319. if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
  1320. return 0;
  1321. /* Disable all tracing before we do anything else */
  1322. tracing_off_permanent();
  1323. printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
  1324. "HC[%lu]:SC[%lu]:NMI[%lu]\n",
  1325. current->trace_recursion,
  1326. hardirq_count() >> HARDIRQ_SHIFT,
  1327. softirq_count() >> SOFTIRQ_SHIFT,
  1328. in_nmi());
  1329. WARN_ON_ONCE(1);
  1330. return -1;
  1331. }
  1332. static void trace_recursive_unlock(void)
  1333. {
  1334. WARN_ON_ONCE(!current->trace_recursion);
  1335. current->trace_recursion--;
  1336. }
  1337. static DEFINE_PER_CPU(int, rb_need_resched);
  1338. /**
  1339. * ring_buffer_lock_reserve - reserve a part of the buffer
  1340. * @buffer: the ring buffer to reserve from
  1341. * @length: the length of the data to reserve (excluding event header)
  1342. *
  1343. * Returns a reseverd event on the ring buffer to copy directly to.
  1344. * The user of this interface will need to get the body to write into
  1345. * and can use the ring_buffer_event_data() interface.
  1346. *
  1347. * The length is the length of the data needed, not the event length
  1348. * which also includes the event header.
  1349. *
  1350. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  1351. * If NULL is returned, then nothing has been allocated or locked.
  1352. */
  1353. struct ring_buffer_event *
  1354. ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
  1355. {
  1356. struct ring_buffer_per_cpu *cpu_buffer;
  1357. struct ring_buffer_event *event;
  1358. int cpu, resched;
  1359. if (ring_buffer_flags != RB_BUFFERS_ON)
  1360. return NULL;
  1361. if (atomic_read(&buffer->record_disabled))
  1362. return NULL;
  1363. /* If we are tracing schedule, we don't want to recurse */
  1364. resched = ftrace_preempt_disable();
  1365. if (trace_recursive_lock())
  1366. goto out_nocheck;
  1367. cpu = raw_smp_processor_id();
  1368. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1369. goto out;
  1370. cpu_buffer = buffer->buffers[cpu];
  1371. if (atomic_read(&cpu_buffer->record_disabled))
  1372. goto out;
  1373. if (length > BUF_MAX_DATA_SIZE)
  1374. goto out;
  1375. event = rb_reserve_next_event(cpu_buffer, length);
  1376. if (!event)
  1377. goto out;
  1378. /*
  1379. * Need to store resched state on this cpu.
  1380. * Only the first needs to.
  1381. */
  1382. if (preempt_count() == 1)
  1383. per_cpu(rb_need_resched, cpu) = resched;
  1384. return event;
  1385. out:
  1386. trace_recursive_unlock();
  1387. out_nocheck:
  1388. ftrace_preempt_enable(resched);
  1389. return NULL;
  1390. }
  1391. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  1392. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1393. struct ring_buffer_event *event)
  1394. {
  1395. local_inc(&cpu_buffer->entries);
  1396. /*
  1397. * The event first in the commit queue updates the
  1398. * time stamp.
  1399. */
  1400. if (rb_event_is_commit(cpu_buffer, event))
  1401. cpu_buffer->write_stamp += event->time_delta;
  1402. rb_end_commit(cpu_buffer);
  1403. }
  1404. /**
  1405. * ring_buffer_unlock_commit - commit a reserved
  1406. * @buffer: The buffer to commit to
  1407. * @event: The event pointer to commit.
  1408. *
  1409. * This commits the data to the ring buffer, and releases any locks held.
  1410. *
  1411. * Must be paired with ring_buffer_lock_reserve.
  1412. */
  1413. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  1414. struct ring_buffer_event *event)
  1415. {
  1416. struct ring_buffer_per_cpu *cpu_buffer;
  1417. int cpu = raw_smp_processor_id();
  1418. cpu_buffer = buffer->buffers[cpu];
  1419. rb_commit(cpu_buffer, event);
  1420. trace_recursive_unlock();
  1421. /*
  1422. * Only the last preempt count needs to restore preemption.
  1423. */
  1424. if (preempt_count() == 1)
  1425. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1426. else
  1427. preempt_enable_no_resched_notrace();
  1428. return 0;
  1429. }
  1430. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  1431. static inline void rb_event_discard(struct ring_buffer_event *event)
  1432. {
  1433. /* array[0] holds the actual length for the discarded event */
  1434. event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
  1435. event->type_len = RINGBUF_TYPE_PADDING;
  1436. /* time delta must be non zero */
  1437. if (!event->time_delta)
  1438. event->time_delta = 1;
  1439. }
  1440. /**
  1441. * ring_buffer_event_discard - discard any event in the ring buffer
  1442. * @event: the event to discard
  1443. *
  1444. * Sometimes a event that is in the ring buffer needs to be ignored.
  1445. * This function lets the user discard an event in the ring buffer
  1446. * and then that event will not be read later.
  1447. *
  1448. * Note, it is up to the user to be careful with this, and protect
  1449. * against races. If the user discards an event that has been consumed
  1450. * it is possible that it could corrupt the ring buffer.
  1451. */
  1452. void ring_buffer_event_discard(struct ring_buffer_event *event)
  1453. {
  1454. rb_event_discard(event);
  1455. }
  1456. EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
  1457. /**
  1458. * ring_buffer_commit_discard - discard an event that has not been committed
  1459. * @buffer: the ring buffer
  1460. * @event: non committed event to discard
  1461. *
  1462. * This is similar to ring_buffer_event_discard but must only be
  1463. * performed on an event that has not been committed yet. The difference
  1464. * is that this will also try to free the event from the ring buffer
  1465. * if another event has not been added behind it.
  1466. *
  1467. * If another event has been added behind it, it will set the event
  1468. * up as discarded, and perform the commit.
  1469. *
  1470. * If this function is called, do not call ring_buffer_unlock_commit on
  1471. * the event.
  1472. */
  1473. void ring_buffer_discard_commit(struct ring_buffer *buffer,
  1474. struct ring_buffer_event *event)
  1475. {
  1476. struct ring_buffer_per_cpu *cpu_buffer;
  1477. int cpu;
  1478. /* The event is discarded regardless */
  1479. rb_event_discard(event);
  1480. cpu = smp_processor_id();
  1481. cpu_buffer = buffer->buffers[cpu];
  1482. /*
  1483. * This must only be called if the event has not been
  1484. * committed yet. Thus we can assume that preemption
  1485. * is still disabled.
  1486. */
  1487. RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
  1488. if (!rb_try_to_discard(cpu_buffer, event))
  1489. goto out;
  1490. /*
  1491. * The commit is still visible by the reader, so we
  1492. * must increment entries.
  1493. */
  1494. local_inc(&cpu_buffer->entries);
  1495. out:
  1496. rb_end_commit(cpu_buffer);
  1497. trace_recursive_unlock();
  1498. /*
  1499. * Only the last preempt count needs to restore preemption.
  1500. */
  1501. if (preempt_count() == 1)
  1502. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1503. else
  1504. preempt_enable_no_resched_notrace();
  1505. }
  1506. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  1507. /**
  1508. * ring_buffer_write - write data to the buffer without reserving
  1509. * @buffer: The ring buffer to write to.
  1510. * @length: The length of the data being written (excluding the event header)
  1511. * @data: The data to write to the buffer.
  1512. *
  1513. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  1514. * one function. If you already have the data to write to the buffer, it
  1515. * may be easier to simply call this function.
  1516. *
  1517. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  1518. * and not the length of the event which would hold the header.
  1519. */
  1520. int ring_buffer_write(struct ring_buffer *buffer,
  1521. unsigned long length,
  1522. void *data)
  1523. {
  1524. struct ring_buffer_per_cpu *cpu_buffer;
  1525. struct ring_buffer_event *event;
  1526. void *body;
  1527. int ret = -EBUSY;
  1528. int cpu, resched;
  1529. if (ring_buffer_flags != RB_BUFFERS_ON)
  1530. return -EBUSY;
  1531. if (atomic_read(&buffer->record_disabled))
  1532. return -EBUSY;
  1533. resched = ftrace_preempt_disable();
  1534. cpu = raw_smp_processor_id();
  1535. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1536. goto out;
  1537. cpu_buffer = buffer->buffers[cpu];
  1538. if (atomic_read(&cpu_buffer->record_disabled))
  1539. goto out;
  1540. if (length > BUF_MAX_DATA_SIZE)
  1541. goto out;
  1542. event = rb_reserve_next_event(cpu_buffer, length);
  1543. if (!event)
  1544. goto out;
  1545. body = rb_event_data(event);
  1546. memcpy(body, data, length);
  1547. rb_commit(cpu_buffer, event);
  1548. ret = 0;
  1549. out:
  1550. ftrace_preempt_enable(resched);
  1551. return ret;
  1552. }
  1553. EXPORT_SYMBOL_GPL(ring_buffer_write);
  1554. static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  1555. {
  1556. struct buffer_page *reader = cpu_buffer->reader_page;
  1557. struct buffer_page *head = cpu_buffer->head_page;
  1558. struct buffer_page *commit = cpu_buffer->commit_page;
  1559. return reader->read == rb_page_commit(reader) &&
  1560. (commit == reader ||
  1561. (commit == head &&
  1562. head->read == rb_page_commit(commit)));
  1563. }
  1564. /**
  1565. * ring_buffer_record_disable - stop all writes into the buffer
  1566. * @buffer: The ring buffer to stop writes to.
  1567. *
  1568. * This prevents all writes to the buffer. Any attempt to write
  1569. * to the buffer after this will fail and return NULL.
  1570. *
  1571. * The caller should call synchronize_sched() after this.
  1572. */
  1573. void ring_buffer_record_disable(struct ring_buffer *buffer)
  1574. {
  1575. atomic_inc(&buffer->record_disabled);
  1576. }
  1577. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  1578. /**
  1579. * ring_buffer_record_enable - enable writes to the buffer
  1580. * @buffer: The ring buffer to enable writes
  1581. *
  1582. * Note, multiple disables will need the same number of enables
  1583. * to truely enable the writing (much like preempt_disable).
  1584. */
  1585. void ring_buffer_record_enable(struct ring_buffer *buffer)
  1586. {
  1587. atomic_dec(&buffer->record_disabled);
  1588. }
  1589. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  1590. /**
  1591. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  1592. * @buffer: The ring buffer to stop writes to.
  1593. * @cpu: The CPU buffer to stop
  1594. *
  1595. * This prevents all writes to the buffer. Any attempt to write
  1596. * to the buffer after this will fail and return NULL.
  1597. *
  1598. * The caller should call synchronize_sched() after this.
  1599. */
  1600. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
  1601. {
  1602. struct ring_buffer_per_cpu *cpu_buffer;
  1603. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1604. return;
  1605. cpu_buffer = buffer->buffers[cpu];
  1606. atomic_inc(&cpu_buffer->record_disabled);
  1607. }
  1608. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  1609. /**
  1610. * ring_buffer_record_enable_cpu - enable writes to the buffer
  1611. * @buffer: The ring buffer to enable writes
  1612. * @cpu: The CPU to enable.
  1613. *
  1614. * Note, multiple disables will need the same number of enables
  1615. * to truely enable the writing (much like preempt_disable).
  1616. */
  1617. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
  1618. {
  1619. struct ring_buffer_per_cpu *cpu_buffer;
  1620. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1621. return;
  1622. cpu_buffer = buffer->buffers[cpu];
  1623. atomic_dec(&cpu_buffer->record_disabled);
  1624. }
  1625. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  1626. /**
  1627. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  1628. * @buffer: The ring buffer
  1629. * @cpu: The per CPU buffer to get the entries from.
  1630. */
  1631. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
  1632. {
  1633. struct ring_buffer_per_cpu *cpu_buffer;
  1634. unsigned long ret;
  1635. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1636. return 0;
  1637. cpu_buffer = buffer->buffers[cpu];
  1638. ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
  1639. - cpu_buffer->read;
  1640. return ret;
  1641. }
  1642. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  1643. /**
  1644. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  1645. * @buffer: The ring buffer
  1646. * @cpu: The per CPU buffer to get the number of overruns from
  1647. */
  1648. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  1649. {
  1650. struct ring_buffer_per_cpu *cpu_buffer;
  1651. unsigned long ret;
  1652. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1653. return 0;
  1654. cpu_buffer = buffer->buffers[cpu];
  1655. ret = cpu_buffer->overrun;
  1656. return ret;
  1657. }
  1658. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  1659. /**
  1660. * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
  1661. * @buffer: The ring buffer
  1662. * @cpu: The per CPU buffer to get the number of overruns from
  1663. */
  1664. unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
  1665. {
  1666. struct ring_buffer_per_cpu *cpu_buffer;
  1667. unsigned long ret;
  1668. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1669. return 0;
  1670. cpu_buffer = buffer->buffers[cpu];
  1671. ret = cpu_buffer->nmi_dropped;
  1672. return ret;
  1673. }
  1674. EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
  1675. /**
  1676. * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
  1677. * @buffer: The ring buffer
  1678. * @cpu: The per CPU buffer to get the number of overruns from
  1679. */
  1680. unsigned long
  1681. ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
  1682. {
  1683. struct ring_buffer_per_cpu *cpu_buffer;
  1684. unsigned long ret;
  1685. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1686. return 0;
  1687. cpu_buffer = buffer->buffers[cpu];
  1688. ret = cpu_buffer->commit_overrun;
  1689. return ret;
  1690. }
  1691. EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
  1692. /**
  1693. * ring_buffer_entries - get the number of entries in a buffer
  1694. * @buffer: The ring buffer
  1695. *
  1696. * Returns the total number of entries in the ring buffer
  1697. * (all CPU entries)
  1698. */
  1699. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  1700. {
  1701. struct ring_buffer_per_cpu *cpu_buffer;
  1702. unsigned long entries = 0;
  1703. int cpu;
  1704. /* if you care about this being correct, lock the buffer */
  1705. for_each_buffer_cpu(buffer, cpu) {
  1706. cpu_buffer = buffer->buffers[cpu];
  1707. entries += (local_read(&cpu_buffer->entries) -
  1708. cpu_buffer->overrun) - cpu_buffer->read;
  1709. }
  1710. return entries;
  1711. }
  1712. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  1713. /**
  1714. * ring_buffer_overrun_cpu - get the number of overruns in buffer
  1715. * @buffer: The ring buffer
  1716. *
  1717. * Returns the total number of overruns in the ring buffer
  1718. * (all CPU entries)
  1719. */
  1720. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  1721. {
  1722. struct ring_buffer_per_cpu *cpu_buffer;
  1723. unsigned long overruns = 0;
  1724. int cpu;
  1725. /* if you care about this being correct, lock the buffer */
  1726. for_each_buffer_cpu(buffer, cpu) {
  1727. cpu_buffer = buffer->buffers[cpu];
  1728. overruns += cpu_buffer->overrun;
  1729. }
  1730. return overruns;
  1731. }
  1732. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  1733. static void rb_iter_reset(struct ring_buffer_iter *iter)
  1734. {
  1735. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1736. /* Iterator usage is expected to have record disabled */
  1737. if (list_empty(&cpu_buffer->reader_page->list)) {
  1738. iter->head_page = cpu_buffer->head_page;
  1739. iter->head = cpu_buffer->head_page->read;
  1740. } else {
  1741. iter->head_page = cpu_buffer->reader_page;
  1742. iter->head = cpu_buffer->reader_page->read;
  1743. }
  1744. if (iter->head)
  1745. iter->read_stamp = cpu_buffer->read_stamp;
  1746. else
  1747. iter->read_stamp = iter->head_page->page->time_stamp;
  1748. }
  1749. /**
  1750. * ring_buffer_iter_reset - reset an iterator
  1751. * @iter: The iterator to reset
  1752. *
  1753. * Resets the iterator, so that it will start from the beginning
  1754. * again.
  1755. */
  1756. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  1757. {
  1758. struct ring_buffer_per_cpu *cpu_buffer;
  1759. unsigned long flags;
  1760. if (!iter)
  1761. return;
  1762. cpu_buffer = iter->cpu_buffer;
  1763. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1764. rb_iter_reset(iter);
  1765. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1766. }
  1767. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  1768. /**
  1769. * ring_buffer_iter_empty - check if an iterator has no more to read
  1770. * @iter: The iterator to check
  1771. */
  1772. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  1773. {
  1774. struct ring_buffer_per_cpu *cpu_buffer;
  1775. cpu_buffer = iter->cpu_buffer;
  1776. return iter->head_page == cpu_buffer->commit_page &&
  1777. iter->head == rb_commit_index(cpu_buffer);
  1778. }
  1779. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  1780. static void
  1781. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1782. struct ring_buffer_event *event)
  1783. {
  1784. u64 delta;
  1785. switch (event->type_len) {
  1786. case RINGBUF_TYPE_PADDING:
  1787. return;
  1788. case RINGBUF_TYPE_TIME_EXTEND:
  1789. delta = event->array[0];
  1790. delta <<= TS_SHIFT;
  1791. delta += event->time_delta;
  1792. cpu_buffer->read_stamp += delta;
  1793. return;
  1794. case RINGBUF_TYPE_TIME_STAMP:
  1795. /* FIXME: not implemented */
  1796. return;
  1797. case RINGBUF_TYPE_DATA:
  1798. cpu_buffer->read_stamp += event->time_delta;
  1799. return;
  1800. default:
  1801. BUG();
  1802. }
  1803. return;
  1804. }
  1805. static void
  1806. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  1807. struct ring_buffer_event *event)
  1808. {
  1809. u64 delta;
  1810. switch (event->type_len) {
  1811. case RINGBUF_TYPE_PADDING:
  1812. return;
  1813. case RINGBUF_TYPE_TIME_EXTEND:
  1814. delta = event->array[0];
  1815. delta <<= TS_SHIFT;
  1816. delta += event->time_delta;
  1817. iter->read_stamp += delta;
  1818. return;
  1819. case RINGBUF_TYPE_TIME_STAMP:
  1820. /* FIXME: not implemented */
  1821. return;
  1822. case RINGBUF_TYPE_DATA:
  1823. iter->read_stamp += event->time_delta;
  1824. return;
  1825. default:
  1826. BUG();
  1827. }
  1828. return;
  1829. }
  1830. static struct buffer_page *
  1831. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  1832. {
  1833. struct buffer_page *reader = NULL;
  1834. unsigned long flags;
  1835. int nr_loops = 0;
  1836. local_irq_save(flags);
  1837. __raw_spin_lock(&cpu_buffer->lock);
  1838. again:
  1839. /*
  1840. * This should normally only loop twice. But because the
  1841. * start of the reader inserts an empty page, it causes
  1842. * a case where we will loop three times. There should be no
  1843. * reason to loop four times (that I know of).
  1844. */
  1845. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  1846. reader = NULL;
  1847. goto out;
  1848. }
  1849. reader = cpu_buffer->reader_page;
  1850. /* If there's more to read, return this page */
  1851. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  1852. goto out;
  1853. /* Never should we have an index greater than the size */
  1854. if (RB_WARN_ON(cpu_buffer,
  1855. cpu_buffer->reader_page->read > rb_page_size(reader)))
  1856. goto out;
  1857. /* check if we caught up to the tail */
  1858. reader = NULL;
  1859. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  1860. goto out;
  1861. /*
  1862. * Splice the empty reader page into the list around the head.
  1863. * Reset the reader page to size zero.
  1864. */
  1865. reader = cpu_buffer->head_page;
  1866. cpu_buffer->reader_page->list.next = reader->list.next;
  1867. cpu_buffer->reader_page->list.prev = reader->list.prev;
  1868. local_set(&cpu_buffer->reader_page->write, 0);
  1869. local_set(&cpu_buffer->reader_page->entries, 0);
  1870. local_set(&cpu_buffer->reader_page->page->commit, 0);
  1871. /* Make the reader page now replace the head */
  1872. reader->list.prev->next = &cpu_buffer->reader_page->list;
  1873. reader->list.next->prev = &cpu_buffer->reader_page->list;
  1874. /*
  1875. * If the tail is on the reader, then we must set the head
  1876. * to the inserted page, otherwise we set it one before.
  1877. */
  1878. cpu_buffer->head_page = cpu_buffer->reader_page;
  1879. if (cpu_buffer->commit_page != reader)
  1880. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  1881. /* Finally update the reader page to the new head */
  1882. cpu_buffer->reader_page = reader;
  1883. rb_reset_reader_page(cpu_buffer);
  1884. goto again;
  1885. out:
  1886. __raw_spin_unlock(&cpu_buffer->lock);
  1887. local_irq_restore(flags);
  1888. return reader;
  1889. }
  1890. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  1891. {
  1892. struct ring_buffer_event *event;
  1893. struct buffer_page *reader;
  1894. unsigned length;
  1895. reader = rb_get_reader_page(cpu_buffer);
  1896. /* This function should not be called when buffer is empty */
  1897. if (RB_WARN_ON(cpu_buffer, !reader))
  1898. return;
  1899. event = rb_reader_event(cpu_buffer);
  1900. if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
  1901. || rb_discarded_event(event))
  1902. cpu_buffer->read++;
  1903. rb_update_read_stamp(cpu_buffer, event);
  1904. length = rb_event_length(event);
  1905. cpu_buffer->reader_page->read += length;
  1906. }
  1907. static void rb_advance_iter(struct ring_buffer_iter *iter)
  1908. {
  1909. struct ring_buffer *buffer;
  1910. struct ring_buffer_per_cpu *cpu_buffer;
  1911. struct ring_buffer_event *event;
  1912. unsigned length;
  1913. cpu_buffer = iter->cpu_buffer;
  1914. buffer = cpu_buffer->buffer;
  1915. /*
  1916. * Check if we are at the end of the buffer.
  1917. */
  1918. if (iter->head >= rb_page_size(iter->head_page)) {
  1919. /* discarded commits can make the page empty */
  1920. if (iter->head_page == cpu_buffer->commit_page)
  1921. return;
  1922. rb_inc_iter(iter);
  1923. return;
  1924. }
  1925. event = rb_iter_head_event(iter);
  1926. length = rb_event_length(event);
  1927. /*
  1928. * This should not be called to advance the header if we are
  1929. * at the tail of the buffer.
  1930. */
  1931. if (RB_WARN_ON(cpu_buffer,
  1932. (iter->head_page == cpu_buffer->commit_page) &&
  1933. (iter->head + length > rb_commit_index(cpu_buffer))))
  1934. return;
  1935. rb_update_iter_read_stamp(iter, event);
  1936. iter->head += length;
  1937. /* check for end of page padding */
  1938. if ((iter->head >= rb_page_size(iter->head_page)) &&
  1939. (iter->head_page != cpu_buffer->commit_page))
  1940. rb_advance_iter(iter);
  1941. }
  1942. static struct ring_buffer_event *
  1943. rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  1944. {
  1945. struct ring_buffer_per_cpu *cpu_buffer;
  1946. struct ring_buffer_event *event;
  1947. struct buffer_page *reader;
  1948. int nr_loops = 0;
  1949. cpu_buffer = buffer->buffers[cpu];
  1950. again:
  1951. /*
  1952. * We repeat when a timestamp is encountered. It is possible
  1953. * to get multiple timestamps from an interrupt entering just
  1954. * as one timestamp is about to be written, or from discarded
  1955. * commits. The most that we can have is the number on a single page.
  1956. */
  1957. if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
  1958. return NULL;
  1959. reader = rb_get_reader_page(cpu_buffer);
  1960. if (!reader)
  1961. return NULL;
  1962. event = rb_reader_event(cpu_buffer);
  1963. switch (event->type_len) {
  1964. case RINGBUF_TYPE_PADDING:
  1965. if (rb_null_event(event))
  1966. RB_WARN_ON(cpu_buffer, 1);
  1967. /*
  1968. * Because the writer could be discarding every
  1969. * event it creates (which would probably be bad)
  1970. * if we were to go back to "again" then we may never
  1971. * catch up, and will trigger the warn on, or lock
  1972. * the box. Return the padding, and we will release
  1973. * the current locks, and try again.
  1974. */
  1975. rb_advance_reader(cpu_buffer);
  1976. return event;
  1977. case RINGBUF_TYPE_TIME_EXTEND:
  1978. /* Internal data, OK to advance */
  1979. rb_advance_reader(cpu_buffer);
  1980. goto again;
  1981. case RINGBUF_TYPE_TIME_STAMP:
  1982. /* FIXME: not implemented */
  1983. rb_advance_reader(cpu_buffer);
  1984. goto again;
  1985. case RINGBUF_TYPE_DATA:
  1986. if (ts) {
  1987. *ts = cpu_buffer->read_stamp + event->time_delta;
  1988. ring_buffer_normalize_time_stamp(buffer,
  1989. cpu_buffer->cpu, ts);
  1990. }
  1991. return event;
  1992. default:
  1993. BUG();
  1994. }
  1995. return NULL;
  1996. }
  1997. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  1998. static struct ring_buffer_event *
  1999. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2000. {
  2001. struct ring_buffer *buffer;
  2002. struct ring_buffer_per_cpu *cpu_buffer;
  2003. struct ring_buffer_event *event;
  2004. int nr_loops = 0;
  2005. if (ring_buffer_iter_empty(iter))
  2006. return NULL;
  2007. cpu_buffer = iter->cpu_buffer;
  2008. buffer = cpu_buffer->buffer;
  2009. again:
  2010. /*
  2011. * We repeat when a timestamp is encountered.
  2012. * We can get multiple timestamps by nested interrupts or also
  2013. * if filtering is on (discarding commits). Since discarding
  2014. * commits can be frequent we can get a lot of timestamps.
  2015. * But we limit them by not adding timestamps if they begin
  2016. * at the start of a page.
  2017. */
  2018. if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
  2019. return NULL;
  2020. if (rb_per_cpu_empty(cpu_buffer))
  2021. return NULL;
  2022. event = rb_iter_head_event(iter);
  2023. switch (event->type_len) {
  2024. case RINGBUF_TYPE_PADDING:
  2025. if (rb_null_event(event)) {
  2026. rb_inc_iter(iter);
  2027. goto again;
  2028. }
  2029. rb_advance_iter(iter);
  2030. return event;
  2031. case RINGBUF_TYPE_TIME_EXTEND:
  2032. /* Internal data, OK to advance */
  2033. rb_advance_iter(iter);
  2034. goto again;
  2035. case RINGBUF_TYPE_TIME_STAMP:
  2036. /* FIXME: not implemented */
  2037. rb_advance_iter(iter);
  2038. goto again;
  2039. case RINGBUF_TYPE_DATA:
  2040. if (ts) {
  2041. *ts = iter->read_stamp + event->time_delta;
  2042. ring_buffer_normalize_time_stamp(buffer,
  2043. cpu_buffer->cpu, ts);
  2044. }
  2045. return event;
  2046. default:
  2047. BUG();
  2048. }
  2049. return NULL;
  2050. }
  2051. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  2052. static inline int rb_ok_to_lock(void)
  2053. {
  2054. /*
  2055. * If an NMI die dumps out the content of the ring buffer
  2056. * do not grab locks. We also permanently disable the ring
  2057. * buffer too. A one time deal is all you get from reading
  2058. * the ring buffer from an NMI.
  2059. */
  2060. if (likely(!in_nmi() && !oops_in_progress))
  2061. return 1;
  2062. tracing_off_permanent();
  2063. return 0;
  2064. }
  2065. /**
  2066. * ring_buffer_peek - peek at the next event to be read
  2067. * @buffer: The ring buffer to read
  2068. * @cpu: The cpu to peak at
  2069. * @ts: The timestamp counter of this event.
  2070. *
  2071. * This will return the event that will be read next, but does
  2072. * not consume the data.
  2073. */
  2074. struct ring_buffer_event *
  2075. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  2076. {
  2077. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2078. struct ring_buffer_event *event;
  2079. unsigned long flags;
  2080. int dolock;
  2081. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2082. return NULL;
  2083. dolock = rb_ok_to_lock();
  2084. again:
  2085. local_irq_save(flags);
  2086. if (dolock)
  2087. spin_lock(&cpu_buffer->reader_lock);
  2088. event = rb_buffer_peek(buffer, cpu, ts);
  2089. if (dolock)
  2090. spin_unlock(&cpu_buffer->reader_lock);
  2091. local_irq_restore(flags);
  2092. if (event && event->type_len == RINGBUF_TYPE_PADDING) {
  2093. cpu_relax();
  2094. goto again;
  2095. }
  2096. return event;
  2097. }
  2098. /**
  2099. * ring_buffer_iter_peek - peek at the next event to be read
  2100. * @iter: The ring buffer iterator
  2101. * @ts: The timestamp counter of this event.
  2102. *
  2103. * This will return the event that will be read next, but does
  2104. * not increment the iterator.
  2105. */
  2106. struct ring_buffer_event *
  2107. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2108. {
  2109. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2110. struct ring_buffer_event *event;
  2111. unsigned long flags;
  2112. again:
  2113. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2114. event = rb_iter_peek(iter, ts);
  2115. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2116. if (event && event->type_len == RINGBUF_TYPE_PADDING) {
  2117. cpu_relax();
  2118. goto again;
  2119. }
  2120. return event;
  2121. }
  2122. /**
  2123. * ring_buffer_consume - return an event and consume it
  2124. * @buffer: The ring buffer to get the next event from
  2125. *
  2126. * Returns the next event in the ring buffer, and that event is consumed.
  2127. * Meaning, that sequential reads will keep returning a different event,
  2128. * and eventually empty the ring buffer if the producer is slower.
  2129. */
  2130. struct ring_buffer_event *
  2131. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
  2132. {
  2133. struct ring_buffer_per_cpu *cpu_buffer;
  2134. struct ring_buffer_event *event = NULL;
  2135. unsigned long flags;
  2136. int dolock;
  2137. dolock = rb_ok_to_lock();
  2138. again:
  2139. /* might be called in atomic */
  2140. preempt_disable();
  2141. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2142. goto out;
  2143. cpu_buffer = buffer->buffers[cpu];
  2144. local_irq_save(flags);
  2145. if (dolock)
  2146. spin_lock(&cpu_buffer->reader_lock);
  2147. event = rb_buffer_peek(buffer, cpu, ts);
  2148. if (!event)
  2149. goto out_unlock;
  2150. rb_advance_reader(cpu_buffer);
  2151. out_unlock:
  2152. if (dolock)
  2153. spin_unlock(&cpu_buffer->reader_lock);
  2154. local_irq_restore(flags);
  2155. out:
  2156. preempt_enable();
  2157. if (event && event->type_len == RINGBUF_TYPE_PADDING) {
  2158. cpu_relax();
  2159. goto again;
  2160. }
  2161. return event;
  2162. }
  2163. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  2164. /**
  2165. * ring_buffer_read_start - start a non consuming read of the buffer
  2166. * @buffer: The ring buffer to read from
  2167. * @cpu: The cpu buffer to iterate over
  2168. *
  2169. * This starts up an iteration through the buffer. It also disables
  2170. * the recording to the buffer until the reading is finished.
  2171. * This prevents the reading from being corrupted. This is not
  2172. * a consuming read, so a producer is not expected.
  2173. *
  2174. * Must be paired with ring_buffer_finish.
  2175. */
  2176. struct ring_buffer_iter *
  2177. ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
  2178. {
  2179. struct ring_buffer_per_cpu *cpu_buffer;
  2180. struct ring_buffer_iter *iter;
  2181. unsigned long flags;
  2182. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2183. return NULL;
  2184. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2185. if (!iter)
  2186. return NULL;
  2187. cpu_buffer = buffer->buffers[cpu];
  2188. iter->cpu_buffer = cpu_buffer;
  2189. atomic_inc(&cpu_buffer->record_disabled);
  2190. synchronize_sched();
  2191. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2192. __raw_spin_lock(&cpu_buffer->lock);
  2193. rb_iter_reset(iter);
  2194. __raw_spin_unlock(&cpu_buffer->lock);
  2195. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2196. return iter;
  2197. }
  2198. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  2199. /**
  2200. * ring_buffer_finish - finish reading the iterator of the buffer
  2201. * @iter: The iterator retrieved by ring_buffer_start
  2202. *
  2203. * This re-enables the recording to the buffer, and frees the
  2204. * iterator.
  2205. */
  2206. void
  2207. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  2208. {
  2209. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2210. atomic_dec(&cpu_buffer->record_disabled);
  2211. kfree(iter);
  2212. }
  2213. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  2214. /**
  2215. * ring_buffer_read - read the next item in the ring buffer by the iterator
  2216. * @iter: The ring buffer iterator
  2217. * @ts: The time stamp of the event read.
  2218. *
  2219. * This reads the next event in the ring buffer and increments the iterator.
  2220. */
  2221. struct ring_buffer_event *
  2222. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  2223. {
  2224. struct ring_buffer_event *event;
  2225. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2226. unsigned long flags;
  2227. again:
  2228. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2229. event = rb_iter_peek(iter, ts);
  2230. if (!event)
  2231. goto out;
  2232. rb_advance_iter(iter);
  2233. out:
  2234. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2235. if (event && event->type_len == RINGBUF_TYPE_PADDING) {
  2236. cpu_relax();
  2237. goto again;
  2238. }
  2239. return event;
  2240. }
  2241. EXPORT_SYMBOL_GPL(ring_buffer_read);
  2242. /**
  2243. * ring_buffer_size - return the size of the ring buffer (in bytes)
  2244. * @buffer: The ring buffer.
  2245. */
  2246. unsigned long ring_buffer_size(struct ring_buffer *buffer)
  2247. {
  2248. return BUF_PAGE_SIZE * buffer->pages;
  2249. }
  2250. EXPORT_SYMBOL_GPL(ring_buffer_size);
  2251. static void
  2252. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  2253. {
  2254. cpu_buffer->head_page
  2255. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  2256. local_set(&cpu_buffer->head_page->write, 0);
  2257. local_set(&cpu_buffer->head_page->entries, 0);
  2258. local_set(&cpu_buffer->head_page->page->commit, 0);
  2259. cpu_buffer->head_page->read = 0;
  2260. cpu_buffer->tail_page = cpu_buffer->head_page;
  2261. cpu_buffer->commit_page = cpu_buffer->head_page;
  2262. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2263. local_set(&cpu_buffer->reader_page->write, 0);
  2264. local_set(&cpu_buffer->reader_page->entries, 0);
  2265. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2266. cpu_buffer->reader_page->read = 0;
  2267. cpu_buffer->nmi_dropped = 0;
  2268. cpu_buffer->commit_overrun = 0;
  2269. cpu_buffer->overrun = 0;
  2270. cpu_buffer->read = 0;
  2271. local_set(&cpu_buffer->entries, 0);
  2272. local_set(&cpu_buffer->committing, 0);
  2273. local_set(&cpu_buffer->commits, 0);
  2274. cpu_buffer->write_stamp = 0;
  2275. cpu_buffer->read_stamp = 0;
  2276. }
  2277. /**
  2278. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  2279. * @buffer: The ring buffer to reset a per cpu buffer of
  2280. * @cpu: The CPU buffer to be reset
  2281. */
  2282. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  2283. {
  2284. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2285. unsigned long flags;
  2286. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2287. return;
  2288. atomic_inc(&cpu_buffer->record_disabled);
  2289. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2290. __raw_spin_lock(&cpu_buffer->lock);
  2291. rb_reset_cpu(cpu_buffer);
  2292. __raw_spin_unlock(&cpu_buffer->lock);
  2293. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2294. atomic_dec(&cpu_buffer->record_disabled);
  2295. }
  2296. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  2297. /**
  2298. * ring_buffer_reset - reset a ring buffer
  2299. * @buffer: The ring buffer to reset all cpu buffers
  2300. */
  2301. void ring_buffer_reset(struct ring_buffer *buffer)
  2302. {
  2303. int cpu;
  2304. for_each_buffer_cpu(buffer, cpu)
  2305. ring_buffer_reset_cpu(buffer, cpu);
  2306. }
  2307. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  2308. /**
  2309. * rind_buffer_empty - is the ring buffer empty?
  2310. * @buffer: The ring buffer to test
  2311. */
  2312. int ring_buffer_empty(struct ring_buffer *buffer)
  2313. {
  2314. struct ring_buffer_per_cpu *cpu_buffer;
  2315. unsigned long flags;
  2316. int dolock;
  2317. int cpu;
  2318. int ret;
  2319. dolock = rb_ok_to_lock();
  2320. /* yes this is racy, but if you don't like the race, lock the buffer */
  2321. for_each_buffer_cpu(buffer, cpu) {
  2322. cpu_buffer = buffer->buffers[cpu];
  2323. local_irq_save(flags);
  2324. if (dolock)
  2325. spin_lock(&cpu_buffer->reader_lock);
  2326. ret = rb_per_cpu_empty(cpu_buffer);
  2327. if (dolock)
  2328. spin_unlock(&cpu_buffer->reader_lock);
  2329. local_irq_restore(flags);
  2330. if (!ret)
  2331. return 0;
  2332. }
  2333. return 1;
  2334. }
  2335. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  2336. /**
  2337. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  2338. * @buffer: The ring buffer
  2339. * @cpu: The CPU buffer to test
  2340. */
  2341. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  2342. {
  2343. struct ring_buffer_per_cpu *cpu_buffer;
  2344. unsigned long flags;
  2345. int dolock;
  2346. int ret;
  2347. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2348. return 1;
  2349. dolock = rb_ok_to_lock();
  2350. cpu_buffer = buffer->buffers[cpu];
  2351. local_irq_save(flags);
  2352. if (dolock)
  2353. spin_lock(&cpu_buffer->reader_lock);
  2354. ret = rb_per_cpu_empty(cpu_buffer);
  2355. if (dolock)
  2356. spin_unlock(&cpu_buffer->reader_lock);
  2357. local_irq_restore(flags);
  2358. return ret;
  2359. }
  2360. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  2361. /**
  2362. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  2363. * @buffer_a: One buffer to swap with
  2364. * @buffer_b: The other buffer to swap with
  2365. *
  2366. * This function is useful for tracers that want to take a "snapshot"
  2367. * of a CPU buffer and has another back up buffer lying around.
  2368. * it is expected that the tracer handles the cpu buffer not being
  2369. * used at the moment.
  2370. */
  2371. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  2372. struct ring_buffer *buffer_b, int cpu)
  2373. {
  2374. struct ring_buffer_per_cpu *cpu_buffer_a;
  2375. struct ring_buffer_per_cpu *cpu_buffer_b;
  2376. int ret = -EINVAL;
  2377. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  2378. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  2379. goto out;
  2380. /* At least make sure the two buffers are somewhat the same */
  2381. if (buffer_a->pages != buffer_b->pages)
  2382. goto out;
  2383. ret = -EAGAIN;
  2384. if (ring_buffer_flags != RB_BUFFERS_ON)
  2385. goto out;
  2386. if (atomic_read(&buffer_a->record_disabled))
  2387. goto out;
  2388. if (atomic_read(&buffer_b->record_disabled))
  2389. goto out;
  2390. cpu_buffer_a = buffer_a->buffers[cpu];
  2391. cpu_buffer_b = buffer_b->buffers[cpu];
  2392. if (atomic_read(&cpu_buffer_a->record_disabled))
  2393. goto out;
  2394. if (atomic_read(&cpu_buffer_b->record_disabled))
  2395. goto out;
  2396. /*
  2397. * We can't do a synchronize_sched here because this
  2398. * function can be called in atomic context.
  2399. * Normally this will be called from the same CPU as cpu.
  2400. * If not it's up to the caller to protect this.
  2401. */
  2402. atomic_inc(&cpu_buffer_a->record_disabled);
  2403. atomic_inc(&cpu_buffer_b->record_disabled);
  2404. buffer_a->buffers[cpu] = cpu_buffer_b;
  2405. buffer_b->buffers[cpu] = cpu_buffer_a;
  2406. cpu_buffer_b->buffer = buffer_a;
  2407. cpu_buffer_a->buffer = buffer_b;
  2408. atomic_dec(&cpu_buffer_a->record_disabled);
  2409. atomic_dec(&cpu_buffer_b->record_disabled);
  2410. ret = 0;
  2411. out:
  2412. return ret;
  2413. }
  2414. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  2415. /**
  2416. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  2417. * @buffer: the buffer to allocate for.
  2418. *
  2419. * This function is used in conjunction with ring_buffer_read_page.
  2420. * When reading a full page from the ring buffer, these functions
  2421. * can be used to speed up the process. The calling function should
  2422. * allocate a few pages first with this function. Then when it
  2423. * needs to get pages from the ring buffer, it passes the result
  2424. * of this function into ring_buffer_read_page, which will swap
  2425. * the page that was allocated, with the read page of the buffer.
  2426. *
  2427. * Returns:
  2428. * The page allocated, or NULL on error.
  2429. */
  2430. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
  2431. {
  2432. struct buffer_data_page *bpage;
  2433. unsigned long addr;
  2434. addr = __get_free_page(GFP_KERNEL);
  2435. if (!addr)
  2436. return NULL;
  2437. bpage = (void *)addr;
  2438. rb_init_page(bpage);
  2439. return bpage;
  2440. }
  2441. EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  2442. /**
  2443. * ring_buffer_free_read_page - free an allocated read page
  2444. * @buffer: the buffer the page was allocate for
  2445. * @data: the page to free
  2446. *
  2447. * Free a page allocated from ring_buffer_alloc_read_page.
  2448. */
  2449. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  2450. {
  2451. free_page((unsigned long)data);
  2452. }
  2453. EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
  2454. /**
  2455. * ring_buffer_read_page - extract a page from the ring buffer
  2456. * @buffer: buffer to extract from
  2457. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  2458. * @len: amount to extract
  2459. * @cpu: the cpu of the buffer to extract
  2460. * @full: should the extraction only happen when the page is full.
  2461. *
  2462. * This function will pull out a page from the ring buffer and consume it.
  2463. * @data_page must be the address of the variable that was returned
  2464. * from ring_buffer_alloc_read_page. This is because the page might be used
  2465. * to swap with a page in the ring buffer.
  2466. *
  2467. * for example:
  2468. * rpage = ring_buffer_alloc_read_page(buffer);
  2469. * if (!rpage)
  2470. * return error;
  2471. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  2472. * if (ret >= 0)
  2473. * process_page(rpage, ret);
  2474. *
  2475. * When @full is set, the function will not return true unless
  2476. * the writer is off the reader page.
  2477. *
  2478. * Note: it is up to the calling functions to handle sleeps and wakeups.
  2479. * The ring buffer can be used anywhere in the kernel and can not
  2480. * blindly call wake_up. The layer that uses the ring buffer must be
  2481. * responsible for that.
  2482. *
  2483. * Returns:
  2484. * >=0 if data has been transferred, returns the offset of consumed data.
  2485. * <0 if no data has been transferred.
  2486. */
  2487. int ring_buffer_read_page(struct ring_buffer *buffer,
  2488. void **data_page, size_t len, int cpu, int full)
  2489. {
  2490. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2491. struct ring_buffer_event *event;
  2492. struct buffer_data_page *bpage;
  2493. struct buffer_page *reader;
  2494. unsigned long flags;
  2495. unsigned int commit;
  2496. unsigned int read;
  2497. u64 save_timestamp;
  2498. int ret = -1;
  2499. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2500. goto out;
  2501. /*
  2502. * If len is not big enough to hold the page header, then
  2503. * we can not copy anything.
  2504. */
  2505. if (len <= BUF_PAGE_HDR_SIZE)
  2506. goto out;
  2507. len -= BUF_PAGE_HDR_SIZE;
  2508. if (!data_page)
  2509. goto out;
  2510. bpage = *data_page;
  2511. if (!bpage)
  2512. goto out;
  2513. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2514. reader = rb_get_reader_page(cpu_buffer);
  2515. if (!reader)
  2516. goto out_unlock;
  2517. event = rb_reader_event(cpu_buffer);
  2518. read = reader->read;
  2519. commit = rb_page_commit(reader);
  2520. /*
  2521. * If this page has been partially read or
  2522. * if len is not big enough to read the rest of the page or
  2523. * a writer is still on the page, then
  2524. * we must copy the data from the page to the buffer.
  2525. * Otherwise, we can simply swap the page with the one passed in.
  2526. */
  2527. if (read || (len < (commit - read)) ||
  2528. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  2529. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  2530. unsigned int rpos = read;
  2531. unsigned int pos = 0;
  2532. unsigned int size;
  2533. if (full)
  2534. goto out_unlock;
  2535. if (len > (commit - read))
  2536. len = (commit - read);
  2537. size = rb_event_length(event);
  2538. if (len < size)
  2539. goto out_unlock;
  2540. /* save the current timestamp, since the user will need it */
  2541. save_timestamp = cpu_buffer->read_stamp;
  2542. /* Need to copy one event at a time */
  2543. do {
  2544. memcpy(bpage->data + pos, rpage->data + rpos, size);
  2545. len -= size;
  2546. rb_advance_reader(cpu_buffer);
  2547. rpos = reader->read;
  2548. pos += size;
  2549. event = rb_reader_event(cpu_buffer);
  2550. size = rb_event_length(event);
  2551. } while (len > size);
  2552. /* update bpage */
  2553. local_set(&bpage->commit, pos);
  2554. bpage->time_stamp = save_timestamp;
  2555. /* we copied everything to the beginning */
  2556. read = 0;
  2557. } else {
  2558. /* update the entry counter */
  2559. cpu_buffer->read += local_read(&reader->entries);
  2560. /* swap the pages */
  2561. rb_init_page(bpage);
  2562. bpage = reader->page;
  2563. reader->page = *data_page;
  2564. local_set(&reader->write, 0);
  2565. local_set(&reader->entries, 0);
  2566. reader->read = 0;
  2567. *data_page = bpage;
  2568. }
  2569. ret = read;
  2570. out_unlock:
  2571. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2572. out:
  2573. return ret;
  2574. }
  2575. EXPORT_SYMBOL_GPL(ring_buffer_read_page);
  2576. static ssize_t
  2577. rb_simple_read(struct file *filp, char __user *ubuf,
  2578. size_t cnt, loff_t *ppos)
  2579. {
  2580. unsigned long *p = filp->private_data;
  2581. char buf[64];
  2582. int r;
  2583. if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
  2584. r = sprintf(buf, "permanently disabled\n");
  2585. else
  2586. r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
  2587. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2588. }
  2589. static ssize_t
  2590. rb_simple_write(struct file *filp, const char __user *ubuf,
  2591. size_t cnt, loff_t *ppos)
  2592. {
  2593. unsigned long *p = filp->private_data;
  2594. char buf[64];
  2595. unsigned long val;
  2596. int ret;
  2597. if (cnt >= sizeof(buf))
  2598. return -EINVAL;
  2599. if (copy_from_user(&buf, ubuf, cnt))
  2600. return -EFAULT;
  2601. buf[cnt] = 0;
  2602. ret = strict_strtoul(buf, 10, &val);
  2603. if (ret < 0)
  2604. return ret;
  2605. if (val)
  2606. set_bit(RB_BUFFERS_ON_BIT, p);
  2607. else
  2608. clear_bit(RB_BUFFERS_ON_BIT, p);
  2609. (*ppos)++;
  2610. return cnt;
  2611. }
  2612. static const struct file_operations rb_simple_fops = {
  2613. .open = tracing_open_generic,
  2614. .read = rb_simple_read,
  2615. .write = rb_simple_write,
  2616. };
  2617. static __init int rb_init_debugfs(void)
  2618. {
  2619. struct dentry *d_tracer;
  2620. d_tracer = tracing_init_dentry();
  2621. trace_create_file("tracing_on", 0644, d_tracer,
  2622. &ring_buffer_flags, &rb_simple_fops);
  2623. return 0;
  2624. }
  2625. fs_initcall(rb_init_debugfs);
  2626. #ifdef CONFIG_HOTPLUG_CPU
  2627. static int rb_cpu_notify(struct notifier_block *self,
  2628. unsigned long action, void *hcpu)
  2629. {
  2630. struct ring_buffer *buffer =
  2631. container_of(self, struct ring_buffer, cpu_notify);
  2632. long cpu = (long)hcpu;
  2633. switch (action) {
  2634. case CPU_UP_PREPARE:
  2635. case CPU_UP_PREPARE_FROZEN:
  2636. if (cpumask_test_cpu(cpu, buffer->cpumask))
  2637. return NOTIFY_OK;
  2638. buffer->buffers[cpu] =
  2639. rb_allocate_cpu_buffer(buffer, cpu);
  2640. if (!buffer->buffers[cpu]) {
  2641. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  2642. cpu);
  2643. return NOTIFY_OK;
  2644. }
  2645. smp_wmb();
  2646. cpumask_set_cpu(cpu, buffer->cpumask);
  2647. break;
  2648. case CPU_DOWN_PREPARE:
  2649. case CPU_DOWN_PREPARE_FROZEN:
  2650. /*
  2651. * Do nothing.
  2652. * If we were to free the buffer, then the user would
  2653. * lose any trace that was in the buffer.
  2654. */
  2655. break;
  2656. default:
  2657. break;
  2658. }
  2659. return NOTIFY_OK;
  2660. }
  2661. #endif