ring_buffer.c 77 KB

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