ring_buffer.c 75 KB

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