ring_buffer.c 77 KB

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