ring_buffer.c 77 KB

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