ring_buffer.c 131 KB

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