ring_buffer.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117
  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 entries;
  363. u64 write_stamp;
  364. u64 read_stamp;
  365. atomic_t record_disabled;
  366. };
  367. struct ring_buffer {
  368. unsigned pages;
  369. unsigned flags;
  370. int cpus;
  371. atomic_t record_disabled;
  372. cpumask_var_t cpumask;
  373. struct mutex mutex;
  374. struct ring_buffer_per_cpu **buffers;
  375. #ifdef CONFIG_HOTPLUG_CPU
  376. struct notifier_block cpu_notify;
  377. #endif
  378. u64 (*clock)(void);
  379. };
  380. struct ring_buffer_iter {
  381. struct ring_buffer_per_cpu *cpu_buffer;
  382. unsigned long head;
  383. struct buffer_page *head_page;
  384. u64 read_stamp;
  385. };
  386. /* buffer may be either ring_buffer or ring_buffer_per_cpu */
  387. #define RB_WARN_ON(buffer, cond) \
  388. ({ \
  389. int _____ret = unlikely(cond); \
  390. if (_____ret) { \
  391. atomic_inc(&buffer->record_disabled); \
  392. WARN_ON(1); \
  393. } \
  394. _____ret; \
  395. })
  396. /* Up this if you want to test the TIME_EXTENTS and normalization */
  397. #define DEBUG_SHIFT 0
  398. u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
  399. {
  400. u64 time;
  401. preempt_disable_notrace();
  402. /* shift to debug/test normalization and TIME_EXTENTS */
  403. time = buffer->clock() << DEBUG_SHIFT;
  404. preempt_enable_no_resched_notrace();
  405. return time;
  406. }
  407. EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
  408. void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
  409. int cpu, u64 *ts)
  410. {
  411. /* Just stupid testing the normalize function and deltas */
  412. *ts >>= DEBUG_SHIFT;
  413. }
  414. EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
  415. /**
  416. * check_pages - integrity check of buffer pages
  417. * @cpu_buffer: CPU buffer with pages to test
  418. *
  419. * As a safety measure we check to make sure the data pages have not
  420. * been corrupted.
  421. */
  422. static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  423. {
  424. struct list_head *head = &cpu_buffer->pages;
  425. struct buffer_page *bpage, *tmp;
  426. if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
  427. return -1;
  428. if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
  429. return -1;
  430. list_for_each_entry_safe(bpage, tmp, head, list) {
  431. if (RB_WARN_ON(cpu_buffer,
  432. bpage->list.next->prev != &bpage->list))
  433. return -1;
  434. if (RB_WARN_ON(cpu_buffer,
  435. bpage->list.prev->next != &bpage->list))
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  441. unsigned nr_pages)
  442. {
  443. struct list_head *head = &cpu_buffer->pages;
  444. struct buffer_page *bpage, *tmp;
  445. unsigned long addr;
  446. LIST_HEAD(pages);
  447. unsigned i;
  448. for (i = 0; i < nr_pages; i++) {
  449. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  450. GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
  451. if (!bpage)
  452. goto free_pages;
  453. list_add(&bpage->list, &pages);
  454. addr = __get_free_page(GFP_KERNEL);
  455. if (!addr)
  456. goto free_pages;
  457. bpage->page = (void *)addr;
  458. rb_init_page(bpage->page);
  459. }
  460. list_splice(&pages, head);
  461. rb_check_pages(cpu_buffer);
  462. return 0;
  463. free_pages:
  464. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  465. list_del_init(&bpage->list);
  466. free_buffer_page(bpage);
  467. }
  468. return -ENOMEM;
  469. }
  470. static struct ring_buffer_per_cpu *
  471. rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
  472. {
  473. struct ring_buffer_per_cpu *cpu_buffer;
  474. struct buffer_page *bpage;
  475. unsigned long addr;
  476. int ret;
  477. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  478. GFP_KERNEL, cpu_to_node(cpu));
  479. if (!cpu_buffer)
  480. return NULL;
  481. cpu_buffer->cpu = cpu;
  482. cpu_buffer->buffer = buffer;
  483. spin_lock_init(&cpu_buffer->reader_lock);
  484. cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  485. INIT_LIST_HEAD(&cpu_buffer->pages);
  486. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  487. GFP_KERNEL, cpu_to_node(cpu));
  488. if (!bpage)
  489. goto fail_free_buffer;
  490. cpu_buffer->reader_page = bpage;
  491. addr = __get_free_page(GFP_KERNEL);
  492. if (!addr)
  493. goto fail_free_reader;
  494. bpage->page = (void *)addr;
  495. rb_init_page(bpage->page);
  496. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  497. ret = rb_allocate_pages(cpu_buffer, buffer->pages);
  498. if (ret < 0)
  499. goto fail_free_reader;
  500. cpu_buffer->head_page
  501. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  502. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  503. return cpu_buffer;
  504. fail_free_reader:
  505. free_buffer_page(cpu_buffer->reader_page);
  506. fail_free_buffer:
  507. kfree(cpu_buffer);
  508. return NULL;
  509. }
  510. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  511. {
  512. struct list_head *head = &cpu_buffer->pages;
  513. struct buffer_page *bpage, *tmp;
  514. free_buffer_page(cpu_buffer->reader_page);
  515. list_for_each_entry_safe(bpage, tmp, head, list) {
  516. list_del_init(&bpage->list);
  517. free_buffer_page(bpage);
  518. }
  519. kfree(cpu_buffer);
  520. }
  521. /*
  522. * Causes compile errors if the struct buffer_page gets bigger
  523. * than the struct page.
  524. */
  525. extern int ring_buffer_page_too_big(void);
  526. #ifdef CONFIG_HOTPLUG_CPU
  527. static int rb_cpu_notify(struct notifier_block *self,
  528. unsigned long action, void *hcpu);
  529. #endif
  530. /**
  531. * ring_buffer_alloc - allocate a new ring_buffer
  532. * @size: the size in bytes per cpu that is needed.
  533. * @flags: attributes to set for the ring buffer.
  534. *
  535. * Currently the only flag that is available is the RB_FL_OVERWRITE
  536. * flag. This flag means that the buffer will overwrite old data
  537. * when the buffer wraps. If this flag is not set, the buffer will
  538. * drop data when the tail hits the head.
  539. */
  540. struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
  541. {
  542. struct ring_buffer *buffer;
  543. int bsize;
  544. int cpu;
  545. /* Paranoid! Optimizes out when all is well */
  546. if (sizeof(struct buffer_page) > sizeof(struct page))
  547. ring_buffer_page_too_big();
  548. /* keep it in its own cache line */
  549. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  550. GFP_KERNEL);
  551. if (!buffer)
  552. return NULL;
  553. if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  554. goto fail_free_buffer;
  555. buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  556. buffer->flags = flags;
  557. buffer->clock = trace_clock_local;
  558. /* need at least two pages */
  559. if (buffer->pages == 1)
  560. buffer->pages++;
  561. /*
  562. * In case of non-hotplug cpu, if the ring-buffer is allocated
  563. * in early initcall, it will not be notified of secondary cpus.
  564. * In that off case, we need to allocate for all possible cpus.
  565. */
  566. #ifdef CONFIG_HOTPLUG_CPU
  567. get_online_cpus();
  568. cpumask_copy(buffer->cpumask, cpu_online_mask);
  569. #else
  570. cpumask_copy(buffer->cpumask, cpu_possible_mask);
  571. #endif
  572. buffer->cpus = nr_cpu_ids;
  573. bsize = sizeof(void *) * nr_cpu_ids;
  574. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  575. GFP_KERNEL);
  576. if (!buffer->buffers)
  577. goto fail_free_cpumask;
  578. for_each_buffer_cpu(buffer, cpu) {
  579. buffer->buffers[cpu] =
  580. rb_allocate_cpu_buffer(buffer, cpu);
  581. if (!buffer->buffers[cpu])
  582. goto fail_free_buffers;
  583. }
  584. #ifdef CONFIG_HOTPLUG_CPU
  585. buffer->cpu_notify.notifier_call = rb_cpu_notify;
  586. buffer->cpu_notify.priority = 0;
  587. register_cpu_notifier(&buffer->cpu_notify);
  588. #endif
  589. put_online_cpus();
  590. mutex_init(&buffer->mutex);
  591. return buffer;
  592. fail_free_buffers:
  593. for_each_buffer_cpu(buffer, cpu) {
  594. if (buffer->buffers[cpu])
  595. rb_free_cpu_buffer(buffer->buffers[cpu]);
  596. }
  597. kfree(buffer->buffers);
  598. fail_free_cpumask:
  599. free_cpumask_var(buffer->cpumask);
  600. put_online_cpus();
  601. fail_free_buffer:
  602. kfree(buffer);
  603. return NULL;
  604. }
  605. EXPORT_SYMBOL_GPL(ring_buffer_alloc);
  606. /**
  607. * ring_buffer_free - free a ring buffer.
  608. * @buffer: the buffer to free.
  609. */
  610. void
  611. ring_buffer_free(struct ring_buffer *buffer)
  612. {
  613. int cpu;
  614. get_online_cpus();
  615. #ifdef CONFIG_HOTPLUG_CPU
  616. unregister_cpu_notifier(&buffer->cpu_notify);
  617. #endif
  618. for_each_buffer_cpu(buffer, cpu)
  619. rb_free_cpu_buffer(buffer->buffers[cpu]);
  620. put_online_cpus();
  621. free_cpumask_var(buffer->cpumask);
  622. kfree(buffer);
  623. }
  624. EXPORT_SYMBOL_GPL(ring_buffer_free);
  625. void ring_buffer_set_clock(struct ring_buffer *buffer,
  626. u64 (*clock)(void))
  627. {
  628. buffer->clock = clock;
  629. }
  630. static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
  631. static void
  632. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
  633. {
  634. struct buffer_page *bpage;
  635. struct list_head *p;
  636. unsigned i;
  637. atomic_inc(&cpu_buffer->record_disabled);
  638. synchronize_sched();
  639. for (i = 0; i < nr_pages; i++) {
  640. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  641. return;
  642. p = cpu_buffer->pages.next;
  643. bpage = list_entry(p, struct buffer_page, list);
  644. list_del_init(&bpage->list);
  645. free_buffer_page(bpage);
  646. }
  647. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  648. return;
  649. rb_reset_cpu(cpu_buffer);
  650. rb_check_pages(cpu_buffer);
  651. atomic_dec(&cpu_buffer->record_disabled);
  652. }
  653. static void
  654. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
  655. struct list_head *pages, unsigned nr_pages)
  656. {
  657. struct buffer_page *bpage;
  658. struct list_head *p;
  659. unsigned i;
  660. atomic_inc(&cpu_buffer->record_disabled);
  661. synchronize_sched();
  662. for (i = 0; i < nr_pages; i++) {
  663. if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
  664. return;
  665. p = pages->next;
  666. bpage = list_entry(p, struct buffer_page, list);
  667. list_del_init(&bpage->list);
  668. list_add_tail(&bpage->list, &cpu_buffer->pages);
  669. }
  670. rb_reset_cpu(cpu_buffer);
  671. rb_check_pages(cpu_buffer);
  672. atomic_dec(&cpu_buffer->record_disabled);
  673. }
  674. /**
  675. * ring_buffer_resize - resize the ring buffer
  676. * @buffer: the buffer to resize.
  677. * @size: the new size.
  678. *
  679. * The tracer is responsible for making sure that the buffer is
  680. * not being used while changing the size.
  681. * Note: We may be able to change the above requirement by using
  682. * RCU synchronizations.
  683. *
  684. * Minimum size is 2 * BUF_PAGE_SIZE.
  685. *
  686. * Returns -1 on failure.
  687. */
  688. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
  689. {
  690. struct ring_buffer_per_cpu *cpu_buffer;
  691. unsigned nr_pages, rm_pages, new_pages;
  692. struct buffer_page *bpage, *tmp;
  693. unsigned long buffer_size;
  694. unsigned long addr;
  695. LIST_HEAD(pages);
  696. int i, cpu;
  697. /*
  698. * Always succeed at resizing a non-existent buffer:
  699. */
  700. if (!buffer)
  701. return size;
  702. size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  703. size *= BUF_PAGE_SIZE;
  704. buffer_size = buffer->pages * BUF_PAGE_SIZE;
  705. /* we need a minimum of two pages */
  706. if (size < BUF_PAGE_SIZE * 2)
  707. size = BUF_PAGE_SIZE * 2;
  708. if (size == buffer_size)
  709. return size;
  710. mutex_lock(&buffer->mutex);
  711. get_online_cpus();
  712. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  713. if (size < buffer_size) {
  714. /* easy case, just free pages */
  715. if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
  716. goto out_fail;
  717. rm_pages = buffer->pages - nr_pages;
  718. for_each_buffer_cpu(buffer, cpu) {
  719. cpu_buffer = buffer->buffers[cpu];
  720. rb_remove_pages(cpu_buffer, rm_pages);
  721. }
  722. goto out;
  723. }
  724. /*
  725. * This is a bit more difficult. We only want to add pages
  726. * when we can allocate enough for all CPUs. We do this
  727. * by allocating all the pages and storing them on a local
  728. * link list. If we succeed in our allocation, then we
  729. * add these pages to the cpu_buffers. Otherwise we just free
  730. * them all and return -ENOMEM;
  731. */
  732. if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
  733. goto out_fail;
  734. new_pages = nr_pages - buffer->pages;
  735. for_each_buffer_cpu(buffer, cpu) {
  736. for (i = 0; i < new_pages; i++) {
  737. bpage = kzalloc_node(ALIGN(sizeof(*bpage),
  738. cache_line_size()),
  739. GFP_KERNEL, cpu_to_node(cpu));
  740. if (!bpage)
  741. goto free_pages;
  742. list_add(&bpage->list, &pages);
  743. addr = __get_free_page(GFP_KERNEL);
  744. if (!addr)
  745. goto free_pages;
  746. bpage->page = (void *)addr;
  747. rb_init_page(bpage->page);
  748. }
  749. }
  750. for_each_buffer_cpu(buffer, cpu) {
  751. cpu_buffer = buffer->buffers[cpu];
  752. rb_insert_pages(cpu_buffer, &pages, new_pages);
  753. }
  754. if (RB_WARN_ON(buffer, !list_empty(&pages)))
  755. goto out_fail;
  756. out:
  757. buffer->pages = nr_pages;
  758. put_online_cpus();
  759. mutex_unlock(&buffer->mutex);
  760. return size;
  761. free_pages:
  762. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  763. list_del_init(&bpage->list);
  764. free_buffer_page(bpage);
  765. }
  766. put_online_cpus();
  767. mutex_unlock(&buffer->mutex);
  768. return -ENOMEM;
  769. /*
  770. * Something went totally wrong, and we are too paranoid
  771. * to even clean up the mess.
  772. */
  773. out_fail:
  774. put_online_cpus();
  775. mutex_unlock(&buffer->mutex);
  776. return -1;
  777. }
  778. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  779. static inline void *
  780. __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
  781. {
  782. return bpage->data + index;
  783. }
  784. static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  785. {
  786. return bpage->page->data + index;
  787. }
  788. static inline struct ring_buffer_event *
  789. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  790. {
  791. return __rb_page_index(cpu_buffer->reader_page,
  792. cpu_buffer->reader_page->read);
  793. }
  794. static inline struct ring_buffer_event *
  795. rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
  796. {
  797. return __rb_page_index(cpu_buffer->head_page,
  798. cpu_buffer->head_page->read);
  799. }
  800. static inline struct ring_buffer_event *
  801. rb_iter_head_event(struct ring_buffer_iter *iter)
  802. {
  803. return __rb_page_index(iter->head_page, iter->head);
  804. }
  805. static inline unsigned rb_page_write(struct buffer_page *bpage)
  806. {
  807. return local_read(&bpage->write);
  808. }
  809. static inline unsigned rb_page_commit(struct buffer_page *bpage)
  810. {
  811. return local_read(&bpage->page->commit);
  812. }
  813. /* Size is determined by what has been commited */
  814. static inline unsigned rb_page_size(struct buffer_page *bpage)
  815. {
  816. return rb_page_commit(bpage);
  817. }
  818. static inline unsigned
  819. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  820. {
  821. return rb_page_commit(cpu_buffer->commit_page);
  822. }
  823. static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
  824. {
  825. return rb_page_commit(cpu_buffer->head_page);
  826. }
  827. /*
  828. * When the tail hits the head and the buffer is in overwrite mode,
  829. * the head jumps to the next page and all content on the previous
  830. * page is discarded. But before doing so, we update the overrun
  831. * variable of the buffer.
  832. */
  833. static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
  834. {
  835. struct ring_buffer_event *event;
  836. unsigned long head;
  837. for (head = 0; head < rb_head_size(cpu_buffer);
  838. head += rb_event_length(event)) {
  839. event = __rb_page_index(cpu_buffer->head_page, head);
  840. if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
  841. return;
  842. /* Only count data entries */
  843. if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  844. continue;
  845. cpu_buffer->overrun++;
  846. cpu_buffer->entries--;
  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. 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. 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 = cpu_buffer->entries;
  1599. return ret;
  1600. }
  1601. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  1602. /**
  1603. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  1604. * @buffer: The ring buffer
  1605. * @cpu: The per CPU buffer to get the number of overruns from
  1606. */
  1607. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  1608. {
  1609. struct ring_buffer_per_cpu *cpu_buffer;
  1610. unsigned long ret;
  1611. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1612. return 0;
  1613. cpu_buffer = buffer->buffers[cpu];
  1614. ret = cpu_buffer->overrun;
  1615. return ret;
  1616. }
  1617. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  1618. /**
  1619. * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
  1620. * @buffer: The ring buffer
  1621. * @cpu: The per CPU buffer to get the number of overruns from
  1622. */
  1623. unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
  1624. {
  1625. struct ring_buffer_per_cpu *cpu_buffer;
  1626. unsigned long ret;
  1627. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1628. return 0;
  1629. cpu_buffer = buffer->buffers[cpu];
  1630. ret = cpu_buffer->nmi_dropped;
  1631. return ret;
  1632. }
  1633. EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
  1634. /**
  1635. * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
  1636. * @buffer: The ring buffer
  1637. * @cpu: The per CPU buffer to get the number of overruns from
  1638. */
  1639. unsigned long
  1640. ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
  1641. {
  1642. struct ring_buffer_per_cpu *cpu_buffer;
  1643. unsigned long ret;
  1644. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1645. return 0;
  1646. cpu_buffer = buffer->buffers[cpu];
  1647. ret = cpu_buffer->commit_overrun;
  1648. return ret;
  1649. }
  1650. EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
  1651. /**
  1652. * ring_buffer_entries - get the number of entries in a buffer
  1653. * @buffer: The ring buffer
  1654. *
  1655. * Returns the total number of entries in the ring buffer
  1656. * (all CPU entries)
  1657. */
  1658. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  1659. {
  1660. struct ring_buffer_per_cpu *cpu_buffer;
  1661. unsigned long entries = 0;
  1662. int cpu;
  1663. /* if you care about this being correct, lock the buffer */
  1664. for_each_buffer_cpu(buffer, cpu) {
  1665. cpu_buffer = buffer->buffers[cpu];
  1666. entries += cpu_buffer->entries;
  1667. }
  1668. return entries;
  1669. }
  1670. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  1671. /**
  1672. * ring_buffer_overrun_cpu - get the number of overruns in buffer
  1673. * @buffer: The ring buffer
  1674. *
  1675. * Returns the total number of overruns in the ring buffer
  1676. * (all CPU entries)
  1677. */
  1678. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  1679. {
  1680. struct ring_buffer_per_cpu *cpu_buffer;
  1681. unsigned long overruns = 0;
  1682. int cpu;
  1683. /* if you care about this being correct, lock the buffer */
  1684. for_each_buffer_cpu(buffer, cpu) {
  1685. cpu_buffer = buffer->buffers[cpu];
  1686. overruns += cpu_buffer->overrun;
  1687. }
  1688. return overruns;
  1689. }
  1690. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  1691. static void rb_iter_reset(struct ring_buffer_iter *iter)
  1692. {
  1693. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1694. /* Iterator usage is expected to have record disabled */
  1695. if (list_empty(&cpu_buffer->reader_page->list)) {
  1696. iter->head_page = cpu_buffer->head_page;
  1697. iter->head = cpu_buffer->head_page->read;
  1698. } else {
  1699. iter->head_page = cpu_buffer->reader_page;
  1700. iter->head = cpu_buffer->reader_page->read;
  1701. }
  1702. if (iter->head)
  1703. iter->read_stamp = cpu_buffer->read_stamp;
  1704. else
  1705. iter->read_stamp = iter->head_page->page->time_stamp;
  1706. }
  1707. /**
  1708. * ring_buffer_iter_reset - reset an iterator
  1709. * @iter: The iterator to reset
  1710. *
  1711. * Resets the iterator, so that it will start from the beginning
  1712. * again.
  1713. */
  1714. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  1715. {
  1716. struct ring_buffer_per_cpu *cpu_buffer;
  1717. unsigned long flags;
  1718. if (!iter)
  1719. return;
  1720. cpu_buffer = iter->cpu_buffer;
  1721. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1722. rb_iter_reset(iter);
  1723. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1724. }
  1725. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  1726. /**
  1727. * ring_buffer_iter_empty - check if an iterator has no more to read
  1728. * @iter: The iterator to check
  1729. */
  1730. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  1731. {
  1732. struct ring_buffer_per_cpu *cpu_buffer;
  1733. cpu_buffer = iter->cpu_buffer;
  1734. return iter->head_page == cpu_buffer->commit_page &&
  1735. iter->head == rb_commit_index(cpu_buffer);
  1736. }
  1737. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  1738. static void
  1739. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1740. struct ring_buffer_event *event)
  1741. {
  1742. u64 delta;
  1743. switch (event->type_len) {
  1744. case RINGBUF_TYPE_PADDING:
  1745. return;
  1746. case RINGBUF_TYPE_TIME_EXTEND:
  1747. delta = event->array[0];
  1748. delta <<= TS_SHIFT;
  1749. delta += event->time_delta;
  1750. cpu_buffer->read_stamp += delta;
  1751. return;
  1752. case RINGBUF_TYPE_TIME_STAMP:
  1753. /* FIXME: not implemented */
  1754. return;
  1755. case RINGBUF_TYPE_DATA:
  1756. cpu_buffer->read_stamp += event->time_delta;
  1757. return;
  1758. default:
  1759. BUG();
  1760. }
  1761. return;
  1762. }
  1763. static void
  1764. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  1765. struct ring_buffer_event *event)
  1766. {
  1767. u64 delta;
  1768. switch (event->type_len) {
  1769. case RINGBUF_TYPE_PADDING:
  1770. return;
  1771. case RINGBUF_TYPE_TIME_EXTEND:
  1772. delta = event->array[0];
  1773. delta <<= TS_SHIFT;
  1774. delta += event->time_delta;
  1775. iter->read_stamp += delta;
  1776. return;
  1777. case RINGBUF_TYPE_TIME_STAMP:
  1778. /* FIXME: not implemented */
  1779. return;
  1780. case RINGBUF_TYPE_DATA:
  1781. iter->read_stamp += event->time_delta;
  1782. return;
  1783. default:
  1784. BUG();
  1785. }
  1786. return;
  1787. }
  1788. static struct buffer_page *
  1789. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  1790. {
  1791. struct buffer_page *reader = NULL;
  1792. unsigned long flags;
  1793. int nr_loops = 0;
  1794. local_irq_save(flags);
  1795. __raw_spin_lock(&cpu_buffer->lock);
  1796. again:
  1797. /*
  1798. * This should normally only loop twice. But because the
  1799. * start of the reader inserts an empty page, it causes
  1800. * a case where we will loop three times. There should be no
  1801. * reason to loop four times (that I know of).
  1802. */
  1803. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  1804. reader = NULL;
  1805. goto out;
  1806. }
  1807. reader = cpu_buffer->reader_page;
  1808. /* If there's more to read, return this page */
  1809. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  1810. goto out;
  1811. /* Never should we have an index greater than the size */
  1812. if (RB_WARN_ON(cpu_buffer,
  1813. cpu_buffer->reader_page->read > rb_page_size(reader)))
  1814. goto out;
  1815. /* check if we caught up to the tail */
  1816. reader = NULL;
  1817. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  1818. goto out;
  1819. /*
  1820. * Splice the empty reader page into the list around the head.
  1821. * Reset the reader page to size zero.
  1822. */
  1823. reader = cpu_buffer->head_page;
  1824. cpu_buffer->reader_page->list.next = reader->list.next;
  1825. cpu_buffer->reader_page->list.prev = reader->list.prev;
  1826. local_set(&cpu_buffer->reader_page->write, 0);
  1827. local_set(&cpu_buffer->reader_page->page->commit, 0);
  1828. /* Make the reader page now replace the head */
  1829. reader->list.prev->next = &cpu_buffer->reader_page->list;
  1830. reader->list.next->prev = &cpu_buffer->reader_page->list;
  1831. /*
  1832. * If the tail is on the reader, then we must set the head
  1833. * to the inserted page, otherwise we set it one before.
  1834. */
  1835. cpu_buffer->head_page = cpu_buffer->reader_page;
  1836. if (cpu_buffer->commit_page != reader)
  1837. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  1838. /* Finally update the reader page to the new head */
  1839. cpu_buffer->reader_page = reader;
  1840. rb_reset_reader_page(cpu_buffer);
  1841. goto again;
  1842. out:
  1843. __raw_spin_unlock(&cpu_buffer->lock);
  1844. local_irq_restore(flags);
  1845. return reader;
  1846. }
  1847. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  1848. {
  1849. struct ring_buffer_event *event;
  1850. struct buffer_page *reader;
  1851. unsigned length;
  1852. reader = rb_get_reader_page(cpu_buffer);
  1853. /* This function should not be called when buffer is empty */
  1854. if (RB_WARN_ON(cpu_buffer, !reader))
  1855. return;
  1856. event = rb_reader_event(cpu_buffer);
  1857. if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
  1858. || rb_discarded_event(event))
  1859. cpu_buffer->entries--;
  1860. rb_update_read_stamp(cpu_buffer, event);
  1861. length = rb_event_length(event);
  1862. cpu_buffer->reader_page->read += length;
  1863. }
  1864. static void rb_advance_iter(struct ring_buffer_iter *iter)
  1865. {
  1866. struct ring_buffer *buffer;
  1867. struct ring_buffer_per_cpu *cpu_buffer;
  1868. struct ring_buffer_event *event;
  1869. unsigned length;
  1870. cpu_buffer = iter->cpu_buffer;
  1871. buffer = cpu_buffer->buffer;
  1872. /*
  1873. * Check if we are at the end of the buffer.
  1874. */
  1875. if (iter->head >= rb_page_size(iter->head_page)) {
  1876. if (RB_WARN_ON(buffer,
  1877. iter->head_page == cpu_buffer->commit_page))
  1878. return;
  1879. rb_inc_iter(iter);
  1880. return;
  1881. }
  1882. event = rb_iter_head_event(iter);
  1883. length = rb_event_length(event);
  1884. /*
  1885. * This should not be called to advance the header if we are
  1886. * at the tail of the buffer.
  1887. */
  1888. if (RB_WARN_ON(cpu_buffer,
  1889. (iter->head_page == cpu_buffer->commit_page) &&
  1890. (iter->head + length > rb_commit_index(cpu_buffer))))
  1891. return;
  1892. rb_update_iter_read_stamp(iter, event);
  1893. iter->head += length;
  1894. /* check for end of page padding */
  1895. if ((iter->head >= rb_page_size(iter->head_page)) &&
  1896. (iter->head_page != cpu_buffer->commit_page))
  1897. rb_advance_iter(iter);
  1898. }
  1899. static struct ring_buffer_event *
  1900. rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  1901. {
  1902. struct ring_buffer_per_cpu *cpu_buffer;
  1903. struct ring_buffer_event *event;
  1904. struct buffer_page *reader;
  1905. int nr_loops = 0;
  1906. cpu_buffer = buffer->buffers[cpu];
  1907. again:
  1908. /*
  1909. * We repeat when a timestamp is encountered. It is possible
  1910. * to get multiple timestamps from an interrupt entering just
  1911. * as one timestamp is about to be written. The max times
  1912. * that this can happen is the number of nested interrupts we
  1913. * can have. Nesting 10 deep of interrupts is clearly
  1914. * an anomaly.
  1915. */
  1916. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
  1917. return NULL;
  1918. reader = rb_get_reader_page(cpu_buffer);
  1919. if (!reader)
  1920. return NULL;
  1921. event = rb_reader_event(cpu_buffer);
  1922. switch (event->type_len) {
  1923. case RINGBUF_TYPE_PADDING:
  1924. if (rb_null_event(event))
  1925. RB_WARN_ON(cpu_buffer, 1);
  1926. /*
  1927. * Because the writer could be discarding every
  1928. * event it creates (which would probably be bad)
  1929. * if we were to go back to "again" then we may never
  1930. * catch up, and will trigger the warn on, or lock
  1931. * the box. Return the padding, and we will release
  1932. * the current locks, and try again.
  1933. */
  1934. rb_advance_reader(cpu_buffer);
  1935. return event;
  1936. case RINGBUF_TYPE_TIME_EXTEND:
  1937. /* Internal data, OK to advance */
  1938. rb_advance_reader(cpu_buffer);
  1939. goto again;
  1940. case RINGBUF_TYPE_TIME_STAMP:
  1941. /* FIXME: not implemented */
  1942. rb_advance_reader(cpu_buffer);
  1943. goto again;
  1944. case RINGBUF_TYPE_DATA:
  1945. if (ts) {
  1946. *ts = cpu_buffer->read_stamp + event->time_delta;
  1947. ring_buffer_normalize_time_stamp(buffer,
  1948. cpu_buffer->cpu, ts);
  1949. }
  1950. return event;
  1951. default:
  1952. BUG();
  1953. }
  1954. return NULL;
  1955. }
  1956. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  1957. static struct ring_buffer_event *
  1958. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  1959. {
  1960. struct ring_buffer *buffer;
  1961. struct ring_buffer_per_cpu *cpu_buffer;
  1962. struct ring_buffer_event *event;
  1963. int nr_loops = 0;
  1964. if (ring_buffer_iter_empty(iter))
  1965. return NULL;
  1966. cpu_buffer = iter->cpu_buffer;
  1967. buffer = cpu_buffer->buffer;
  1968. again:
  1969. /*
  1970. * We repeat when a timestamp is encountered. It is possible
  1971. * to get multiple timestamps from an interrupt entering just
  1972. * as one timestamp is about to be written. The max times
  1973. * that this can happen is the number of nested interrupts we
  1974. * can have. Nesting 10 deep of interrupts is clearly
  1975. * an anomaly.
  1976. */
  1977. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
  1978. return NULL;
  1979. if (rb_per_cpu_empty(cpu_buffer))
  1980. return NULL;
  1981. event = rb_iter_head_event(iter);
  1982. switch (event->type_len) {
  1983. case RINGBUF_TYPE_PADDING:
  1984. if (rb_null_event(event)) {
  1985. rb_inc_iter(iter);
  1986. goto again;
  1987. }
  1988. rb_advance_iter(iter);
  1989. return event;
  1990. case RINGBUF_TYPE_TIME_EXTEND:
  1991. /* Internal data, OK to advance */
  1992. rb_advance_iter(iter);
  1993. goto again;
  1994. case RINGBUF_TYPE_TIME_STAMP:
  1995. /* FIXME: not implemented */
  1996. rb_advance_iter(iter);
  1997. goto again;
  1998. case RINGBUF_TYPE_DATA:
  1999. if (ts) {
  2000. *ts = iter->read_stamp + event->time_delta;
  2001. ring_buffer_normalize_time_stamp(buffer,
  2002. cpu_buffer->cpu, ts);
  2003. }
  2004. return event;
  2005. default:
  2006. BUG();
  2007. }
  2008. return NULL;
  2009. }
  2010. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  2011. /**
  2012. * ring_buffer_peek - peek at the next event to be read
  2013. * @buffer: The ring buffer to read
  2014. * @cpu: The cpu to peak at
  2015. * @ts: The timestamp counter of this event.
  2016. *
  2017. * This will return the event that will be read next, but does
  2018. * not consume the data.
  2019. */
  2020. struct ring_buffer_event *
  2021. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  2022. {
  2023. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2024. struct ring_buffer_event *event;
  2025. unsigned long flags;
  2026. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2027. return NULL;
  2028. again:
  2029. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2030. event = rb_buffer_peek(buffer, cpu, ts);
  2031. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2032. if (event && event->type_len == RINGBUF_TYPE_PADDING) {
  2033. cpu_relax();
  2034. goto again;
  2035. }
  2036. return event;
  2037. }
  2038. /**
  2039. * ring_buffer_iter_peek - peek at the next event to be read
  2040. * @iter: The ring buffer iterator
  2041. * @ts: The timestamp counter of this event.
  2042. *
  2043. * This will return the event that will be read next, but does
  2044. * not increment the iterator.
  2045. */
  2046. struct ring_buffer_event *
  2047. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2048. {
  2049. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2050. struct ring_buffer_event *event;
  2051. unsigned long flags;
  2052. again:
  2053. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2054. event = rb_iter_peek(iter, ts);
  2055. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2056. if (event && event->type_len == RINGBUF_TYPE_PADDING) {
  2057. cpu_relax();
  2058. goto again;
  2059. }
  2060. return event;
  2061. }
  2062. /**
  2063. * ring_buffer_consume - return an event and consume it
  2064. * @buffer: The ring buffer to get the next event from
  2065. *
  2066. * Returns the next event in the ring buffer, and that event is consumed.
  2067. * Meaning, that sequential reads will keep returning a different event,
  2068. * and eventually empty the ring buffer if the producer is slower.
  2069. */
  2070. struct ring_buffer_event *
  2071. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
  2072. {
  2073. struct ring_buffer_per_cpu *cpu_buffer;
  2074. struct ring_buffer_event *event = NULL;
  2075. unsigned long flags;
  2076. again:
  2077. /* might be called in atomic */
  2078. preempt_disable();
  2079. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2080. goto out;
  2081. cpu_buffer = buffer->buffers[cpu];
  2082. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2083. event = rb_buffer_peek(buffer, cpu, ts);
  2084. if (!event)
  2085. goto out_unlock;
  2086. rb_advance_reader(cpu_buffer);
  2087. out_unlock:
  2088. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2089. out:
  2090. preempt_enable();
  2091. if (event && event->type_len == RINGBUF_TYPE_PADDING) {
  2092. cpu_relax();
  2093. goto again;
  2094. }
  2095. return event;
  2096. }
  2097. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  2098. /**
  2099. * ring_buffer_read_start - start a non consuming read of the buffer
  2100. * @buffer: The ring buffer to read from
  2101. * @cpu: The cpu buffer to iterate over
  2102. *
  2103. * This starts up an iteration through the buffer. It also disables
  2104. * the recording to the buffer until the reading is finished.
  2105. * This prevents the reading from being corrupted. This is not
  2106. * a consuming read, so a producer is not expected.
  2107. *
  2108. * Must be paired with ring_buffer_finish.
  2109. */
  2110. struct ring_buffer_iter *
  2111. ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
  2112. {
  2113. struct ring_buffer_per_cpu *cpu_buffer;
  2114. struct ring_buffer_iter *iter;
  2115. unsigned long flags;
  2116. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2117. return NULL;
  2118. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2119. if (!iter)
  2120. return NULL;
  2121. cpu_buffer = buffer->buffers[cpu];
  2122. iter->cpu_buffer = cpu_buffer;
  2123. atomic_inc(&cpu_buffer->record_disabled);
  2124. synchronize_sched();
  2125. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2126. __raw_spin_lock(&cpu_buffer->lock);
  2127. rb_iter_reset(iter);
  2128. __raw_spin_unlock(&cpu_buffer->lock);
  2129. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2130. return iter;
  2131. }
  2132. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  2133. /**
  2134. * ring_buffer_finish - finish reading the iterator of the buffer
  2135. * @iter: The iterator retrieved by ring_buffer_start
  2136. *
  2137. * This re-enables the recording to the buffer, and frees the
  2138. * iterator.
  2139. */
  2140. void
  2141. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  2142. {
  2143. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2144. atomic_dec(&cpu_buffer->record_disabled);
  2145. kfree(iter);
  2146. }
  2147. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  2148. /**
  2149. * ring_buffer_read - read the next item in the ring buffer by the iterator
  2150. * @iter: The ring buffer iterator
  2151. * @ts: The time stamp of the event read.
  2152. *
  2153. * This reads the next event in the ring buffer and increments the iterator.
  2154. */
  2155. struct ring_buffer_event *
  2156. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  2157. {
  2158. struct ring_buffer_event *event;
  2159. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2160. unsigned long flags;
  2161. again:
  2162. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2163. event = rb_iter_peek(iter, ts);
  2164. if (!event)
  2165. goto out;
  2166. rb_advance_iter(iter);
  2167. out:
  2168. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2169. if (event && event->type_len == RINGBUF_TYPE_PADDING) {
  2170. cpu_relax();
  2171. goto again;
  2172. }
  2173. return event;
  2174. }
  2175. EXPORT_SYMBOL_GPL(ring_buffer_read);
  2176. /**
  2177. * ring_buffer_size - return the size of the ring buffer (in bytes)
  2178. * @buffer: The ring buffer.
  2179. */
  2180. unsigned long ring_buffer_size(struct ring_buffer *buffer)
  2181. {
  2182. return BUF_PAGE_SIZE * buffer->pages;
  2183. }
  2184. EXPORT_SYMBOL_GPL(ring_buffer_size);
  2185. static void
  2186. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  2187. {
  2188. cpu_buffer->head_page
  2189. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  2190. local_set(&cpu_buffer->head_page->write, 0);
  2191. local_set(&cpu_buffer->head_page->page->commit, 0);
  2192. cpu_buffer->head_page->read = 0;
  2193. cpu_buffer->tail_page = cpu_buffer->head_page;
  2194. cpu_buffer->commit_page = cpu_buffer->head_page;
  2195. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2196. local_set(&cpu_buffer->reader_page->write, 0);
  2197. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2198. cpu_buffer->reader_page->read = 0;
  2199. cpu_buffer->nmi_dropped = 0;
  2200. cpu_buffer->commit_overrun = 0;
  2201. cpu_buffer->overrun = 0;
  2202. cpu_buffer->entries = 0;
  2203. cpu_buffer->write_stamp = 0;
  2204. cpu_buffer->read_stamp = 0;
  2205. }
  2206. /**
  2207. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  2208. * @buffer: The ring buffer to reset a per cpu buffer of
  2209. * @cpu: The CPU buffer to be reset
  2210. */
  2211. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  2212. {
  2213. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2214. unsigned long flags;
  2215. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2216. return;
  2217. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2218. __raw_spin_lock(&cpu_buffer->lock);
  2219. rb_reset_cpu(cpu_buffer);
  2220. __raw_spin_unlock(&cpu_buffer->lock);
  2221. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2222. }
  2223. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  2224. /**
  2225. * ring_buffer_reset - reset a ring buffer
  2226. * @buffer: The ring buffer to reset all cpu buffers
  2227. */
  2228. void ring_buffer_reset(struct ring_buffer *buffer)
  2229. {
  2230. int cpu;
  2231. for_each_buffer_cpu(buffer, cpu)
  2232. ring_buffer_reset_cpu(buffer, cpu);
  2233. }
  2234. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  2235. /**
  2236. * rind_buffer_empty - is the ring buffer empty?
  2237. * @buffer: The ring buffer to test
  2238. */
  2239. int ring_buffer_empty(struct ring_buffer *buffer)
  2240. {
  2241. struct ring_buffer_per_cpu *cpu_buffer;
  2242. int cpu;
  2243. /* yes this is racy, but if you don't like the race, lock the buffer */
  2244. for_each_buffer_cpu(buffer, cpu) {
  2245. cpu_buffer = buffer->buffers[cpu];
  2246. if (!rb_per_cpu_empty(cpu_buffer))
  2247. return 0;
  2248. }
  2249. return 1;
  2250. }
  2251. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  2252. /**
  2253. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  2254. * @buffer: The ring buffer
  2255. * @cpu: The CPU buffer to test
  2256. */
  2257. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  2258. {
  2259. struct ring_buffer_per_cpu *cpu_buffer;
  2260. int ret;
  2261. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2262. return 1;
  2263. cpu_buffer = buffer->buffers[cpu];
  2264. ret = rb_per_cpu_empty(cpu_buffer);
  2265. return ret;
  2266. }
  2267. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  2268. /**
  2269. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  2270. * @buffer_a: One buffer to swap with
  2271. * @buffer_b: The other buffer to swap with
  2272. *
  2273. * This function is useful for tracers that want to take a "snapshot"
  2274. * of a CPU buffer and has another back up buffer lying around.
  2275. * it is expected that the tracer handles the cpu buffer not being
  2276. * used at the moment.
  2277. */
  2278. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  2279. struct ring_buffer *buffer_b, int cpu)
  2280. {
  2281. struct ring_buffer_per_cpu *cpu_buffer_a;
  2282. struct ring_buffer_per_cpu *cpu_buffer_b;
  2283. int ret = -EINVAL;
  2284. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  2285. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  2286. goto out;
  2287. /* At least make sure the two buffers are somewhat the same */
  2288. if (buffer_a->pages != buffer_b->pages)
  2289. goto out;
  2290. ret = -EAGAIN;
  2291. if (ring_buffer_flags != RB_BUFFERS_ON)
  2292. goto out;
  2293. if (atomic_read(&buffer_a->record_disabled))
  2294. goto out;
  2295. if (atomic_read(&buffer_b->record_disabled))
  2296. goto out;
  2297. cpu_buffer_a = buffer_a->buffers[cpu];
  2298. cpu_buffer_b = buffer_b->buffers[cpu];
  2299. if (atomic_read(&cpu_buffer_a->record_disabled))
  2300. goto out;
  2301. if (atomic_read(&cpu_buffer_b->record_disabled))
  2302. goto out;
  2303. /*
  2304. * We can't do a synchronize_sched here because this
  2305. * function can be called in atomic context.
  2306. * Normally this will be called from the same CPU as cpu.
  2307. * If not it's up to the caller to protect this.
  2308. */
  2309. atomic_inc(&cpu_buffer_a->record_disabled);
  2310. atomic_inc(&cpu_buffer_b->record_disabled);
  2311. buffer_a->buffers[cpu] = cpu_buffer_b;
  2312. buffer_b->buffers[cpu] = cpu_buffer_a;
  2313. cpu_buffer_b->buffer = buffer_a;
  2314. cpu_buffer_a->buffer = buffer_b;
  2315. atomic_dec(&cpu_buffer_a->record_disabled);
  2316. atomic_dec(&cpu_buffer_b->record_disabled);
  2317. ret = 0;
  2318. out:
  2319. return ret;
  2320. }
  2321. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  2322. static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
  2323. struct buffer_data_page *bpage,
  2324. unsigned int offset)
  2325. {
  2326. struct ring_buffer_event *event;
  2327. unsigned long head;
  2328. __raw_spin_lock(&cpu_buffer->lock);
  2329. for (head = offset; head < local_read(&bpage->commit);
  2330. head += rb_event_length(event)) {
  2331. event = __rb_data_page_index(bpage, head);
  2332. if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
  2333. return;
  2334. /* Only count data entries */
  2335. if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  2336. continue;
  2337. cpu_buffer->entries--;
  2338. }
  2339. __raw_spin_unlock(&cpu_buffer->lock);
  2340. }
  2341. /**
  2342. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  2343. * @buffer: the buffer to allocate for.
  2344. *
  2345. * This function is used in conjunction with ring_buffer_read_page.
  2346. * When reading a full page from the ring buffer, these functions
  2347. * can be used to speed up the process. The calling function should
  2348. * allocate a few pages first with this function. Then when it
  2349. * needs to get pages from the ring buffer, it passes the result
  2350. * of this function into ring_buffer_read_page, which will swap
  2351. * the page that was allocated, with the read page of the buffer.
  2352. *
  2353. * Returns:
  2354. * The page allocated, or NULL on error.
  2355. */
  2356. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
  2357. {
  2358. struct buffer_data_page *bpage;
  2359. unsigned long addr;
  2360. addr = __get_free_page(GFP_KERNEL);
  2361. if (!addr)
  2362. return NULL;
  2363. bpage = (void *)addr;
  2364. rb_init_page(bpage);
  2365. return bpage;
  2366. }
  2367. EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  2368. /**
  2369. * ring_buffer_free_read_page - free an allocated read page
  2370. * @buffer: the buffer the page was allocate for
  2371. * @data: the page to free
  2372. *
  2373. * Free a page allocated from ring_buffer_alloc_read_page.
  2374. */
  2375. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  2376. {
  2377. free_page((unsigned long)data);
  2378. }
  2379. EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
  2380. /**
  2381. * ring_buffer_read_page - extract a page from the ring buffer
  2382. * @buffer: buffer to extract from
  2383. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  2384. * @len: amount to extract
  2385. * @cpu: the cpu of the buffer to extract
  2386. * @full: should the extraction only happen when the page is full.
  2387. *
  2388. * This function will pull out a page from the ring buffer and consume it.
  2389. * @data_page must be the address of the variable that was returned
  2390. * from ring_buffer_alloc_read_page. This is because the page might be used
  2391. * to swap with a page in the ring buffer.
  2392. *
  2393. * for example:
  2394. * rpage = ring_buffer_alloc_read_page(buffer);
  2395. * if (!rpage)
  2396. * return error;
  2397. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  2398. * if (ret >= 0)
  2399. * process_page(rpage, ret);
  2400. *
  2401. * When @full is set, the function will not return true unless
  2402. * the writer is off the reader page.
  2403. *
  2404. * Note: it is up to the calling functions to handle sleeps and wakeups.
  2405. * The ring buffer can be used anywhere in the kernel and can not
  2406. * blindly call wake_up. The layer that uses the ring buffer must be
  2407. * responsible for that.
  2408. *
  2409. * Returns:
  2410. * >=0 if data has been transferred, returns the offset of consumed data.
  2411. * <0 if no data has been transferred.
  2412. */
  2413. int ring_buffer_read_page(struct ring_buffer *buffer,
  2414. void **data_page, size_t len, int cpu, int full)
  2415. {
  2416. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2417. struct ring_buffer_event *event;
  2418. struct buffer_data_page *bpage;
  2419. struct buffer_page *reader;
  2420. unsigned long flags;
  2421. unsigned int commit;
  2422. unsigned int read;
  2423. u64 save_timestamp;
  2424. int ret = -1;
  2425. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2426. goto out;
  2427. /*
  2428. * If len is not big enough to hold the page header, then
  2429. * we can not copy anything.
  2430. */
  2431. if (len <= BUF_PAGE_HDR_SIZE)
  2432. goto out;
  2433. len -= BUF_PAGE_HDR_SIZE;
  2434. if (!data_page)
  2435. goto out;
  2436. bpage = *data_page;
  2437. if (!bpage)
  2438. goto out;
  2439. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2440. reader = rb_get_reader_page(cpu_buffer);
  2441. if (!reader)
  2442. goto out_unlock;
  2443. event = rb_reader_event(cpu_buffer);
  2444. read = reader->read;
  2445. commit = rb_page_commit(reader);
  2446. /*
  2447. * If this page has been partially read or
  2448. * if len is not big enough to read the rest of the page or
  2449. * a writer is still on the page, then
  2450. * we must copy the data from the page to the buffer.
  2451. * Otherwise, we can simply swap the page with the one passed in.
  2452. */
  2453. if (read || (len < (commit - read)) ||
  2454. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  2455. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  2456. unsigned int rpos = read;
  2457. unsigned int pos = 0;
  2458. unsigned int size;
  2459. if (full)
  2460. goto out_unlock;
  2461. if (len > (commit - read))
  2462. len = (commit - read);
  2463. size = rb_event_length(event);
  2464. if (len < size)
  2465. goto out_unlock;
  2466. /* save the current timestamp, since the user will need it */
  2467. save_timestamp = cpu_buffer->read_stamp;
  2468. /* Need to copy one event at a time */
  2469. do {
  2470. memcpy(bpage->data + pos, rpage->data + rpos, size);
  2471. len -= size;
  2472. rb_advance_reader(cpu_buffer);
  2473. rpos = reader->read;
  2474. pos += size;
  2475. event = rb_reader_event(cpu_buffer);
  2476. size = rb_event_length(event);
  2477. } while (len > size);
  2478. /* update bpage */
  2479. local_set(&bpage->commit, pos);
  2480. bpage->time_stamp = save_timestamp;
  2481. /* we copied everything to the beginning */
  2482. read = 0;
  2483. } else {
  2484. /* swap the pages */
  2485. rb_init_page(bpage);
  2486. bpage = reader->page;
  2487. reader->page = *data_page;
  2488. local_set(&reader->write, 0);
  2489. reader->read = 0;
  2490. *data_page = bpage;
  2491. /* update the entry counter */
  2492. rb_remove_entries(cpu_buffer, bpage, read);
  2493. }
  2494. ret = read;
  2495. out_unlock:
  2496. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2497. out:
  2498. return ret;
  2499. }
  2500. EXPORT_SYMBOL_GPL(ring_buffer_read_page);
  2501. static ssize_t
  2502. rb_simple_read(struct file *filp, char __user *ubuf,
  2503. size_t cnt, loff_t *ppos)
  2504. {
  2505. unsigned long *p = filp->private_data;
  2506. char buf[64];
  2507. int r;
  2508. if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
  2509. r = sprintf(buf, "permanently disabled\n");
  2510. else
  2511. r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
  2512. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2513. }
  2514. static ssize_t
  2515. rb_simple_write(struct file *filp, const char __user *ubuf,
  2516. size_t cnt, loff_t *ppos)
  2517. {
  2518. unsigned long *p = filp->private_data;
  2519. char buf[64];
  2520. unsigned long val;
  2521. int ret;
  2522. if (cnt >= sizeof(buf))
  2523. return -EINVAL;
  2524. if (copy_from_user(&buf, ubuf, cnt))
  2525. return -EFAULT;
  2526. buf[cnt] = 0;
  2527. ret = strict_strtoul(buf, 10, &val);
  2528. if (ret < 0)
  2529. return ret;
  2530. if (val)
  2531. set_bit(RB_BUFFERS_ON_BIT, p);
  2532. else
  2533. clear_bit(RB_BUFFERS_ON_BIT, p);
  2534. (*ppos)++;
  2535. return cnt;
  2536. }
  2537. static const struct file_operations rb_simple_fops = {
  2538. .open = tracing_open_generic,
  2539. .read = rb_simple_read,
  2540. .write = rb_simple_write,
  2541. };
  2542. static __init int rb_init_debugfs(void)
  2543. {
  2544. struct dentry *d_tracer;
  2545. d_tracer = tracing_init_dentry();
  2546. trace_create_file("tracing_on", 0644, d_tracer,
  2547. &ring_buffer_flags, &rb_simple_fops);
  2548. return 0;
  2549. }
  2550. fs_initcall(rb_init_debugfs);
  2551. #ifdef CONFIG_HOTPLUG_CPU
  2552. static int rb_cpu_notify(struct notifier_block *self,
  2553. unsigned long action, void *hcpu)
  2554. {
  2555. struct ring_buffer *buffer =
  2556. container_of(self, struct ring_buffer, cpu_notify);
  2557. long cpu = (long)hcpu;
  2558. switch (action) {
  2559. case CPU_UP_PREPARE:
  2560. case CPU_UP_PREPARE_FROZEN:
  2561. if (cpu_isset(cpu, *buffer->cpumask))
  2562. return NOTIFY_OK;
  2563. buffer->buffers[cpu] =
  2564. rb_allocate_cpu_buffer(buffer, cpu);
  2565. if (!buffer->buffers[cpu]) {
  2566. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  2567. cpu);
  2568. return NOTIFY_OK;
  2569. }
  2570. smp_wmb();
  2571. cpu_set(cpu, *buffer->cpumask);
  2572. break;
  2573. case CPU_DOWN_PREPARE:
  2574. case CPU_DOWN_PREPARE_FROZEN:
  2575. /*
  2576. * Do nothing.
  2577. * If we were to free the buffer, then the user would
  2578. * lose any trace that was in the buffer.
  2579. */
  2580. break;
  2581. default:
  2582. break;
  2583. }
  2584. return NOTIFY_OK;
  2585. }
  2586. #endif