ring_buffer.c 114 KB

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