ring_buffer.c 136 KB

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