ring_buffer.c 101 KB

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