ring_buffer.c 79 KB

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