ring_buffer.c 115 KB

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