ring_buffer.c 79 KB

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