ring_buffer.c 76 KB

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