ring_buffer.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870
  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. #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
  189. #define RB_ALIGNMENT 4U
  190. #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  191. #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
  192. /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
  193. #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
  194. enum {
  195. RB_LEN_TIME_EXTEND = 8,
  196. RB_LEN_TIME_STAMP = 16,
  197. };
  198. static inline int rb_null_event(struct ring_buffer_event *event)
  199. {
  200. return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
  201. }
  202. static void rb_event_set_padding(struct ring_buffer_event *event)
  203. {
  204. /* padding has a NULL time_delta */
  205. event->type_len = RINGBUF_TYPE_PADDING;
  206. event->time_delta = 0;
  207. }
  208. static unsigned
  209. rb_event_data_length(struct ring_buffer_event *event)
  210. {
  211. unsigned length;
  212. if (event->type_len)
  213. length = event->type_len * RB_ALIGNMENT;
  214. else
  215. length = event->array[0];
  216. return length + RB_EVNT_HDR_SIZE;
  217. }
  218. /* inline for ring buffer fast paths */
  219. static unsigned
  220. rb_event_length(struct ring_buffer_event *event)
  221. {
  222. switch (event->type_len) {
  223. case RINGBUF_TYPE_PADDING:
  224. if (rb_null_event(event))
  225. /* undefined */
  226. return -1;
  227. return event->array[0] + RB_EVNT_HDR_SIZE;
  228. case RINGBUF_TYPE_TIME_EXTEND:
  229. return RB_LEN_TIME_EXTEND;
  230. case RINGBUF_TYPE_TIME_STAMP:
  231. return RB_LEN_TIME_STAMP;
  232. case RINGBUF_TYPE_DATA:
  233. return rb_event_data_length(event);
  234. default:
  235. BUG();
  236. }
  237. /* not hit */
  238. return 0;
  239. }
  240. /**
  241. * ring_buffer_event_length - return the length of the event
  242. * @event: the event to get the length of
  243. */
  244. unsigned ring_buffer_event_length(struct ring_buffer_event *event)
  245. {
  246. unsigned length = rb_event_length(event);
  247. if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  248. return length;
  249. length -= RB_EVNT_HDR_SIZE;
  250. if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
  251. length -= sizeof(event->array[0]);
  252. return length;
  253. }
  254. EXPORT_SYMBOL_GPL(ring_buffer_event_length);
  255. /* inline for ring buffer fast paths */
  256. static void *
  257. rb_event_data(struct ring_buffer_event *event)
  258. {
  259. BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
  260. /* If length is in len field, then array[0] has the data */
  261. if (event->type_len)
  262. return (void *)&event->array[0];
  263. /* Otherwise length is in array[0] and array[1] has the data */
  264. return (void *)&event->array[1];
  265. }
  266. /**
  267. * ring_buffer_event_data - return the data of the event
  268. * @event: the event to get the data from
  269. */
  270. void *ring_buffer_event_data(struct ring_buffer_event *event)
  271. {
  272. return rb_event_data(event);
  273. }
  274. EXPORT_SYMBOL_GPL(ring_buffer_event_data);
  275. #define for_each_buffer_cpu(buffer, cpu) \
  276. for_each_cpu(cpu, buffer->cpumask)
  277. #define TS_SHIFT 27
  278. #define TS_MASK ((1ULL << TS_SHIFT) - 1)
  279. #define TS_DELTA_TEST (~TS_MASK)
  280. struct buffer_data_page {
  281. u64 time_stamp; /* page time stamp */
  282. local_t commit; /* write committed index */
  283. unsigned char data[]; /* data of buffer page */
  284. };
  285. /*
  286. * Note, the buffer_page list must be first. The buffer pages
  287. * are allocated in cache lines, which means that each buffer
  288. * page will be at the beginning of a cache line, and thus
  289. * the least significant bits will be zero. We use this to
  290. * add flags in the list struct pointers, to make the ring buffer
  291. * lockless.
  292. */
  293. struct buffer_page {
  294. struct list_head list; /* list of buffer pages */
  295. local_t write; /* index for next write */
  296. unsigned read; /* index for next read */
  297. local_t entries; /* entries on this page */
  298. struct buffer_data_page *page; /* Actual data page */
  299. };
  300. /*
  301. * The buffer page counters, write and entries, must be reset
  302. * atomically when crossing page boundaries. To synchronize this
  303. * update, two counters are inserted into the number. One is
  304. * the actual counter for the write position or count on the page.
  305. *
  306. * The other is a counter of updaters. Before an update happens
  307. * the update partition of the counter is incremented. This will
  308. * allow the updater to update the counter atomically.
  309. *
  310. * The counter is 20 bits, and the state data is 12.
  311. */
  312. #define RB_WRITE_MASK 0xfffff
  313. #define RB_WRITE_INTCNT (1 << 20)
  314. static void rb_init_page(struct buffer_data_page *bpage)
  315. {
  316. local_set(&bpage->commit, 0);
  317. }
  318. /**
  319. * ring_buffer_page_len - the size of data on the page.
  320. * @page: The page to read
  321. *
  322. * Returns the amount of data on the page, including buffer page header.
  323. */
  324. size_t ring_buffer_page_len(void *page)
  325. {
  326. return local_read(&((struct buffer_data_page *)page)->commit)
  327. + BUF_PAGE_HDR_SIZE;
  328. }
  329. /*
  330. * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
  331. * this issue out.
  332. */
  333. static void free_buffer_page(struct buffer_page *bpage)
  334. {
  335. free_page((unsigned long)bpage->page);
  336. kfree(bpage);
  337. }
  338. /*
  339. * We need to fit the time_stamp delta into 27 bits.
  340. */
  341. static inline int test_time_stamp(u64 delta)
  342. {
  343. if (delta & TS_DELTA_TEST)
  344. return 1;
  345. return 0;
  346. }
  347. #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
  348. /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
  349. #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
  350. /* Max number of timestamps that can fit on a page */
  351. #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
  352. int ring_buffer_print_page_header(struct trace_seq *s)
  353. {
  354. struct buffer_data_page field;
  355. int ret;
  356. ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
  357. "offset:0;\tsize:%u;\tsigned:%u;\n",
  358. (unsigned int)sizeof(field.time_stamp),
  359. (unsigned int)is_signed_type(u64));
  360. ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
  361. "offset:%u;\tsize:%u;\tsigned:%u;\n",
  362. (unsigned int)offsetof(typeof(field), commit),
  363. (unsigned int)sizeof(field.commit),
  364. (unsigned int)is_signed_type(long));
  365. ret = trace_seq_printf(s, "\tfield: char data;\t"
  366. "offset:%u;\tsize:%u;\tsigned:%u;\n",
  367. (unsigned int)offsetof(typeof(field), data),
  368. (unsigned int)BUF_PAGE_SIZE,
  369. (unsigned int)is_signed_type(char));
  370. return ret;
  371. }
  372. /*
  373. * head_page == tail_page && head == tail then buffer is empty.
  374. */
  375. struct ring_buffer_per_cpu {
  376. int cpu;
  377. struct ring_buffer *buffer;
  378. spinlock_t reader_lock; /* serialize readers */
  379. arch_spinlock_t lock;
  380. struct lock_class_key lock_key;
  381. struct list_head *pages;
  382. struct buffer_page *head_page; /* read from head */
  383. struct buffer_page *tail_page; /* write to tail */
  384. struct buffer_page *commit_page; /* committed pages */
  385. struct buffer_page *reader_page;
  386. local_t commit_overrun;
  387. local_t overrun;
  388. local_t entries;
  389. local_t committing;
  390. local_t commits;
  391. unsigned long read;
  392. u64 write_stamp;
  393. u64 read_stamp;
  394. atomic_t record_disabled;
  395. };
  396. struct ring_buffer {
  397. unsigned pages;
  398. unsigned flags;
  399. int cpus;
  400. atomic_t record_disabled;
  401. cpumask_var_t cpumask;
  402. struct lock_class_key *reader_lock_key;
  403. struct mutex mutex;
  404. struct ring_buffer_per_cpu **buffers;
  405. #ifdef CONFIG_HOTPLUG_CPU
  406. struct notifier_block cpu_notify;
  407. #endif
  408. u64 (*clock)(void);
  409. };
  410. struct ring_buffer_iter {
  411. struct ring_buffer_per_cpu *cpu_buffer;
  412. unsigned long head;
  413. struct buffer_page *head_page;
  414. u64 read_stamp;
  415. };
  416. /* buffer may be either ring_buffer or ring_buffer_per_cpu */
  417. #define RB_WARN_ON(b, cond) \
  418. ({ \
  419. int _____ret = unlikely(cond); \
  420. if (_____ret) { \
  421. if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
  422. struct ring_buffer_per_cpu *__b = \
  423. (void *)b; \
  424. atomic_inc(&__b->buffer->record_disabled); \
  425. } else \
  426. atomic_inc(&b->record_disabled); \
  427. WARN_ON(1); \
  428. } \
  429. _____ret; \
  430. })
  431. /* Up this if you want to test the TIME_EXTENTS and normalization */
  432. #define DEBUG_SHIFT 0
  433. static inline u64 rb_time_stamp(struct ring_buffer *buffer)
  434. {
  435. /* shift to debug/test normalization and TIME_EXTENTS */
  436. return buffer->clock() << DEBUG_SHIFT;
  437. }
  438. u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
  439. {
  440. u64 time;
  441. preempt_disable_notrace();
  442. time = rb_time_stamp(buffer);
  443. preempt_enable_no_resched_notrace();
  444. return time;
  445. }
  446. EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
  447. void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
  448. int cpu, u64 *ts)
  449. {
  450. /* Just stupid testing the normalize function and deltas */
  451. *ts >>= DEBUG_SHIFT;
  452. }
  453. EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
  454. /*
  455. * Making the ring buffer lockless makes things tricky.
  456. * Although writes only happen on the CPU that they are on,
  457. * and they only need to worry about interrupts. Reads can
  458. * happen on any CPU.
  459. *
  460. * The reader page is always off the ring buffer, but when the
  461. * reader finishes with a page, it needs to swap its page with
  462. * a new one from the buffer. The reader needs to take from
  463. * the head (writes go to the tail). But if a writer is in overwrite
  464. * mode and wraps, it must push the head page forward.
  465. *
  466. * Here lies the problem.
  467. *
  468. * The reader must be careful to replace only the head page, and
  469. * not another one. As described at the top of the file in the
  470. * ASCII art, the reader sets its old page to point to the next
  471. * page after head. It then sets the page after head to point to
  472. * the old reader page. But if the writer moves the head page
  473. * during this operation, the reader could end up with the tail.
  474. *
  475. * We use cmpxchg to help prevent this race. We also do something
  476. * special with the page before head. We set the LSB to 1.
  477. *
  478. * When the writer must push the page forward, it will clear the
  479. * bit that points to the head page, move the head, and then set
  480. * the bit that points to the new head page.
  481. *
  482. * We also don't want an interrupt coming in and moving the head
  483. * page on another writer. Thus we use the second LSB to catch
  484. * that too. Thus:
  485. *
  486. * head->list->prev->next bit 1 bit 0
  487. * ------- -------
  488. * Normal page 0 0
  489. * Points to head page 0 1
  490. * New head page 1 0
  491. *
  492. * Note we can not trust the prev pointer of the head page, because:
  493. *
  494. * +----+ +-----+ +-----+
  495. * | |------>| T |---X--->| N |
  496. * | |<------| | | |
  497. * +----+ +-----+ +-----+
  498. * ^ ^ |
  499. * | +-----+ | |
  500. * +----------| R |----------+ |
  501. * | |<-----------+
  502. * +-----+
  503. *
  504. * Key: ---X--> HEAD flag set in pointer
  505. * T Tail page
  506. * R Reader page
  507. * N Next page
  508. *
  509. * (see __rb_reserve_next() to see where this happens)
  510. *
  511. * What the above shows is that the reader just swapped out
  512. * the reader page with a page in the buffer, but before it
  513. * could make the new header point back to the new page added
  514. * it was preempted by a writer. The writer moved forward onto
  515. * the new page added by the reader and is about to move forward
  516. * again.
  517. *
  518. * You can see, it is legitimate for the previous pointer of
  519. * the head (or any page) not to point back to itself. But only
  520. * temporarially.
  521. */
  522. #define RB_PAGE_NORMAL 0UL
  523. #define RB_PAGE_HEAD 1UL
  524. #define RB_PAGE_UPDATE 2UL
  525. #define RB_FLAG_MASK 3UL
  526. /* PAGE_MOVED is not part of the mask */
  527. #define RB_PAGE_MOVED 4UL
  528. /*
  529. * rb_list_head - remove any bit
  530. */
  531. static struct list_head *rb_list_head(struct list_head *list)
  532. {
  533. unsigned long val = (unsigned long)list;
  534. return (struct list_head *)(val & ~RB_FLAG_MASK);
  535. }
  536. /*
  537. * rb_is_head_page - test if the given page is the head page
  538. *
  539. * Because the reader may move the head_page pointer, we can
  540. * not trust what the head page is (it may be pointing to
  541. * the reader page). But if the next page is a header page,
  542. * its flags will be non zero.
  543. */
  544. static int inline
  545. rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
  546. struct buffer_page *page, struct list_head *list)
  547. {
  548. unsigned long val;
  549. val = (unsigned long)list->next;
  550. if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
  551. return RB_PAGE_MOVED;
  552. return val & RB_FLAG_MASK;
  553. }
  554. /*
  555. * rb_is_reader_page
  556. *
  557. * The unique thing about the reader page, is that, if the
  558. * writer is ever on it, the previous pointer never points
  559. * back to the reader page.
  560. */
  561. static int rb_is_reader_page(struct buffer_page *page)
  562. {
  563. struct list_head *list = page->list.prev;
  564. return rb_list_head(list->next) != &page->list;
  565. }
  566. /*
  567. * rb_set_list_to_head - set a list_head to be pointing to head.
  568. */
  569. static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
  570. struct list_head *list)
  571. {
  572. unsigned long *ptr;
  573. ptr = (unsigned long *)&list->next;
  574. *ptr |= RB_PAGE_HEAD;
  575. *ptr &= ~RB_PAGE_UPDATE;
  576. }
  577. /*
  578. * rb_head_page_activate - sets up head page
  579. */
  580. static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
  581. {
  582. struct buffer_page *head;
  583. head = cpu_buffer->head_page;
  584. if (!head)
  585. return;
  586. /*
  587. * Set the previous list pointer to have the HEAD flag.
  588. */
  589. rb_set_list_to_head(cpu_buffer, head->list.prev);
  590. }
  591. static void rb_list_head_clear(struct list_head *list)
  592. {
  593. unsigned long *ptr = (unsigned long *)&list->next;
  594. *ptr &= ~RB_FLAG_MASK;
  595. }
  596. /*
  597. * rb_head_page_dactivate - clears head page ptr (for free list)
  598. */
  599. static void
  600. rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
  601. {
  602. struct list_head *hd;
  603. /* Go through the whole list and clear any pointers found. */
  604. rb_list_head_clear(cpu_buffer->pages);
  605. list_for_each(hd, cpu_buffer->pages)
  606. rb_list_head_clear(hd);
  607. }
  608. static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
  609. struct buffer_page *head,
  610. struct buffer_page *prev,
  611. int old_flag, int new_flag)
  612. {
  613. struct list_head *list;
  614. unsigned long val = (unsigned long)&head->list;
  615. unsigned long ret;
  616. list = &prev->list;
  617. val &= ~RB_FLAG_MASK;
  618. ret = cmpxchg((unsigned long *)&list->next,
  619. val | old_flag, val | new_flag);
  620. /* check if the reader took the page */
  621. if ((ret & ~RB_FLAG_MASK) != val)
  622. return RB_PAGE_MOVED;
  623. return ret & RB_FLAG_MASK;
  624. }
  625. static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
  626. struct buffer_page *head,
  627. struct buffer_page *prev,
  628. int old_flag)
  629. {
  630. return rb_head_page_set(cpu_buffer, head, prev,
  631. old_flag, RB_PAGE_UPDATE);
  632. }
  633. static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
  634. struct buffer_page *head,
  635. struct buffer_page *prev,
  636. int old_flag)
  637. {
  638. return rb_head_page_set(cpu_buffer, head, prev,
  639. old_flag, RB_PAGE_HEAD);
  640. }
  641. static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
  642. struct buffer_page *head,
  643. struct buffer_page *prev,
  644. int old_flag)
  645. {
  646. return rb_head_page_set(cpu_buffer, head, prev,
  647. old_flag, RB_PAGE_NORMAL);
  648. }
  649. static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
  650. struct buffer_page **bpage)
  651. {
  652. struct list_head *p = rb_list_head((*bpage)->list.next);
  653. *bpage = list_entry(p, struct buffer_page, list);
  654. }
  655. static struct buffer_page *
  656. rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
  657. {
  658. struct buffer_page *head;
  659. struct buffer_page *page;
  660. struct list_head *list;
  661. int i;
  662. if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
  663. return NULL;
  664. /* sanity check */
  665. list = cpu_buffer->pages;
  666. if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
  667. return NULL;
  668. page = head = cpu_buffer->head_page;
  669. /*
  670. * It is possible that the writer moves the header behind
  671. * where we started, and we miss in one loop.
  672. * A second loop should grab the header, but we'll do
  673. * three loops just because I'm paranoid.
  674. */
  675. for (i = 0; i < 3; i++) {
  676. do {
  677. if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
  678. cpu_buffer->head_page = page;
  679. return page;
  680. }
  681. rb_inc_page(cpu_buffer, &page);
  682. } while (page != head);
  683. }
  684. RB_WARN_ON(cpu_buffer, 1);
  685. return NULL;
  686. }
  687. static int rb_head_page_replace(struct buffer_page *old,
  688. struct buffer_page *new)
  689. {
  690. unsigned long *ptr = (unsigned long *)&old->list.prev->next;
  691. unsigned long val;
  692. unsigned long ret;
  693. val = *ptr & ~RB_FLAG_MASK;
  694. val |= RB_PAGE_HEAD;
  695. ret = cmpxchg(ptr, val, (unsigned long)&new->list);
  696. return ret == val;
  697. }
  698. /*
  699. * rb_tail_page_update - move the tail page forward
  700. *
  701. * Returns 1 if moved tail page, 0 if someone else did.
  702. */
  703. static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
  704. struct buffer_page *tail_page,
  705. struct buffer_page *next_page)
  706. {
  707. struct buffer_page *old_tail;
  708. unsigned long old_entries;
  709. unsigned long old_write;
  710. int ret = 0;
  711. /*
  712. * The tail page now needs to be moved forward.
  713. *
  714. * We need to reset the tail page, but without messing
  715. * with possible erasing of data brought in by interrupts
  716. * that have moved the tail page and are currently on it.
  717. *
  718. * We add a counter to the write field to denote this.
  719. */
  720. old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
  721. old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
  722. /*
  723. * Just make sure we have seen our old_write and synchronize
  724. * with any interrupts that come in.
  725. */
  726. barrier();
  727. /*
  728. * If the tail page is still the same as what we think
  729. * it is, then it is up to us to update the tail
  730. * pointer.
  731. */
  732. if (tail_page == cpu_buffer->tail_page) {
  733. /* Zero the write counter */
  734. unsigned long val = old_write & ~RB_WRITE_MASK;
  735. unsigned long eval = old_entries & ~RB_WRITE_MASK;
  736. /*
  737. * This will only succeed if an interrupt did
  738. * not come in and change it. In which case, we
  739. * do not want to modify it.
  740. *
  741. * We add (void) to let the compiler know that we do not care
  742. * about the return value of these functions. We use the
  743. * cmpxchg to only update if an interrupt did not already
  744. * do it for us. If the cmpxchg fails, we don't care.
  745. */
  746. (void)local_cmpxchg(&next_page->write, old_write, val);
  747. (void)local_cmpxchg(&next_page->entries, old_entries, eval);
  748. /*
  749. * No need to worry about races with clearing out the commit.
  750. * it only can increment when a commit takes place. But that
  751. * only happens in the outer most nested commit.
  752. */
  753. local_set(&next_page->page->commit, 0);
  754. old_tail = cmpxchg(&cpu_buffer->tail_page,
  755. tail_page, next_page);
  756. if (old_tail == tail_page)
  757. ret = 1;
  758. }
  759. return ret;
  760. }
  761. static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
  762. struct buffer_page *bpage)
  763. {
  764. unsigned long val = (unsigned long)bpage;
  765. if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
  766. return 1;
  767. return 0;
  768. }
  769. /**
  770. * rb_check_list - make sure a pointer to a list has the last bits zero
  771. */
  772. static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
  773. struct list_head *list)
  774. {
  775. if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
  776. return 1;
  777. if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
  778. return 1;
  779. return 0;
  780. }
  781. /**
  782. * check_pages - integrity check of buffer pages
  783. * @cpu_buffer: CPU buffer with pages to test
  784. *
  785. * As a safety measure we check to make sure the data pages have not
  786. * been corrupted.
  787. */
  788. static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  789. {
  790. struct list_head *head = cpu_buffer->pages;
  791. struct buffer_page *bpage, *tmp;
  792. rb_head_page_deactivate(cpu_buffer);
  793. if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
  794. return -1;
  795. if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
  796. return -1;
  797. if (rb_check_list(cpu_buffer, head))
  798. return -1;
  799. list_for_each_entry_safe(bpage, tmp, head, list) {
  800. if (RB_WARN_ON(cpu_buffer,
  801. bpage->list.next->prev != &bpage->list))
  802. return -1;
  803. if (RB_WARN_ON(cpu_buffer,
  804. bpage->list.prev->next != &bpage->list))
  805. return -1;
  806. if (rb_check_list(cpu_buffer, &bpage->list))
  807. return -1;
  808. }
  809. rb_head_page_activate(cpu_buffer);
  810. return 0;
  811. }
  812. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  813. unsigned nr_pages)
  814. {
  815. struct buffer_page *bpage, *tmp;
  816. unsigned long addr;
  817. LIST_HEAD(pages);
  818. unsigned i;
  819. WARN_ON(!nr_pages);
  820. for (i = 0; i < nr_pages; i++) {
  821. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  822. GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
  823. if (!bpage)
  824. goto free_pages;
  825. rb_check_bpage(cpu_buffer, bpage);
  826. list_add(&bpage->list, &pages);
  827. addr = __get_free_page(GFP_KERNEL);
  828. if (!addr)
  829. goto free_pages;
  830. bpage->page = (void *)addr;
  831. rb_init_page(bpage->page);
  832. }
  833. /*
  834. * The ring buffer page list is a circular list that does not
  835. * start and end with a list head. All page list items point to
  836. * other pages.
  837. */
  838. cpu_buffer->pages = pages.next;
  839. list_del(&pages);
  840. rb_check_pages(cpu_buffer);
  841. return 0;
  842. free_pages:
  843. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  844. list_del_init(&bpage->list);
  845. free_buffer_page(bpage);
  846. }
  847. return -ENOMEM;
  848. }
  849. static struct ring_buffer_per_cpu *
  850. rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
  851. {
  852. struct ring_buffer_per_cpu *cpu_buffer;
  853. struct buffer_page *bpage;
  854. unsigned long addr;
  855. int ret;
  856. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  857. GFP_KERNEL, cpu_to_node(cpu));
  858. if (!cpu_buffer)
  859. return NULL;
  860. cpu_buffer->cpu = cpu;
  861. cpu_buffer->buffer = buffer;
  862. spin_lock_init(&cpu_buffer->reader_lock);
  863. lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
  864. cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  865. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  866. GFP_KERNEL, cpu_to_node(cpu));
  867. if (!bpage)
  868. goto fail_free_buffer;
  869. rb_check_bpage(cpu_buffer, bpage);
  870. cpu_buffer->reader_page = bpage;
  871. addr = __get_free_page(GFP_KERNEL);
  872. if (!addr)
  873. goto fail_free_reader;
  874. bpage->page = (void *)addr;
  875. rb_init_page(bpage->page);
  876. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  877. ret = rb_allocate_pages(cpu_buffer, buffer->pages);
  878. if (ret < 0)
  879. goto fail_free_reader;
  880. cpu_buffer->head_page
  881. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  882. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  883. rb_head_page_activate(cpu_buffer);
  884. return cpu_buffer;
  885. fail_free_reader:
  886. free_buffer_page(cpu_buffer->reader_page);
  887. fail_free_buffer:
  888. kfree(cpu_buffer);
  889. return NULL;
  890. }
  891. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  892. {
  893. struct list_head *head = cpu_buffer->pages;
  894. struct buffer_page *bpage, *tmp;
  895. free_buffer_page(cpu_buffer->reader_page);
  896. rb_head_page_deactivate(cpu_buffer);
  897. if (head) {
  898. list_for_each_entry_safe(bpage, tmp, head, list) {
  899. list_del_init(&bpage->list);
  900. free_buffer_page(bpage);
  901. }
  902. bpage = list_entry(head, struct buffer_page, list);
  903. free_buffer_page(bpage);
  904. }
  905. kfree(cpu_buffer);
  906. }
  907. #ifdef CONFIG_HOTPLUG_CPU
  908. static int rb_cpu_notify(struct notifier_block *self,
  909. unsigned long action, void *hcpu);
  910. #endif
  911. /**
  912. * ring_buffer_alloc - allocate a new ring_buffer
  913. * @size: the size in bytes per cpu that is needed.
  914. * @flags: attributes to set for the ring buffer.
  915. *
  916. * Currently the only flag that is available is the RB_FL_OVERWRITE
  917. * flag. This flag means that the buffer will overwrite old data
  918. * when the buffer wraps. If this flag is not set, the buffer will
  919. * drop data when the tail hits the head.
  920. */
  921. struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
  922. struct lock_class_key *key)
  923. {
  924. struct ring_buffer *buffer;
  925. int bsize;
  926. int cpu;
  927. /* keep it in its own cache line */
  928. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  929. GFP_KERNEL);
  930. if (!buffer)
  931. return NULL;
  932. if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  933. goto fail_free_buffer;
  934. buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  935. buffer->flags = flags;
  936. buffer->clock = trace_clock_local;
  937. buffer->reader_lock_key = key;
  938. /* need at least two pages */
  939. if (buffer->pages < 2)
  940. buffer->pages = 2;
  941. /*
  942. * In case of non-hotplug cpu, if the ring-buffer is allocated
  943. * in early initcall, it will not be notified of secondary cpus.
  944. * In that off case, we need to allocate for all possible cpus.
  945. */
  946. #ifdef CONFIG_HOTPLUG_CPU
  947. get_online_cpus();
  948. cpumask_copy(buffer->cpumask, cpu_online_mask);
  949. #else
  950. cpumask_copy(buffer->cpumask, cpu_possible_mask);
  951. #endif
  952. buffer->cpus = nr_cpu_ids;
  953. bsize = sizeof(void *) * nr_cpu_ids;
  954. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  955. GFP_KERNEL);
  956. if (!buffer->buffers)
  957. goto fail_free_cpumask;
  958. for_each_buffer_cpu(buffer, cpu) {
  959. buffer->buffers[cpu] =
  960. rb_allocate_cpu_buffer(buffer, cpu);
  961. if (!buffer->buffers[cpu])
  962. goto fail_free_buffers;
  963. }
  964. #ifdef CONFIG_HOTPLUG_CPU
  965. buffer->cpu_notify.notifier_call = rb_cpu_notify;
  966. buffer->cpu_notify.priority = 0;
  967. register_cpu_notifier(&buffer->cpu_notify);
  968. #endif
  969. put_online_cpus();
  970. mutex_init(&buffer->mutex);
  971. return buffer;
  972. fail_free_buffers:
  973. for_each_buffer_cpu(buffer, cpu) {
  974. if (buffer->buffers[cpu])
  975. rb_free_cpu_buffer(buffer->buffers[cpu]);
  976. }
  977. kfree(buffer->buffers);
  978. fail_free_cpumask:
  979. free_cpumask_var(buffer->cpumask);
  980. put_online_cpus();
  981. fail_free_buffer:
  982. kfree(buffer);
  983. return NULL;
  984. }
  985. EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
  986. /**
  987. * ring_buffer_free - free a ring buffer.
  988. * @buffer: the buffer to free.
  989. */
  990. void
  991. ring_buffer_free(struct ring_buffer *buffer)
  992. {
  993. int cpu;
  994. get_online_cpus();
  995. #ifdef CONFIG_HOTPLUG_CPU
  996. unregister_cpu_notifier(&buffer->cpu_notify);
  997. #endif
  998. for_each_buffer_cpu(buffer, cpu)
  999. rb_free_cpu_buffer(buffer->buffers[cpu]);
  1000. put_online_cpus();
  1001. kfree(buffer->buffers);
  1002. free_cpumask_var(buffer->cpumask);
  1003. kfree(buffer);
  1004. }
  1005. EXPORT_SYMBOL_GPL(ring_buffer_free);
  1006. void ring_buffer_set_clock(struct ring_buffer *buffer,
  1007. u64 (*clock)(void))
  1008. {
  1009. buffer->clock = clock;
  1010. }
  1011. static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
  1012. static void
  1013. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
  1014. {
  1015. struct buffer_page *bpage;
  1016. struct list_head *p;
  1017. unsigned i;
  1018. spin_lock_irq(&cpu_buffer->reader_lock);
  1019. rb_head_page_deactivate(cpu_buffer);
  1020. for (i = 0; i < nr_pages; i++) {
  1021. if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
  1022. return;
  1023. p = cpu_buffer->pages->next;
  1024. bpage = list_entry(p, struct buffer_page, list);
  1025. list_del_init(&bpage->list);
  1026. free_buffer_page(bpage);
  1027. }
  1028. if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
  1029. return;
  1030. rb_reset_cpu(cpu_buffer);
  1031. rb_check_pages(cpu_buffer);
  1032. spin_unlock_irq(&cpu_buffer->reader_lock);
  1033. }
  1034. static void
  1035. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
  1036. struct list_head *pages, unsigned nr_pages)
  1037. {
  1038. struct buffer_page *bpage;
  1039. struct list_head *p;
  1040. unsigned i;
  1041. spin_lock_irq(&cpu_buffer->reader_lock);
  1042. rb_head_page_deactivate(cpu_buffer);
  1043. for (i = 0; i < nr_pages; i++) {
  1044. if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
  1045. return;
  1046. p = pages->next;
  1047. bpage = list_entry(p, struct buffer_page, list);
  1048. list_del_init(&bpage->list);
  1049. list_add_tail(&bpage->list, cpu_buffer->pages);
  1050. }
  1051. rb_reset_cpu(cpu_buffer);
  1052. rb_check_pages(cpu_buffer);
  1053. spin_unlock_irq(&cpu_buffer->reader_lock);
  1054. }
  1055. /**
  1056. * ring_buffer_resize - resize the ring buffer
  1057. * @buffer: the buffer to resize.
  1058. * @size: the new size.
  1059. *
  1060. * Minimum size is 2 * BUF_PAGE_SIZE.
  1061. *
  1062. * Returns -1 on failure.
  1063. */
  1064. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
  1065. {
  1066. struct ring_buffer_per_cpu *cpu_buffer;
  1067. unsigned nr_pages, rm_pages, new_pages;
  1068. struct buffer_page *bpage, *tmp;
  1069. unsigned long buffer_size;
  1070. unsigned long addr;
  1071. LIST_HEAD(pages);
  1072. int i, cpu;
  1073. /*
  1074. * Always succeed at resizing a non-existent buffer:
  1075. */
  1076. if (!buffer)
  1077. return size;
  1078. size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  1079. size *= BUF_PAGE_SIZE;
  1080. buffer_size = buffer->pages * BUF_PAGE_SIZE;
  1081. /* we need a minimum of two pages */
  1082. if (size < BUF_PAGE_SIZE * 2)
  1083. size = BUF_PAGE_SIZE * 2;
  1084. if (size == buffer_size)
  1085. return size;
  1086. atomic_inc(&buffer->record_disabled);
  1087. /* Make sure all writers are done with this buffer. */
  1088. synchronize_sched();
  1089. mutex_lock(&buffer->mutex);
  1090. get_online_cpus();
  1091. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  1092. if (size < buffer_size) {
  1093. /* easy case, just free pages */
  1094. if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
  1095. goto out_fail;
  1096. rm_pages = buffer->pages - nr_pages;
  1097. for_each_buffer_cpu(buffer, cpu) {
  1098. cpu_buffer = buffer->buffers[cpu];
  1099. rb_remove_pages(cpu_buffer, rm_pages);
  1100. }
  1101. goto out;
  1102. }
  1103. /*
  1104. * This is a bit more difficult. We only want to add pages
  1105. * when we can allocate enough for all CPUs. We do this
  1106. * by allocating all the pages and storing them on a local
  1107. * link list. If we succeed in our allocation, then we
  1108. * add these pages to the cpu_buffers. Otherwise we just free
  1109. * them all and return -ENOMEM;
  1110. */
  1111. if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
  1112. goto out_fail;
  1113. new_pages = nr_pages - buffer->pages;
  1114. for_each_buffer_cpu(buffer, cpu) {
  1115. for (i = 0; i < new_pages; i++) {
  1116. bpage = kzalloc_node(ALIGN(sizeof(*bpage),
  1117. cache_line_size()),
  1118. GFP_KERNEL, cpu_to_node(cpu));
  1119. if (!bpage)
  1120. goto free_pages;
  1121. list_add(&bpage->list, &pages);
  1122. addr = __get_free_page(GFP_KERNEL);
  1123. if (!addr)
  1124. goto free_pages;
  1125. bpage->page = (void *)addr;
  1126. rb_init_page(bpage->page);
  1127. }
  1128. }
  1129. for_each_buffer_cpu(buffer, cpu) {
  1130. cpu_buffer = buffer->buffers[cpu];
  1131. rb_insert_pages(cpu_buffer, &pages, new_pages);
  1132. }
  1133. if (RB_WARN_ON(buffer, !list_empty(&pages)))
  1134. goto out_fail;
  1135. out:
  1136. buffer->pages = nr_pages;
  1137. put_online_cpus();
  1138. mutex_unlock(&buffer->mutex);
  1139. atomic_dec(&buffer->record_disabled);
  1140. return size;
  1141. free_pages:
  1142. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  1143. list_del_init(&bpage->list);
  1144. free_buffer_page(bpage);
  1145. }
  1146. put_online_cpus();
  1147. mutex_unlock(&buffer->mutex);
  1148. atomic_dec(&buffer->record_disabled);
  1149. return -ENOMEM;
  1150. /*
  1151. * Something went totally wrong, and we are too paranoid
  1152. * to even clean up the mess.
  1153. */
  1154. out_fail:
  1155. put_online_cpus();
  1156. mutex_unlock(&buffer->mutex);
  1157. atomic_dec(&buffer->record_disabled);
  1158. return -1;
  1159. }
  1160. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  1161. static inline void *
  1162. __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
  1163. {
  1164. return bpage->data + index;
  1165. }
  1166. static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  1167. {
  1168. return bpage->page->data + index;
  1169. }
  1170. static inline struct ring_buffer_event *
  1171. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  1172. {
  1173. return __rb_page_index(cpu_buffer->reader_page,
  1174. cpu_buffer->reader_page->read);
  1175. }
  1176. static inline struct ring_buffer_event *
  1177. rb_iter_head_event(struct ring_buffer_iter *iter)
  1178. {
  1179. return __rb_page_index(iter->head_page, iter->head);
  1180. }
  1181. static inline unsigned long rb_page_write(struct buffer_page *bpage)
  1182. {
  1183. return local_read(&bpage->write) & RB_WRITE_MASK;
  1184. }
  1185. static inline unsigned rb_page_commit(struct buffer_page *bpage)
  1186. {
  1187. return local_read(&bpage->page->commit);
  1188. }
  1189. static inline unsigned long rb_page_entries(struct buffer_page *bpage)
  1190. {
  1191. return local_read(&bpage->entries) & RB_WRITE_MASK;
  1192. }
  1193. /* Size is determined by what has been commited */
  1194. static inline unsigned rb_page_size(struct buffer_page *bpage)
  1195. {
  1196. return rb_page_commit(bpage);
  1197. }
  1198. static inline unsigned
  1199. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  1200. {
  1201. return rb_page_commit(cpu_buffer->commit_page);
  1202. }
  1203. static inline unsigned
  1204. rb_event_index(struct ring_buffer_event *event)
  1205. {
  1206. unsigned long addr = (unsigned long)event;
  1207. return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
  1208. }
  1209. static inline int
  1210. rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1211. struct ring_buffer_event *event)
  1212. {
  1213. unsigned long addr = (unsigned long)event;
  1214. unsigned long index;
  1215. index = rb_event_index(event);
  1216. addr &= PAGE_MASK;
  1217. return cpu_buffer->commit_page->page == (void *)addr &&
  1218. rb_commit_index(cpu_buffer) == index;
  1219. }
  1220. static void
  1221. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  1222. {
  1223. unsigned long max_count;
  1224. /*
  1225. * We only race with interrupts and NMIs on this CPU.
  1226. * If we own the commit event, then we can commit
  1227. * all others that interrupted us, since the interruptions
  1228. * are in stack format (they finish before they come
  1229. * back to us). This allows us to do a simple loop to
  1230. * assign the commit to the tail.
  1231. */
  1232. again:
  1233. max_count = cpu_buffer->buffer->pages * 100;
  1234. while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
  1235. if (RB_WARN_ON(cpu_buffer, !(--max_count)))
  1236. return;
  1237. if (RB_WARN_ON(cpu_buffer,
  1238. rb_is_reader_page(cpu_buffer->tail_page)))
  1239. return;
  1240. local_set(&cpu_buffer->commit_page->page->commit,
  1241. rb_page_write(cpu_buffer->commit_page));
  1242. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  1243. cpu_buffer->write_stamp =
  1244. cpu_buffer->commit_page->page->time_stamp;
  1245. /* add barrier to keep gcc from optimizing too much */
  1246. barrier();
  1247. }
  1248. while (rb_commit_index(cpu_buffer) !=
  1249. rb_page_write(cpu_buffer->commit_page)) {
  1250. local_set(&cpu_buffer->commit_page->page->commit,
  1251. rb_page_write(cpu_buffer->commit_page));
  1252. RB_WARN_ON(cpu_buffer,
  1253. local_read(&cpu_buffer->commit_page->page->commit) &
  1254. ~RB_WRITE_MASK);
  1255. barrier();
  1256. }
  1257. /* again, keep gcc from optimizing */
  1258. barrier();
  1259. /*
  1260. * If an interrupt came in just after the first while loop
  1261. * and pushed the tail page forward, we will be left with
  1262. * a dangling commit that will never go forward.
  1263. */
  1264. if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
  1265. goto again;
  1266. }
  1267. static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  1268. {
  1269. cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
  1270. cpu_buffer->reader_page->read = 0;
  1271. }
  1272. static void rb_inc_iter(struct ring_buffer_iter *iter)
  1273. {
  1274. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1275. /*
  1276. * The iterator could be on the reader page (it starts there).
  1277. * But the head could have moved, since the reader was
  1278. * found. Check for this case and assign the iterator
  1279. * to the head page instead of next.
  1280. */
  1281. if (iter->head_page == cpu_buffer->reader_page)
  1282. iter->head_page = rb_set_head_page(cpu_buffer);
  1283. else
  1284. rb_inc_page(cpu_buffer, &iter->head_page);
  1285. iter->read_stamp = iter->head_page->page->time_stamp;
  1286. iter->head = 0;
  1287. }
  1288. /**
  1289. * ring_buffer_update_event - update event type and data
  1290. * @event: the even to update
  1291. * @type: the type of event
  1292. * @length: the size of the event field in the ring buffer
  1293. *
  1294. * Update the type and data fields of the event. The length
  1295. * is the actual size that is written to the ring buffer,
  1296. * and with this, we can determine what to place into the
  1297. * data field.
  1298. */
  1299. static void
  1300. rb_update_event(struct ring_buffer_event *event,
  1301. unsigned type, unsigned length)
  1302. {
  1303. event->type_len = type;
  1304. switch (type) {
  1305. case RINGBUF_TYPE_PADDING:
  1306. case RINGBUF_TYPE_TIME_EXTEND:
  1307. case RINGBUF_TYPE_TIME_STAMP:
  1308. break;
  1309. case 0:
  1310. length -= RB_EVNT_HDR_SIZE;
  1311. if (length > RB_MAX_SMALL_DATA)
  1312. event->array[0] = length;
  1313. else
  1314. event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  1315. break;
  1316. default:
  1317. BUG();
  1318. }
  1319. }
  1320. /*
  1321. * rb_handle_head_page - writer hit the head page
  1322. *
  1323. * Returns: +1 to retry page
  1324. * 0 to continue
  1325. * -1 on error
  1326. */
  1327. static int
  1328. rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
  1329. struct buffer_page *tail_page,
  1330. struct buffer_page *next_page)
  1331. {
  1332. struct buffer_page *new_head;
  1333. int entries;
  1334. int type;
  1335. int ret;
  1336. entries = rb_page_entries(next_page);
  1337. /*
  1338. * The hard part is here. We need to move the head
  1339. * forward, and protect against both readers on
  1340. * other CPUs and writers coming in via interrupts.
  1341. */
  1342. type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
  1343. RB_PAGE_HEAD);
  1344. /*
  1345. * type can be one of four:
  1346. * NORMAL - an interrupt already moved it for us
  1347. * HEAD - we are the first to get here.
  1348. * UPDATE - we are the interrupt interrupting
  1349. * a current move.
  1350. * MOVED - a reader on another CPU moved the next
  1351. * pointer to its reader page. Give up
  1352. * and try again.
  1353. */
  1354. switch (type) {
  1355. case RB_PAGE_HEAD:
  1356. /*
  1357. * We changed the head to UPDATE, thus
  1358. * it is our responsibility to update
  1359. * the counters.
  1360. */
  1361. local_add(entries, &cpu_buffer->overrun);
  1362. /*
  1363. * The entries will be zeroed out when we move the
  1364. * tail page.
  1365. */
  1366. /* still more to do */
  1367. break;
  1368. case RB_PAGE_UPDATE:
  1369. /*
  1370. * This is an interrupt that interrupt the
  1371. * previous update. Still more to do.
  1372. */
  1373. break;
  1374. case RB_PAGE_NORMAL:
  1375. /*
  1376. * An interrupt came in before the update
  1377. * and processed this for us.
  1378. * Nothing left to do.
  1379. */
  1380. return 1;
  1381. case RB_PAGE_MOVED:
  1382. /*
  1383. * The reader is on another CPU and just did
  1384. * a swap with our next_page.
  1385. * Try again.
  1386. */
  1387. return 1;
  1388. default:
  1389. RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
  1390. return -1;
  1391. }
  1392. /*
  1393. * Now that we are here, the old head pointer is
  1394. * set to UPDATE. This will keep the reader from
  1395. * swapping the head page with the reader page.
  1396. * The reader (on another CPU) will spin till
  1397. * we are finished.
  1398. *
  1399. * We just need to protect against interrupts
  1400. * doing the job. We will set the next pointer
  1401. * to HEAD. After that, we set the old pointer
  1402. * to NORMAL, but only if it was HEAD before.
  1403. * otherwise we are an interrupt, and only
  1404. * want the outer most commit to reset it.
  1405. */
  1406. new_head = next_page;
  1407. rb_inc_page(cpu_buffer, &new_head);
  1408. ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
  1409. RB_PAGE_NORMAL);
  1410. /*
  1411. * Valid returns are:
  1412. * HEAD - an interrupt came in and already set it.
  1413. * NORMAL - One of two things:
  1414. * 1) We really set it.
  1415. * 2) A bunch of interrupts came in and moved
  1416. * the page forward again.
  1417. */
  1418. switch (ret) {
  1419. case RB_PAGE_HEAD:
  1420. case RB_PAGE_NORMAL:
  1421. /* OK */
  1422. break;
  1423. default:
  1424. RB_WARN_ON(cpu_buffer, 1);
  1425. return -1;
  1426. }
  1427. /*
  1428. * It is possible that an interrupt came in,
  1429. * set the head up, then more interrupts came in
  1430. * and moved it again. When we get back here,
  1431. * the page would have been set to NORMAL but we
  1432. * just set it back to HEAD.
  1433. *
  1434. * How do you detect this? Well, if that happened
  1435. * the tail page would have moved.
  1436. */
  1437. if (ret == RB_PAGE_NORMAL) {
  1438. /*
  1439. * If the tail had moved passed next, then we need
  1440. * to reset the pointer.
  1441. */
  1442. if (cpu_buffer->tail_page != tail_page &&
  1443. cpu_buffer->tail_page != next_page)
  1444. rb_head_page_set_normal(cpu_buffer, new_head,
  1445. next_page,
  1446. RB_PAGE_HEAD);
  1447. }
  1448. /*
  1449. * If this was the outer most commit (the one that
  1450. * changed the original pointer from HEAD to UPDATE),
  1451. * then it is up to us to reset it to NORMAL.
  1452. */
  1453. if (type == RB_PAGE_HEAD) {
  1454. ret = rb_head_page_set_normal(cpu_buffer, next_page,
  1455. tail_page,
  1456. RB_PAGE_UPDATE);
  1457. if (RB_WARN_ON(cpu_buffer,
  1458. ret != RB_PAGE_UPDATE))
  1459. return -1;
  1460. }
  1461. return 0;
  1462. }
  1463. static unsigned rb_calculate_event_length(unsigned length)
  1464. {
  1465. struct ring_buffer_event event; /* Used only for sizeof array */
  1466. /* zero length can cause confusions */
  1467. if (!length)
  1468. length = 1;
  1469. if (length > RB_MAX_SMALL_DATA)
  1470. length += sizeof(event.array[0]);
  1471. length += RB_EVNT_HDR_SIZE;
  1472. length = ALIGN(length, RB_ALIGNMENT);
  1473. return length;
  1474. }
  1475. static inline void
  1476. rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
  1477. struct buffer_page *tail_page,
  1478. unsigned long tail, unsigned long length)
  1479. {
  1480. struct ring_buffer_event *event;
  1481. /*
  1482. * Only the event that crossed the page boundary
  1483. * must fill the old tail_page with padding.
  1484. */
  1485. if (tail >= BUF_PAGE_SIZE) {
  1486. local_sub(length, &tail_page->write);
  1487. return;
  1488. }
  1489. event = __rb_page_index(tail_page, tail);
  1490. kmemcheck_annotate_bitfield(event, bitfield);
  1491. /*
  1492. * If this event is bigger than the minimum size, then
  1493. * we need to be careful that we don't subtract the
  1494. * write counter enough to allow another writer to slip
  1495. * in on this page.
  1496. * We put in a discarded commit instead, to make sure
  1497. * that this space is not used again.
  1498. *
  1499. * If we are less than the minimum size, we don't need to
  1500. * worry about it.
  1501. */
  1502. if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
  1503. /* No room for any events */
  1504. /* Mark the rest of the page with padding */
  1505. rb_event_set_padding(event);
  1506. /* Set the write back to the previous setting */
  1507. local_sub(length, &tail_page->write);
  1508. return;
  1509. }
  1510. /* Put in a discarded event */
  1511. event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
  1512. event->type_len = RINGBUF_TYPE_PADDING;
  1513. /* time delta must be non zero */
  1514. event->time_delta = 1;
  1515. /* Set write to end of buffer */
  1516. length = (tail + length) - BUF_PAGE_SIZE;
  1517. local_sub(length, &tail_page->write);
  1518. }
  1519. static struct ring_buffer_event *
  1520. rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
  1521. unsigned long length, unsigned long tail,
  1522. struct buffer_page *tail_page, u64 *ts)
  1523. {
  1524. struct buffer_page *commit_page = cpu_buffer->commit_page;
  1525. struct ring_buffer *buffer = cpu_buffer->buffer;
  1526. struct buffer_page *next_page;
  1527. int ret;
  1528. next_page = tail_page;
  1529. rb_inc_page(cpu_buffer, &next_page);
  1530. /*
  1531. * If for some reason, we had an interrupt storm that made
  1532. * it all the way around the buffer, bail, and warn
  1533. * about it.
  1534. */
  1535. if (unlikely(next_page == commit_page)) {
  1536. local_inc(&cpu_buffer->commit_overrun);
  1537. goto out_reset;
  1538. }
  1539. /*
  1540. * This is where the fun begins!
  1541. *
  1542. * We are fighting against races between a reader that
  1543. * could be on another CPU trying to swap its reader
  1544. * page with the buffer head.
  1545. *
  1546. * We are also fighting against interrupts coming in and
  1547. * moving the head or tail on us as well.
  1548. *
  1549. * If the next page is the head page then we have filled
  1550. * the buffer, unless the commit page is still on the
  1551. * reader page.
  1552. */
  1553. if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
  1554. /*
  1555. * If the commit is not on the reader page, then
  1556. * move the header page.
  1557. */
  1558. if (!rb_is_reader_page(cpu_buffer->commit_page)) {
  1559. /*
  1560. * If we are not in overwrite mode,
  1561. * this is easy, just stop here.
  1562. */
  1563. if (!(buffer->flags & RB_FL_OVERWRITE))
  1564. goto out_reset;
  1565. ret = rb_handle_head_page(cpu_buffer,
  1566. tail_page,
  1567. next_page);
  1568. if (ret < 0)
  1569. goto out_reset;
  1570. if (ret)
  1571. goto out_again;
  1572. } else {
  1573. /*
  1574. * We need to be careful here too. The
  1575. * commit page could still be on the reader
  1576. * page. We could have a small buffer, and
  1577. * have filled up the buffer with events
  1578. * from interrupts and such, and wrapped.
  1579. *
  1580. * Note, if the tail page is also the on the
  1581. * reader_page, we let it move out.
  1582. */
  1583. if (unlikely((cpu_buffer->commit_page !=
  1584. cpu_buffer->tail_page) &&
  1585. (cpu_buffer->commit_page ==
  1586. cpu_buffer->reader_page))) {
  1587. local_inc(&cpu_buffer->commit_overrun);
  1588. goto out_reset;
  1589. }
  1590. }
  1591. }
  1592. ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
  1593. if (ret) {
  1594. /*
  1595. * Nested commits always have zero deltas, so
  1596. * just reread the time stamp
  1597. */
  1598. *ts = rb_time_stamp(buffer);
  1599. next_page->page->time_stamp = *ts;
  1600. }
  1601. out_again:
  1602. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1603. /* fail and let the caller try again */
  1604. return ERR_PTR(-EAGAIN);
  1605. out_reset:
  1606. /* reset write */
  1607. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1608. return NULL;
  1609. }
  1610. static struct ring_buffer_event *
  1611. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  1612. unsigned type, unsigned long length, u64 *ts)
  1613. {
  1614. struct buffer_page *tail_page;
  1615. struct ring_buffer_event *event;
  1616. unsigned long tail, write;
  1617. tail_page = cpu_buffer->tail_page;
  1618. write = local_add_return(length, &tail_page->write);
  1619. /* set write to only the index of the write */
  1620. write &= RB_WRITE_MASK;
  1621. tail = write - length;
  1622. /* See if we shot pass the end of this buffer page */
  1623. if (write > BUF_PAGE_SIZE)
  1624. return rb_move_tail(cpu_buffer, length, tail,
  1625. tail_page, ts);
  1626. /* We reserved something on the buffer */
  1627. event = __rb_page_index(tail_page, tail);
  1628. kmemcheck_annotate_bitfield(event, bitfield);
  1629. rb_update_event(event, type, length);
  1630. /* The passed in type is zero for DATA */
  1631. if (likely(!type))
  1632. local_inc(&tail_page->entries);
  1633. /*
  1634. * If this is the first commit on the page, then update
  1635. * its timestamp.
  1636. */
  1637. if (!tail)
  1638. tail_page->page->time_stamp = *ts;
  1639. return event;
  1640. }
  1641. static inline int
  1642. rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
  1643. struct ring_buffer_event *event)
  1644. {
  1645. unsigned long new_index, old_index;
  1646. struct buffer_page *bpage;
  1647. unsigned long index;
  1648. unsigned long addr;
  1649. new_index = rb_event_index(event);
  1650. old_index = new_index + rb_event_length(event);
  1651. addr = (unsigned long)event;
  1652. addr &= PAGE_MASK;
  1653. bpage = cpu_buffer->tail_page;
  1654. if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
  1655. unsigned long write_mask =
  1656. local_read(&bpage->write) & ~RB_WRITE_MASK;
  1657. /*
  1658. * This is on the tail page. It is possible that
  1659. * a write could come in and move the tail page
  1660. * and write to the next page. That is fine
  1661. * because we just shorten what is on this page.
  1662. */
  1663. old_index += write_mask;
  1664. new_index += write_mask;
  1665. index = local_cmpxchg(&bpage->write, old_index, new_index);
  1666. if (index == old_index)
  1667. return 1;
  1668. }
  1669. /* could not discard */
  1670. return 0;
  1671. }
  1672. static int
  1673. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1674. u64 *ts, u64 *delta)
  1675. {
  1676. struct ring_buffer_event *event;
  1677. static int once;
  1678. int ret;
  1679. if (unlikely(*delta > (1ULL << 59) && !once++)) {
  1680. printk(KERN_WARNING "Delta way too big! %llu"
  1681. " ts=%llu write stamp = %llu\n",
  1682. (unsigned long long)*delta,
  1683. (unsigned long long)*ts,
  1684. (unsigned long long)cpu_buffer->write_stamp);
  1685. WARN_ON(1);
  1686. }
  1687. /*
  1688. * The delta is too big, we to add a
  1689. * new timestamp.
  1690. */
  1691. event = __rb_reserve_next(cpu_buffer,
  1692. RINGBUF_TYPE_TIME_EXTEND,
  1693. RB_LEN_TIME_EXTEND,
  1694. ts);
  1695. if (!event)
  1696. return -EBUSY;
  1697. if (PTR_ERR(event) == -EAGAIN)
  1698. return -EAGAIN;
  1699. /* Only a commited time event can update the write stamp */
  1700. if (rb_event_is_commit(cpu_buffer, event)) {
  1701. /*
  1702. * If this is the first on the page, then it was
  1703. * updated with the page itself. Try to discard it
  1704. * and if we can't just make it zero.
  1705. */
  1706. if (rb_event_index(event)) {
  1707. event->time_delta = *delta & TS_MASK;
  1708. event->array[0] = *delta >> TS_SHIFT;
  1709. } else {
  1710. /* try to discard, since we do not need this */
  1711. if (!rb_try_to_discard(cpu_buffer, event)) {
  1712. /* nope, just zero it */
  1713. event->time_delta = 0;
  1714. event->array[0] = 0;
  1715. }
  1716. }
  1717. cpu_buffer->write_stamp = *ts;
  1718. /* let the caller know this was the commit */
  1719. ret = 1;
  1720. } else {
  1721. /* Try to discard the event */
  1722. if (!rb_try_to_discard(cpu_buffer, event)) {
  1723. /* Darn, this is just wasted space */
  1724. event->time_delta = 0;
  1725. event->array[0] = 0;
  1726. }
  1727. ret = 0;
  1728. }
  1729. *delta = 0;
  1730. return ret;
  1731. }
  1732. static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1733. {
  1734. local_inc(&cpu_buffer->committing);
  1735. local_inc(&cpu_buffer->commits);
  1736. }
  1737. static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1738. {
  1739. unsigned long commits;
  1740. if (RB_WARN_ON(cpu_buffer,
  1741. !local_read(&cpu_buffer->committing)))
  1742. return;
  1743. again:
  1744. commits = local_read(&cpu_buffer->commits);
  1745. /* synchronize with interrupts */
  1746. barrier();
  1747. if (local_read(&cpu_buffer->committing) == 1)
  1748. rb_set_commit_to_write(cpu_buffer);
  1749. local_dec(&cpu_buffer->committing);
  1750. /* synchronize with interrupts */
  1751. barrier();
  1752. /*
  1753. * Need to account for interrupts coming in between the
  1754. * updating of the commit page and the clearing of the
  1755. * committing counter.
  1756. */
  1757. if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
  1758. !local_read(&cpu_buffer->committing)) {
  1759. local_inc(&cpu_buffer->committing);
  1760. goto again;
  1761. }
  1762. }
  1763. static struct ring_buffer_event *
  1764. rb_reserve_next_event(struct ring_buffer *buffer,
  1765. struct ring_buffer_per_cpu *cpu_buffer,
  1766. unsigned long length)
  1767. {
  1768. struct ring_buffer_event *event;
  1769. u64 ts, delta = 0;
  1770. int commit = 0;
  1771. int nr_loops = 0;
  1772. rb_start_commit(cpu_buffer);
  1773. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  1774. /*
  1775. * Due to the ability to swap a cpu buffer from a buffer
  1776. * it is possible it was swapped before we committed.
  1777. * (committing stops a swap). We check for it here and
  1778. * if it happened, we have to fail the write.
  1779. */
  1780. barrier();
  1781. if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
  1782. local_dec(&cpu_buffer->committing);
  1783. local_dec(&cpu_buffer->commits);
  1784. return NULL;
  1785. }
  1786. #endif
  1787. length = rb_calculate_event_length(length);
  1788. again:
  1789. /*
  1790. * We allow for interrupts to reenter here and do a trace.
  1791. * If one does, it will cause this original code to loop
  1792. * back here. Even with heavy interrupts happening, this
  1793. * should only happen a few times in a row. If this happens
  1794. * 1000 times in a row, there must be either an interrupt
  1795. * storm or we have something buggy.
  1796. * Bail!
  1797. */
  1798. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  1799. goto out_fail;
  1800. ts = rb_time_stamp(cpu_buffer->buffer);
  1801. /*
  1802. * Only the first commit can update the timestamp.
  1803. * Yes there is a race here. If an interrupt comes in
  1804. * just after the conditional and it traces too, then it
  1805. * will also check the deltas. More than one timestamp may
  1806. * also be made. But only the entry that did the actual
  1807. * commit will be something other than zero.
  1808. */
  1809. if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
  1810. rb_page_write(cpu_buffer->tail_page) ==
  1811. rb_commit_index(cpu_buffer))) {
  1812. u64 diff;
  1813. diff = ts - cpu_buffer->write_stamp;
  1814. /* make sure this diff is calculated here */
  1815. barrier();
  1816. /* Did the write stamp get updated already? */
  1817. if (unlikely(ts < cpu_buffer->write_stamp))
  1818. goto get_event;
  1819. delta = diff;
  1820. if (unlikely(test_time_stamp(delta))) {
  1821. commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
  1822. if (commit == -EBUSY)
  1823. goto out_fail;
  1824. if (commit == -EAGAIN)
  1825. goto again;
  1826. RB_WARN_ON(cpu_buffer, commit < 0);
  1827. }
  1828. }
  1829. get_event:
  1830. event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
  1831. if (unlikely(PTR_ERR(event) == -EAGAIN))
  1832. goto again;
  1833. if (!event)
  1834. goto out_fail;
  1835. if (!rb_event_is_commit(cpu_buffer, event))
  1836. delta = 0;
  1837. event->time_delta = delta;
  1838. return event;
  1839. out_fail:
  1840. rb_end_commit(cpu_buffer);
  1841. return NULL;
  1842. }
  1843. #ifdef CONFIG_TRACING
  1844. #define TRACE_RECURSIVE_DEPTH 16
  1845. static int trace_recursive_lock(void)
  1846. {
  1847. current->trace_recursion++;
  1848. if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
  1849. return 0;
  1850. /* Disable all tracing before we do anything else */
  1851. tracing_off_permanent();
  1852. printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
  1853. "HC[%lu]:SC[%lu]:NMI[%lu]\n",
  1854. current->trace_recursion,
  1855. hardirq_count() >> HARDIRQ_SHIFT,
  1856. softirq_count() >> SOFTIRQ_SHIFT,
  1857. in_nmi());
  1858. WARN_ON_ONCE(1);
  1859. return -1;
  1860. }
  1861. static void trace_recursive_unlock(void)
  1862. {
  1863. WARN_ON_ONCE(!current->trace_recursion);
  1864. current->trace_recursion--;
  1865. }
  1866. #else
  1867. #define trace_recursive_lock() (0)
  1868. #define trace_recursive_unlock() do { } while (0)
  1869. #endif
  1870. static DEFINE_PER_CPU(int, rb_need_resched);
  1871. /**
  1872. * ring_buffer_lock_reserve - reserve a part of the buffer
  1873. * @buffer: the ring buffer to reserve from
  1874. * @length: the length of the data to reserve (excluding event header)
  1875. *
  1876. * Returns a reseverd event on the ring buffer to copy directly to.
  1877. * The user of this interface will need to get the body to write into
  1878. * and can use the ring_buffer_event_data() interface.
  1879. *
  1880. * The length is the length of the data needed, not the event length
  1881. * which also includes the event header.
  1882. *
  1883. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  1884. * If NULL is returned, then nothing has been allocated or locked.
  1885. */
  1886. struct ring_buffer_event *
  1887. ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
  1888. {
  1889. struct ring_buffer_per_cpu *cpu_buffer;
  1890. struct ring_buffer_event *event;
  1891. int cpu, resched;
  1892. if (ring_buffer_flags != RB_BUFFERS_ON)
  1893. return NULL;
  1894. if (atomic_read(&buffer->record_disabled))
  1895. return NULL;
  1896. /* If we are tracing schedule, we don't want to recurse */
  1897. resched = ftrace_preempt_disable();
  1898. if (trace_recursive_lock())
  1899. goto out_nocheck;
  1900. cpu = raw_smp_processor_id();
  1901. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1902. goto out;
  1903. cpu_buffer = buffer->buffers[cpu];
  1904. if (atomic_read(&cpu_buffer->record_disabled))
  1905. goto out;
  1906. if (length > BUF_MAX_DATA_SIZE)
  1907. goto out;
  1908. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  1909. if (!event)
  1910. goto out;
  1911. /*
  1912. * Need to store resched state on this cpu.
  1913. * Only the first needs to.
  1914. */
  1915. if (preempt_count() == 1)
  1916. per_cpu(rb_need_resched, cpu) = resched;
  1917. return event;
  1918. out:
  1919. trace_recursive_unlock();
  1920. out_nocheck:
  1921. ftrace_preempt_enable(resched);
  1922. return NULL;
  1923. }
  1924. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  1925. static void
  1926. rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1927. struct ring_buffer_event *event)
  1928. {
  1929. /*
  1930. * The event first in the commit queue updates the
  1931. * time stamp.
  1932. */
  1933. if (rb_event_is_commit(cpu_buffer, event))
  1934. cpu_buffer->write_stamp += event->time_delta;
  1935. }
  1936. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1937. struct ring_buffer_event *event)
  1938. {
  1939. local_inc(&cpu_buffer->entries);
  1940. rb_update_write_stamp(cpu_buffer, event);
  1941. rb_end_commit(cpu_buffer);
  1942. }
  1943. /**
  1944. * ring_buffer_unlock_commit - commit a reserved
  1945. * @buffer: The buffer to commit to
  1946. * @event: The event pointer to commit.
  1947. *
  1948. * This commits the data to the ring buffer, and releases any locks held.
  1949. *
  1950. * Must be paired with ring_buffer_lock_reserve.
  1951. */
  1952. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  1953. struct ring_buffer_event *event)
  1954. {
  1955. struct ring_buffer_per_cpu *cpu_buffer;
  1956. int cpu = raw_smp_processor_id();
  1957. cpu_buffer = buffer->buffers[cpu];
  1958. rb_commit(cpu_buffer, event);
  1959. trace_recursive_unlock();
  1960. /*
  1961. * Only the last preempt count needs to restore preemption.
  1962. */
  1963. if (preempt_count() == 1)
  1964. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1965. else
  1966. preempt_enable_no_resched_notrace();
  1967. return 0;
  1968. }
  1969. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  1970. static inline void rb_event_discard(struct ring_buffer_event *event)
  1971. {
  1972. /* array[0] holds the actual length for the discarded event */
  1973. event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
  1974. event->type_len = RINGBUF_TYPE_PADDING;
  1975. /* time delta must be non zero */
  1976. if (!event->time_delta)
  1977. event->time_delta = 1;
  1978. }
  1979. /*
  1980. * Decrement the entries to the page that an event is on.
  1981. * The event does not even need to exist, only the pointer
  1982. * to the page it is on. This may only be called before the commit
  1983. * takes place.
  1984. */
  1985. static inline void
  1986. rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
  1987. struct ring_buffer_event *event)
  1988. {
  1989. unsigned long addr = (unsigned long)event;
  1990. struct buffer_page *bpage = cpu_buffer->commit_page;
  1991. struct buffer_page *start;
  1992. addr &= PAGE_MASK;
  1993. /* Do the likely case first */
  1994. if (likely(bpage->page == (void *)addr)) {
  1995. local_dec(&bpage->entries);
  1996. return;
  1997. }
  1998. /*
  1999. * Because the commit page may be on the reader page we
  2000. * start with the next page and check the end loop there.
  2001. */
  2002. rb_inc_page(cpu_buffer, &bpage);
  2003. start = bpage;
  2004. do {
  2005. if (bpage->page == (void *)addr) {
  2006. local_dec(&bpage->entries);
  2007. return;
  2008. }
  2009. rb_inc_page(cpu_buffer, &bpage);
  2010. } while (bpage != start);
  2011. /* commit not part of this buffer?? */
  2012. RB_WARN_ON(cpu_buffer, 1);
  2013. }
  2014. /**
  2015. * ring_buffer_commit_discard - discard an event that has not been committed
  2016. * @buffer: the ring buffer
  2017. * @event: non committed event to discard
  2018. *
  2019. * Sometimes an event that is in the ring buffer needs to be ignored.
  2020. * This function lets the user discard an event in the ring buffer
  2021. * and then that event will not be read later.
  2022. *
  2023. * This function only works if it is called before the the item has been
  2024. * committed. It will try to free the event from the ring buffer
  2025. * if another event has not been added behind it.
  2026. *
  2027. * If another event has been added behind it, it will set the event
  2028. * up as discarded, and perform the commit.
  2029. *
  2030. * If this function is called, do not call ring_buffer_unlock_commit on
  2031. * the event.
  2032. */
  2033. void ring_buffer_discard_commit(struct ring_buffer *buffer,
  2034. struct ring_buffer_event *event)
  2035. {
  2036. struct ring_buffer_per_cpu *cpu_buffer;
  2037. int cpu;
  2038. /* The event is discarded regardless */
  2039. rb_event_discard(event);
  2040. cpu = smp_processor_id();
  2041. cpu_buffer = buffer->buffers[cpu];
  2042. /*
  2043. * This must only be called if the event has not been
  2044. * committed yet. Thus we can assume that preemption
  2045. * is still disabled.
  2046. */
  2047. RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
  2048. rb_decrement_entry(cpu_buffer, event);
  2049. if (rb_try_to_discard(cpu_buffer, event))
  2050. goto out;
  2051. /*
  2052. * The commit is still visible by the reader, so we
  2053. * must still update the timestamp.
  2054. */
  2055. rb_update_write_stamp(cpu_buffer, event);
  2056. out:
  2057. rb_end_commit(cpu_buffer);
  2058. trace_recursive_unlock();
  2059. /*
  2060. * Only the last preempt count needs to restore preemption.
  2061. */
  2062. if (preempt_count() == 1)
  2063. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  2064. else
  2065. preempt_enable_no_resched_notrace();
  2066. }
  2067. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  2068. /**
  2069. * ring_buffer_write - write data to the buffer without reserving
  2070. * @buffer: The ring buffer to write to.
  2071. * @length: The length of the data being written (excluding the event header)
  2072. * @data: The data to write to the buffer.
  2073. *
  2074. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  2075. * one function. If you already have the data to write to the buffer, it
  2076. * may be easier to simply call this function.
  2077. *
  2078. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  2079. * and not the length of the event which would hold the header.
  2080. */
  2081. int ring_buffer_write(struct ring_buffer *buffer,
  2082. unsigned long length,
  2083. void *data)
  2084. {
  2085. struct ring_buffer_per_cpu *cpu_buffer;
  2086. struct ring_buffer_event *event;
  2087. void *body;
  2088. int ret = -EBUSY;
  2089. int cpu, resched;
  2090. if (ring_buffer_flags != RB_BUFFERS_ON)
  2091. return -EBUSY;
  2092. if (atomic_read(&buffer->record_disabled))
  2093. return -EBUSY;
  2094. resched = ftrace_preempt_disable();
  2095. cpu = raw_smp_processor_id();
  2096. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2097. goto out;
  2098. cpu_buffer = buffer->buffers[cpu];
  2099. if (atomic_read(&cpu_buffer->record_disabled))
  2100. goto out;
  2101. if (length > BUF_MAX_DATA_SIZE)
  2102. goto out;
  2103. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  2104. if (!event)
  2105. goto out;
  2106. body = rb_event_data(event);
  2107. memcpy(body, data, length);
  2108. rb_commit(cpu_buffer, event);
  2109. ret = 0;
  2110. out:
  2111. ftrace_preempt_enable(resched);
  2112. return ret;
  2113. }
  2114. EXPORT_SYMBOL_GPL(ring_buffer_write);
  2115. static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  2116. {
  2117. struct buffer_page *reader = cpu_buffer->reader_page;
  2118. struct buffer_page *head = rb_set_head_page(cpu_buffer);
  2119. struct buffer_page *commit = cpu_buffer->commit_page;
  2120. /* In case of error, head will be NULL */
  2121. if (unlikely(!head))
  2122. return 1;
  2123. return reader->read == rb_page_commit(reader) &&
  2124. (commit == reader ||
  2125. (commit == head &&
  2126. head->read == rb_page_commit(commit)));
  2127. }
  2128. /**
  2129. * ring_buffer_record_disable - stop all writes into the buffer
  2130. * @buffer: The ring buffer to stop writes to.
  2131. *
  2132. * This prevents all writes to the buffer. Any attempt to write
  2133. * to the buffer after this will fail and return NULL.
  2134. *
  2135. * The caller should call synchronize_sched() after this.
  2136. */
  2137. void ring_buffer_record_disable(struct ring_buffer *buffer)
  2138. {
  2139. atomic_inc(&buffer->record_disabled);
  2140. }
  2141. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  2142. /**
  2143. * ring_buffer_record_enable - enable writes to the buffer
  2144. * @buffer: The ring buffer to enable writes
  2145. *
  2146. * Note, multiple disables will need the same number of enables
  2147. * to truely enable the writing (much like preempt_disable).
  2148. */
  2149. void ring_buffer_record_enable(struct ring_buffer *buffer)
  2150. {
  2151. atomic_dec(&buffer->record_disabled);
  2152. }
  2153. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  2154. /**
  2155. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  2156. * @buffer: The ring buffer to stop writes to.
  2157. * @cpu: The CPU buffer to stop
  2158. *
  2159. * This prevents all writes to the buffer. Any attempt to write
  2160. * to the buffer after this will fail and return NULL.
  2161. *
  2162. * The caller should call synchronize_sched() after this.
  2163. */
  2164. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
  2165. {
  2166. struct ring_buffer_per_cpu *cpu_buffer;
  2167. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2168. return;
  2169. cpu_buffer = buffer->buffers[cpu];
  2170. atomic_inc(&cpu_buffer->record_disabled);
  2171. }
  2172. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  2173. /**
  2174. * ring_buffer_record_enable_cpu - enable writes to the buffer
  2175. * @buffer: The ring buffer to enable writes
  2176. * @cpu: The CPU to enable.
  2177. *
  2178. * Note, multiple disables will need the same number of enables
  2179. * to truely enable the writing (much like preempt_disable).
  2180. */
  2181. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
  2182. {
  2183. struct ring_buffer_per_cpu *cpu_buffer;
  2184. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2185. return;
  2186. cpu_buffer = buffer->buffers[cpu];
  2187. atomic_dec(&cpu_buffer->record_disabled);
  2188. }
  2189. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  2190. /**
  2191. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  2192. * @buffer: The ring buffer
  2193. * @cpu: The per CPU buffer to get the entries from.
  2194. */
  2195. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
  2196. {
  2197. struct ring_buffer_per_cpu *cpu_buffer;
  2198. unsigned long ret;
  2199. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2200. return 0;
  2201. cpu_buffer = buffer->buffers[cpu];
  2202. ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
  2203. - cpu_buffer->read;
  2204. return ret;
  2205. }
  2206. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  2207. /**
  2208. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  2209. * @buffer: The ring buffer
  2210. * @cpu: The per CPU buffer to get the number of overruns from
  2211. */
  2212. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  2213. {
  2214. struct ring_buffer_per_cpu *cpu_buffer;
  2215. unsigned long ret;
  2216. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2217. return 0;
  2218. cpu_buffer = buffer->buffers[cpu];
  2219. ret = local_read(&cpu_buffer->overrun);
  2220. return ret;
  2221. }
  2222. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  2223. /**
  2224. * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
  2225. * @buffer: The ring buffer
  2226. * @cpu: The per CPU buffer to get the number of overruns from
  2227. */
  2228. unsigned long
  2229. ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
  2230. {
  2231. struct ring_buffer_per_cpu *cpu_buffer;
  2232. unsigned long ret;
  2233. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2234. return 0;
  2235. cpu_buffer = buffer->buffers[cpu];
  2236. ret = local_read(&cpu_buffer->commit_overrun);
  2237. return ret;
  2238. }
  2239. EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
  2240. /**
  2241. * ring_buffer_entries - get the number of entries in a buffer
  2242. * @buffer: The ring buffer
  2243. *
  2244. * Returns the total number of entries in the ring buffer
  2245. * (all CPU entries)
  2246. */
  2247. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  2248. {
  2249. struct ring_buffer_per_cpu *cpu_buffer;
  2250. unsigned long entries = 0;
  2251. int cpu;
  2252. /* if you care about this being correct, lock the buffer */
  2253. for_each_buffer_cpu(buffer, cpu) {
  2254. cpu_buffer = buffer->buffers[cpu];
  2255. entries += (local_read(&cpu_buffer->entries) -
  2256. local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
  2257. }
  2258. return entries;
  2259. }
  2260. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  2261. /**
  2262. * ring_buffer_overruns - get the number of overruns in buffer
  2263. * @buffer: The ring buffer
  2264. *
  2265. * Returns the total number of overruns in the ring buffer
  2266. * (all CPU entries)
  2267. */
  2268. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  2269. {
  2270. struct ring_buffer_per_cpu *cpu_buffer;
  2271. unsigned long overruns = 0;
  2272. int cpu;
  2273. /* if you care about this being correct, lock the buffer */
  2274. for_each_buffer_cpu(buffer, cpu) {
  2275. cpu_buffer = buffer->buffers[cpu];
  2276. overruns += local_read(&cpu_buffer->overrun);
  2277. }
  2278. return overruns;
  2279. }
  2280. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  2281. static void rb_iter_reset(struct ring_buffer_iter *iter)
  2282. {
  2283. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2284. /* Iterator usage is expected to have record disabled */
  2285. if (list_empty(&cpu_buffer->reader_page->list)) {
  2286. iter->head_page = rb_set_head_page(cpu_buffer);
  2287. if (unlikely(!iter->head_page))
  2288. return;
  2289. iter->head = iter->head_page->read;
  2290. } else {
  2291. iter->head_page = cpu_buffer->reader_page;
  2292. iter->head = cpu_buffer->reader_page->read;
  2293. }
  2294. if (iter->head)
  2295. iter->read_stamp = cpu_buffer->read_stamp;
  2296. else
  2297. iter->read_stamp = iter->head_page->page->time_stamp;
  2298. }
  2299. /**
  2300. * ring_buffer_iter_reset - reset an iterator
  2301. * @iter: The iterator to reset
  2302. *
  2303. * Resets the iterator, so that it will start from the beginning
  2304. * again.
  2305. */
  2306. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  2307. {
  2308. struct ring_buffer_per_cpu *cpu_buffer;
  2309. unsigned long flags;
  2310. if (!iter)
  2311. return;
  2312. cpu_buffer = iter->cpu_buffer;
  2313. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2314. rb_iter_reset(iter);
  2315. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2316. }
  2317. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  2318. /**
  2319. * ring_buffer_iter_empty - check if an iterator has no more to read
  2320. * @iter: The iterator to check
  2321. */
  2322. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  2323. {
  2324. struct ring_buffer_per_cpu *cpu_buffer;
  2325. cpu_buffer = iter->cpu_buffer;
  2326. return iter->head_page == cpu_buffer->commit_page &&
  2327. iter->head == rb_commit_index(cpu_buffer);
  2328. }
  2329. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  2330. static void
  2331. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  2332. struct ring_buffer_event *event)
  2333. {
  2334. u64 delta;
  2335. switch (event->type_len) {
  2336. case RINGBUF_TYPE_PADDING:
  2337. return;
  2338. case RINGBUF_TYPE_TIME_EXTEND:
  2339. delta = event->array[0];
  2340. delta <<= TS_SHIFT;
  2341. delta += event->time_delta;
  2342. cpu_buffer->read_stamp += delta;
  2343. return;
  2344. case RINGBUF_TYPE_TIME_STAMP:
  2345. /* FIXME: not implemented */
  2346. return;
  2347. case RINGBUF_TYPE_DATA:
  2348. cpu_buffer->read_stamp += event->time_delta;
  2349. return;
  2350. default:
  2351. BUG();
  2352. }
  2353. return;
  2354. }
  2355. static void
  2356. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  2357. struct ring_buffer_event *event)
  2358. {
  2359. u64 delta;
  2360. switch (event->type_len) {
  2361. case RINGBUF_TYPE_PADDING:
  2362. return;
  2363. case RINGBUF_TYPE_TIME_EXTEND:
  2364. delta = event->array[0];
  2365. delta <<= TS_SHIFT;
  2366. delta += event->time_delta;
  2367. iter->read_stamp += delta;
  2368. return;
  2369. case RINGBUF_TYPE_TIME_STAMP:
  2370. /* FIXME: not implemented */
  2371. return;
  2372. case RINGBUF_TYPE_DATA:
  2373. iter->read_stamp += event->time_delta;
  2374. return;
  2375. default:
  2376. BUG();
  2377. }
  2378. return;
  2379. }
  2380. static struct buffer_page *
  2381. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  2382. {
  2383. struct buffer_page *reader = NULL;
  2384. unsigned long flags;
  2385. int nr_loops = 0;
  2386. int ret;
  2387. local_irq_save(flags);
  2388. arch_spin_lock(&cpu_buffer->lock);
  2389. again:
  2390. /*
  2391. * This should normally only loop twice. But because the
  2392. * start of the reader inserts an empty page, it causes
  2393. * a case where we will loop three times. There should be no
  2394. * reason to loop four times (that I know of).
  2395. */
  2396. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  2397. reader = NULL;
  2398. goto out;
  2399. }
  2400. reader = cpu_buffer->reader_page;
  2401. /* If there's more to read, return this page */
  2402. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  2403. goto out;
  2404. /* Never should we have an index greater than the size */
  2405. if (RB_WARN_ON(cpu_buffer,
  2406. cpu_buffer->reader_page->read > rb_page_size(reader)))
  2407. goto out;
  2408. /* check if we caught up to the tail */
  2409. reader = NULL;
  2410. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  2411. goto out;
  2412. /*
  2413. * Reset the reader page to size zero.
  2414. */
  2415. local_set(&cpu_buffer->reader_page->write, 0);
  2416. local_set(&cpu_buffer->reader_page->entries, 0);
  2417. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2418. spin:
  2419. /*
  2420. * Splice the empty reader page into the list around the head.
  2421. */
  2422. reader = rb_set_head_page(cpu_buffer);
  2423. cpu_buffer->reader_page->list.next = reader->list.next;
  2424. cpu_buffer->reader_page->list.prev = reader->list.prev;
  2425. /*
  2426. * cpu_buffer->pages just needs to point to the buffer, it
  2427. * has no specific buffer page to point to. Lets move it out
  2428. * of our way so we don't accidently swap it.
  2429. */
  2430. cpu_buffer->pages = reader->list.prev;
  2431. /* The reader page will be pointing to the new head */
  2432. rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
  2433. /*
  2434. * Here's the tricky part.
  2435. *
  2436. * We need to move the pointer past the header page.
  2437. * But we can only do that if a writer is not currently
  2438. * moving it. The page before the header page has the
  2439. * flag bit '1' set if it is pointing to the page we want.
  2440. * but if the writer is in the process of moving it
  2441. * than it will be '2' or already moved '0'.
  2442. */
  2443. ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
  2444. /*
  2445. * If we did not convert it, then we must try again.
  2446. */
  2447. if (!ret)
  2448. goto spin;
  2449. /*
  2450. * Yeah! We succeeded in replacing the page.
  2451. *
  2452. * Now make the new head point back to the reader page.
  2453. */
  2454. reader->list.next->prev = &cpu_buffer->reader_page->list;
  2455. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  2456. /* Finally update the reader page to the new head */
  2457. cpu_buffer->reader_page = reader;
  2458. rb_reset_reader_page(cpu_buffer);
  2459. goto again;
  2460. out:
  2461. arch_spin_unlock(&cpu_buffer->lock);
  2462. local_irq_restore(flags);
  2463. return reader;
  2464. }
  2465. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  2466. {
  2467. struct ring_buffer_event *event;
  2468. struct buffer_page *reader;
  2469. unsigned length;
  2470. reader = rb_get_reader_page(cpu_buffer);
  2471. /* This function should not be called when buffer is empty */
  2472. if (RB_WARN_ON(cpu_buffer, !reader))
  2473. return;
  2474. event = rb_reader_event(cpu_buffer);
  2475. if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  2476. cpu_buffer->read++;
  2477. rb_update_read_stamp(cpu_buffer, event);
  2478. length = rb_event_length(event);
  2479. cpu_buffer->reader_page->read += length;
  2480. }
  2481. static void rb_advance_iter(struct ring_buffer_iter *iter)
  2482. {
  2483. struct ring_buffer *buffer;
  2484. struct ring_buffer_per_cpu *cpu_buffer;
  2485. struct ring_buffer_event *event;
  2486. unsigned length;
  2487. cpu_buffer = iter->cpu_buffer;
  2488. buffer = cpu_buffer->buffer;
  2489. /*
  2490. * Check if we are at the end of the buffer.
  2491. */
  2492. if (iter->head >= rb_page_size(iter->head_page)) {
  2493. /* discarded commits can make the page empty */
  2494. if (iter->head_page == cpu_buffer->commit_page)
  2495. return;
  2496. rb_inc_iter(iter);
  2497. return;
  2498. }
  2499. event = rb_iter_head_event(iter);
  2500. length = rb_event_length(event);
  2501. /*
  2502. * This should not be called to advance the header if we are
  2503. * at the tail of the buffer.
  2504. */
  2505. if (RB_WARN_ON(cpu_buffer,
  2506. (iter->head_page == cpu_buffer->commit_page) &&
  2507. (iter->head + length > rb_commit_index(cpu_buffer))))
  2508. return;
  2509. rb_update_iter_read_stamp(iter, event);
  2510. iter->head += length;
  2511. /* check for end of page padding */
  2512. if ((iter->head >= rb_page_size(iter->head_page)) &&
  2513. (iter->head_page != cpu_buffer->commit_page))
  2514. rb_advance_iter(iter);
  2515. }
  2516. static struct ring_buffer_event *
  2517. rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
  2518. {
  2519. struct ring_buffer_event *event;
  2520. struct buffer_page *reader;
  2521. int nr_loops = 0;
  2522. again:
  2523. /*
  2524. * We repeat when a timestamp is encountered. It is possible
  2525. * to get multiple timestamps from an interrupt entering just
  2526. * as one timestamp is about to be written, or from discarded
  2527. * commits. The most that we can have is the number on a single page.
  2528. */
  2529. if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
  2530. return NULL;
  2531. reader = rb_get_reader_page(cpu_buffer);
  2532. if (!reader)
  2533. return NULL;
  2534. event = rb_reader_event(cpu_buffer);
  2535. switch (event->type_len) {
  2536. case RINGBUF_TYPE_PADDING:
  2537. if (rb_null_event(event))
  2538. RB_WARN_ON(cpu_buffer, 1);
  2539. /*
  2540. * Because the writer could be discarding every
  2541. * event it creates (which would probably be bad)
  2542. * if we were to go back to "again" then we may never
  2543. * catch up, and will trigger the warn on, or lock
  2544. * the box. Return the padding, and we will release
  2545. * the current locks, and try again.
  2546. */
  2547. return event;
  2548. case RINGBUF_TYPE_TIME_EXTEND:
  2549. /* Internal data, OK to advance */
  2550. rb_advance_reader(cpu_buffer);
  2551. goto again;
  2552. case RINGBUF_TYPE_TIME_STAMP:
  2553. /* FIXME: not implemented */
  2554. rb_advance_reader(cpu_buffer);
  2555. goto again;
  2556. case RINGBUF_TYPE_DATA:
  2557. if (ts) {
  2558. *ts = cpu_buffer->read_stamp + event->time_delta;
  2559. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  2560. cpu_buffer->cpu, ts);
  2561. }
  2562. return event;
  2563. default:
  2564. BUG();
  2565. }
  2566. return NULL;
  2567. }
  2568. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  2569. static struct ring_buffer_event *
  2570. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2571. {
  2572. struct ring_buffer *buffer;
  2573. struct ring_buffer_per_cpu *cpu_buffer;
  2574. struct ring_buffer_event *event;
  2575. int nr_loops = 0;
  2576. if (ring_buffer_iter_empty(iter))
  2577. return NULL;
  2578. cpu_buffer = iter->cpu_buffer;
  2579. buffer = cpu_buffer->buffer;
  2580. again:
  2581. /*
  2582. * We repeat when a timestamp is encountered.
  2583. * We can get multiple timestamps by nested interrupts or also
  2584. * if filtering is on (discarding commits). Since discarding
  2585. * commits can be frequent we can get a lot of timestamps.
  2586. * But we limit them by not adding timestamps if they begin
  2587. * at the start of a page.
  2588. */
  2589. if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
  2590. return NULL;
  2591. if (rb_per_cpu_empty(cpu_buffer))
  2592. return NULL;
  2593. event = rb_iter_head_event(iter);
  2594. switch (event->type_len) {
  2595. case RINGBUF_TYPE_PADDING:
  2596. if (rb_null_event(event)) {
  2597. rb_inc_iter(iter);
  2598. goto again;
  2599. }
  2600. rb_advance_iter(iter);
  2601. return event;
  2602. case RINGBUF_TYPE_TIME_EXTEND:
  2603. /* Internal data, OK to advance */
  2604. rb_advance_iter(iter);
  2605. goto again;
  2606. case RINGBUF_TYPE_TIME_STAMP:
  2607. /* FIXME: not implemented */
  2608. rb_advance_iter(iter);
  2609. goto again;
  2610. case RINGBUF_TYPE_DATA:
  2611. if (ts) {
  2612. *ts = iter->read_stamp + event->time_delta;
  2613. ring_buffer_normalize_time_stamp(buffer,
  2614. cpu_buffer->cpu, ts);
  2615. }
  2616. return event;
  2617. default:
  2618. BUG();
  2619. }
  2620. return NULL;
  2621. }
  2622. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  2623. static inline int rb_ok_to_lock(void)
  2624. {
  2625. /*
  2626. * If an NMI die dumps out the content of the ring buffer
  2627. * do not grab locks. We also permanently disable the ring
  2628. * buffer too. A one time deal is all you get from reading
  2629. * the ring buffer from an NMI.
  2630. */
  2631. if (likely(!in_nmi()))
  2632. return 1;
  2633. tracing_off_permanent();
  2634. return 0;
  2635. }
  2636. /**
  2637. * ring_buffer_peek - peek at the next event to be read
  2638. * @buffer: The ring buffer to read
  2639. * @cpu: The cpu to peak at
  2640. * @ts: The timestamp counter of this event.
  2641. *
  2642. * This will return the event that will be read next, but does
  2643. * not consume the data.
  2644. */
  2645. struct ring_buffer_event *
  2646. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  2647. {
  2648. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2649. struct ring_buffer_event *event;
  2650. unsigned long flags;
  2651. int dolock;
  2652. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2653. return NULL;
  2654. dolock = rb_ok_to_lock();
  2655. again:
  2656. local_irq_save(flags);
  2657. if (dolock)
  2658. spin_lock(&cpu_buffer->reader_lock);
  2659. event = rb_buffer_peek(cpu_buffer, ts);
  2660. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2661. rb_advance_reader(cpu_buffer);
  2662. if (dolock)
  2663. spin_unlock(&cpu_buffer->reader_lock);
  2664. local_irq_restore(flags);
  2665. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2666. goto again;
  2667. return event;
  2668. }
  2669. /**
  2670. * ring_buffer_iter_peek - peek at the next event to be read
  2671. * @iter: The ring buffer iterator
  2672. * @ts: The timestamp counter of this event.
  2673. *
  2674. * This will return the event that will be read next, but does
  2675. * not increment the iterator.
  2676. */
  2677. struct ring_buffer_event *
  2678. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2679. {
  2680. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2681. struct ring_buffer_event *event;
  2682. unsigned long flags;
  2683. again:
  2684. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2685. event = rb_iter_peek(iter, ts);
  2686. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2687. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2688. goto again;
  2689. return event;
  2690. }
  2691. /**
  2692. * ring_buffer_consume - return an event and consume it
  2693. * @buffer: The ring buffer to get the next event from
  2694. *
  2695. * Returns the next event in the ring buffer, and that event is consumed.
  2696. * Meaning, that sequential reads will keep returning a different event,
  2697. * and eventually empty the ring buffer if the producer is slower.
  2698. */
  2699. struct ring_buffer_event *
  2700. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
  2701. {
  2702. struct ring_buffer_per_cpu *cpu_buffer;
  2703. struct ring_buffer_event *event = NULL;
  2704. unsigned long flags;
  2705. int dolock;
  2706. dolock = rb_ok_to_lock();
  2707. again:
  2708. /* might be called in atomic */
  2709. preempt_disable();
  2710. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2711. goto out;
  2712. cpu_buffer = buffer->buffers[cpu];
  2713. local_irq_save(flags);
  2714. if (dolock)
  2715. spin_lock(&cpu_buffer->reader_lock);
  2716. event = rb_buffer_peek(cpu_buffer, ts);
  2717. if (event)
  2718. rb_advance_reader(cpu_buffer);
  2719. if (dolock)
  2720. spin_unlock(&cpu_buffer->reader_lock);
  2721. local_irq_restore(flags);
  2722. out:
  2723. preempt_enable();
  2724. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2725. goto again;
  2726. return event;
  2727. }
  2728. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  2729. /**
  2730. * ring_buffer_read_start - start a non consuming read of the buffer
  2731. * @buffer: The ring buffer to read from
  2732. * @cpu: The cpu buffer to iterate over
  2733. *
  2734. * This starts up an iteration through the buffer. It also disables
  2735. * the recording to the buffer until the reading is finished.
  2736. * This prevents the reading from being corrupted. This is not
  2737. * a consuming read, so a producer is not expected.
  2738. *
  2739. * Must be paired with ring_buffer_finish.
  2740. */
  2741. struct ring_buffer_iter *
  2742. ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
  2743. {
  2744. struct ring_buffer_per_cpu *cpu_buffer;
  2745. struct ring_buffer_iter *iter;
  2746. unsigned long flags;
  2747. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2748. return NULL;
  2749. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2750. if (!iter)
  2751. return NULL;
  2752. cpu_buffer = buffer->buffers[cpu];
  2753. iter->cpu_buffer = cpu_buffer;
  2754. atomic_inc(&cpu_buffer->record_disabled);
  2755. synchronize_sched();
  2756. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2757. arch_spin_lock(&cpu_buffer->lock);
  2758. rb_iter_reset(iter);
  2759. arch_spin_unlock(&cpu_buffer->lock);
  2760. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2761. return iter;
  2762. }
  2763. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  2764. /**
  2765. * ring_buffer_finish - finish reading the iterator of the buffer
  2766. * @iter: The iterator retrieved by ring_buffer_start
  2767. *
  2768. * This re-enables the recording to the buffer, and frees the
  2769. * iterator.
  2770. */
  2771. void
  2772. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  2773. {
  2774. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2775. atomic_dec(&cpu_buffer->record_disabled);
  2776. kfree(iter);
  2777. }
  2778. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  2779. /**
  2780. * ring_buffer_read - read the next item in the ring buffer by the iterator
  2781. * @iter: The ring buffer iterator
  2782. * @ts: The time stamp of the event read.
  2783. *
  2784. * This reads the next event in the ring buffer and increments the iterator.
  2785. */
  2786. struct ring_buffer_event *
  2787. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  2788. {
  2789. struct ring_buffer_event *event;
  2790. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2791. unsigned long flags;
  2792. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2793. again:
  2794. event = rb_iter_peek(iter, ts);
  2795. if (!event)
  2796. goto out;
  2797. if (event->type_len == RINGBUF_TYPE_PADDING)
  2798. goto again;
  2799. rb_advance_iter(iter);
  2800. out:
  2801. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2802. return event;
  2803. }
  2804. EXPORT_SYMBOL_GPL(ring_buffer_read);
  2805. /**
  2806. * ring_buffer_size - return the size of the ring buffer (in bytes)
  2807. * @buffer: The ring buffer.
  2808. */
  2809. unsigned long ring_buffer_size(struct ring_buffer *buffer)
  2810. {
  2811. return BUF_PAGE_SIZE * buffer->pages;
  2812. }
  2813. EXPORT_SYMBOL_GPL(ring_buffer_size);
  2814. static void
  2815. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  2816. {
  2817. rb_head_page_deactivate(cpu_buffer);
  2818. cpu_buffer->head_page
  2819. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  2820. local_set(&cpu_buffer->head_page->write, 0);
  2821. local_set(&cpu_buffer->head_page->entries, 0);
  2822. local_set(&cpu_buffer->head_page->page->commit, 0);
  2823. cpu_buffer->head_page->read = 0;
  2824. cpu_buffer->tail_page = cpu_buffer->head_page;
  2825. cpu_buffer->commit_page = cpu_buffer->head_page;
  2826. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2827. local_set(&cpu_buffer->reader_page->write, 0);
  2828. local_set(&cpu_buffer->reader_page->entries, 0);
  2829. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2830. cpu_buffer->reader_page->read = 0;
  2831. local_set(&cpu_buffer->commit_overrun, 0);
  2832. local_set(&cpu_buffer->overrun, 0);
  2833. local_set(&cpu_buffer->entries, 0);
  2834. local_set(&cpu_buffer->committing, 0);
  2835. local_set(&cpu_buffer->commits, 0);
  2836. cpu_buffer->read = 0;
  2837. cpu_buffer->write_stamp = 0;
  2838. cpu_buffer->read_stamp = 0;
  2839. rb_head_page_activate(cpu_buffer);
  2840. }
  2841. /**
  2842. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  2843. * @buffer: The ring buffer to reset a per cpu buffer of
  2844. * @cpu: The CPU buffer to be reset
  2845. */
  2846. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  2847. {
  2848. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2849. unsigned long flags;
  2850. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2851. return;
  2852. atomic_inc(&cpu_buffer->record_disabled);
  2853. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2854. if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
  2855. goto out;
  2856. arch_spin_lock(&cpu_buffer->lock);
  2857. rb_reset_cpu(cpu_buffer);
  2858. arch_spin_unlock(&cpu_buffer->lock);
  2859. out:
  2860. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2861. atomic_dec(&cpu_buffer->record_disabled);
  2862. }
  2863. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  2864. /**
  2865. * ring_buffer_reset - reset a ring buffer
  2866. * @buffer: The ring buffer to reset all cpu buffers
  2867. */
  2868. void ring_buffer_reset(struct ring_buffer *buffer)
  2869. {
  2870. int cpu;
  2871. for_each_buffer_cpu(buffer, cpu)
  2872. ring_buffer_reset_cpu(buffer, cpu);
  2873. }
  2874. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  2875. /**
  2876. * rind_buffer_empty - is the ring buffer empty?
  2877. * @buffer: The ring buffer to test
  2878. */
  2879. int ring_buffer_empty(struct ring_buffer *buffer)
  2880. {
  2881. struct ring_buffer_per_cpu *cpu_buffer;
  2882. unsigned long flags;
  2883. int dolock;
  2884. int cpu;
  2885. int ret;
  2886. dolock = rb_ok_to_lock();
  2887. /* yes this is racy, but if you don't like the race, lock the buffer */
  2888. for_each_buffer_cpu(buffer, cpu) {
  2889. cpu_buffer = buffer->buffers[cpu];
  2890. local_irq_save(flags);
  2891. if (dolock)
  2892. spin_lock(&cpu_buffer->reader_lock);
  2893. ret = rb_per_cpu_empty(cpu_buffer);
  2894. if (dolock)
  2895. spin_unlock(&cpu_buffer->reader_lock);
  2896. local_irq_restore(flags);
  2897. if (!ret)
  2898. return 0;
  2899. }
  2900. return 1;
  2901. }
  2902. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  2903. /**
  2904. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  2905. * @buffer: The ring buffer
  2906. * @cpu: The CPU buffer to test
  2907. */
  2908. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  2909. {
  2910. struct ring_buffer_per_cpu *cpu_buffer;
  2911. unsigned long flags;
  2912. int dolock;
  2913. int ret;
  2914. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2915. return 1;
  2916. dolock = rb_ok_to_lock();
  2917. cpu_buffer = buffer->buffers[cpu];
  2918. local_irq_save(flags);
  2919. if (dolock)
  2920. spin_lock(&cpu_buffer->reader_lock);
  2921. ret = rb_per_cpu_empty(cpu_buffer);
  2922. if (dolock)
  2923. spin_unlock(&cpu_buffer->reader_lock);
  2924. local_irq_restore(flags);
  2925. return ret;
  2926. }
  2927. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  2928. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  2929. /**
  2930. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  2931. * @buffer_a: One buffer to swap with
  2932. * @buffer_b: The other buffer to swap with
  2933. *
  2934. * This function is useful for tracers that want to take a "snapshot"
  2935. * of a CPU buffer and has another back up buffer lying around.
  2936. * it is expected that the tracer handles the cpu buffer not being
  2937. * used at the moment.
  2938. */
  2939. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  2940. struct ring_buffer *buffer_b, int cpu)
  2941. {
  2942. struct ring_buffer_per_cpu *cpu_buffer_a;
  2943. struct ring_buffer_per_cpu *cpu_buffer_b;
  2944. int ret = -EINVAL;
  2945. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  2946. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  2947. goto out;
  2948. /* At least make sure the two buffers are somewhat the same */
  2949. if (buffer_a->pages != buffer_b->pages)
  2950. goto out;
  2951. ret = -EAGAIN;
  2952. if (ring_buffer_flags != RB_BUFFERS_ON)
  2953. goto out;
  2954. if (atomic_read(&buffer_a->record_disabled))
  2955. goto out;
  2956. if (atomic_read(&buffer_b->record_disabled))
  2957. goto out;
  2958. cpu_buffer_a = buffer_a->buffers[cpu];
  2959. cpu_buffer_b = buffer_b->buffers[cpu];
  2960. if (atomic_read(&cpu_buffer_a->record_disabled))
  2961. goto out;
  2962. if (atomic_read(&cpu_buffer_b->record_disabled))
  2963. goto out;
  2964. /*
  2965. * We can't do a synchronize_sched here because this
  2966. * function can be called in atomic context.
  2967. * Normally this will be called from the same CPU as cpu.
  2968. * If not it's up to the caller to protect this.
  2969. */
  2970. atomic_inc(&cpu_buffer_a->record_disabled);
  2971. atomic_inc(&cpu_buffer_b->record_disabled);
  2972. ret = -EBUSY;
  2973. if (local_read(&cpu_buffer_a->committing))
  2974. goto out_dec;
  2975. if (local_read(&cpu_buffer_b->committing))
  2976. goto out_dec;
  2977. buffer_a->buffers[cpu] = cpu_buffer_b;
  2978. buffer_b->buffers[cpu] = cpu_buffer_a;
  2979. cpu_buffer_b->buffer = buffer_a;
  2980. cpu_buffer_a->buffer = buffer_b;
  2981. ret = 0;
  2982. out_dec:
  2983. atomic_dec(&cpu_buffer_a->record_disabled);
  2984. atomic_dec(&cpu_buffer_b->record_disabled);
  2985. out:
  2986. return ret;
  2987. }
  2988. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  2989. #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
  2990. /**
  2991. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  2992. * @buffer: the buffer to allocate for.
  2993. *
  2994. * This function is used in conjunction with ring_buffer_read_page.
  2995. * When reading a full page from the ring buffer, these functions
  2996. * can be used to speed up the process. The calling function should
  2997. * allocate a few pages first with this function. Then when it
  2998. * needs to get pages from the ring buffer, it passes the result
  2999. * of this function into ring_buffer_read_page, which will swap
  3000. * the page that was allocated, with the read page of the buffer.
  3001. *
  3002. * Returns:
  3003. * The page allocated, or NULL on error.
  3004. */
  3005. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
  3006. {
  3007. struct buffer_data_page *bpage;
  3008. unsigned long addr;
  3009. addr = __get_free_page(GFP_KERNEL);
  3010. if (!addr)
  3011. return NULL;
  3012. bpage = (void *)addr;
  3013. rb_init_page(bpage);
  3014. return bpage;
  3015. }
  3016. EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  3017. /**
  3018. * ring_buffer_free_read_page - free an allocated read page
  3019. * @buffer: the buffer the page was allocate for
  3020. * @data: the page to free
  3021. *
  3022. * Free a page allocated from ring_buffer_alloc_read_page.
  3023. */
  3024. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  3025. {
  3026. free_page((unsigned long)data);
  3027. }
  3028. EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
  3029. /**
  3030. * ring_buffer_read_page - extract a page from the ring buffer
  3031. * @buffer: buffer to extract from
  3032. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  3033. * @len: amount to extract
  3034. * @cpu: the cpu of the buffer to extract
  3035. * @full: should the extraction only happen when the page is full.
  3036. *
  3037. * This function will pull out a page from the ring buffer and consume it.
  3038. * @data_page must be the address of the variable that was returned
  3039. * from ring_buffer_alloc_read_page. This is because the page might be used
  3040. * to swap with a page in the ring buffer.
  3041. *
  3042. * for example:
  3043. * rpage = ring_buffer_alloc_read_page(buffer);
  3044. * if (!rpage)
  3045. * return error;
  3046. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  3047. * if (ret >= 0)
  3048. * process_page(rpage, ret);
  3049. *
  3050. * When @full is set, the function will not return true unless
  3051. * the writer is off the reader page.
  3052. *
  3053. * Note: it is up to the calling functions to handle sleeps and wakeups.
  3054. * The ring buffer can be used anywhere in the kernel and can not
  3055. * blindly call wake_up. The layer that uses the ring buffer must be
  3056. * responsible for that.
  3057. *
  3058. * Returns:
  3059. * >=0 if data has been transferred, returns the offset of consumed data.
  3060. * <0 if no data has been transferred.
  3061. */
  3062. int ring_buffer_read_page(struct ring_buffer *buffer,
  3063. void **data_page, size_t len, int cpu, int full)
  3064. {
  3065. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  3066. struct ring_buffer_event *event;
  3067. struct buffer_data_page *bpage;
  3068. struct buffer_page *reader;
  3069. unsigned long flags;
  3070. unsigned int commit;
  3071. unsigned int read;
  3072. u64 save_timestamp;
  3073. int ret = -1;
  3074. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3075. goto out;
  3076. /*
  3077. * If len is not big enough to hold the page header, then
  3078. * we can not copy anything.
  3079. */
  3080. if (len <= BUF_PAGE_HDR_SIZE)
  3081. goto out;
  3082. len -= BUF_PAGE_HDR_SIZE;
  3083. if (!data_page)
  3084. goto out;
  3085. bpage = *data_page;
  3086. if (!bpage)
  3087. goto out;
  3088. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  3089. reader = rb_get_reader_page(cpu_buffer);
  3090. if (!reader)
  3091. goto out_unlock;
  3092. event = rb_reader_event(cpu_buffer);
  3093. read = reader->read;
  3094. commit = rb_page_commit(reader);
  3095. /*
  3096. * If this page has been partially read or
  3097. * if len is not big enough to read the rest of the page or
  3098. * a writer is still on the page, then
  3099. * we must copy the data from the page to the buffer.
  3100. * Otherwise, we can simply swap the page with the one passed in.
  3101. */
  3102. if (read || (len < (commit - read)) ||
  3103. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  3104. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  3105. unsigned int rpos = read;
  3106. unsigned int pos = 0;
  3107. unsigned int size;
  3108. if (full)
  3109. goto out_unlock;
  3110. if (len > (commit - read))
  3111. len = (commit - read);
  3112. size = rb_event_length(event);
  3113. if (len < size)
  3114. goto out_unlock;
  3115. /* save the current timestamp, since the user will need it */
  3116. save_timestamp = cpu_buffer->read_stamp;
  3117. /* Need to copy one event at a time */
  3118. do {
  3119. memcpy(bpage->data + pos, rpage->data + rpos, size);
  3120. len -= size;
  3121. rb_advance_reader(cpu_buffer);
  3122. rpos = reader->read;
  3123. pos += size;
  3124. event = rb_reader_event(cpu_buffer);
  3125. size = rb_event_length(event);
  3126. } while (len > size);
  3127. /* update bpage */
  3128. local_set(&bpage->commit, pos);
  3129. bpage->time_stamp = save_timestamp;
  3130. /* we copied everything to the beginning */
  3131. read = 0;
  3132. } else {
  3133. /* update the entry counter */
  3134. cpu_buffer->read += rb_page_entries(reader);
  3135. /* swap the pages */
  3136. rb_init_page(bpage);
  3137. bpage = reader->page;
  3138. reader->page = *data_page;
  3139. local_set(&reader->write, 0);
  3140. local_set(&reader->entries, 0);
  3141. reader->read = 0;
  3142. *data_page = bpage;
  3143. }
  3144. ret = read;
  3145. out_unlock:
  3146. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  3147. out:
  3148. return ret;
  3149. }
  3150. EXPORT_SYMBOL_GPL(ring_buffer_read_page);
  3151. #ifdef CONFIG_TRACING
  3152. static ssize_t
  3153. rb_simple_read(struct file *filp, char __user *ubuf,
  3154. size_t cnt, loff_t *ppos)
  3155. {
  3156. unsigned long *p = filp->private_data;
  3157. char buf[64];
  3158. int r;
  3159. if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
  3160. r = sprintf(buf, "permanently disabled\n");
  3161. else
  3162. r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
  3163. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3164. }
  3165. static ssize_t
  3166. rb_simple_write(struct file *filp, const char __user *ubuf,
  3167. size_t cnt, loff_t *ppos)
  3168. {
  3169. unsigned long *p = filp->private_data;
  3170. char buf[64];
  3171. unsigned long val;
  3172. int ret;
  3173. if (cnt >= sizeof(buf))
  3174. return -EINVAL;
  3175. if (copy_from_user(&buf, ubuf, cnt))
  3176. return -EFAULT;
  3177. buf[cnt] = 0;
  3178. ret = strict_strtoul(buf, 10, &val);
  3179. if (ret < 0)
  3180. return ret;
  3181. if (val)
  3182. set_bit(RB_BUFFERS_ON_BIT, p);
  3183. else
  3184. clear_bit(RB_BUFFERS_ON_BIT, p);
  3185. (*ppos)++;
  3186. return cnt;
  3187. }
  3188. static const struct file_operations rb_simple_fops = {
  3189. .open = tracing_open_generic,
  3190. .read = rb_simple_read,
  3191. .write = rb_simple_write,
  3192. };
  3193. static __init int rb_init_debugfs(void)
  3194. {
  3195. struct dentry *d_tracer;
  3196. d_tracer = tracing_init_dentry();
  3197. trace_create_file("tracing_on", 0644, d_tracer,
  3198. &ring_buffer_flags, &rb_simple_fops);
  3199. return 0;
  3200. }
  3201. fs_initcall(rb_init_debugfs);
  3202. #endif
  3203. #ifdef CONFIG_HOTPLUG_CPU
  3204. static int rb_cpu_notify(struct notifier_block *self,
  3205. unsigned long action, void *hcpu)
  3206. {
  3207. struct ring_buffer *buffer =
  3208. container_of(self, struct ring_buffer, cpu_notify);
  3209. long cpu = (long)hcpu;
  3210. switch (action) {
  3211. case CPU_UP_PREPARE:
  3212. case CPU_UP_PREPARE_FROZEN:
  3213. if (cpumask_test_cpu(cpu, buffer->cpumask))
  3214. return NOTIFY_OK;
  3215. buffer->buffers[cpu] =
  3216. rb_allocate_cpu_buffer(buffer, cpu);
  3217. if (!buffer->buffers[cpu]) {
  3218. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  3219. cpu);
  3220. return NOTIFY_OK;
  3221. }
  3222. smp_wmb();
  3223. cpumask_set_cpu(cpu, buffer->cpumask);
  3224. break;
  3225. case CPU_DOWN_PREPARE:
  3226. case CPU_DOWN_PREPARE_FROZEN:
  3227. /*
  3228. * Do nothing.
  3229. * If we were to free the buffer, then the user would
  3230. * lose any trace that was in the buffer.
  3231. */
  3232. break;
  3233. default:
  3234. break;
  3235. }
  3236. return NOTIFY_OK;
  3237. }
  3238. #endif