ring_buffer.c 130 KB

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