ring_buffer.c 128 KB

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