ring_buffer.c 77 KB

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