ring_buffer.c 132 KB

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