ring_buffer.c 75 KB

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