ring_buffer.c 78 KB

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