ring_buffer.c 130 KB

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