ring_buffer.c 102 KB

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