ring_buffer.c 77 KB

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