ring_buffer.c 138 KB

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