ring_buffer.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026
  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_EXTEND)
  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. /*
  1558. * This is the slow path, force gcc not to inline it.
  1559. */
  1560. static noinline struct ring_buffer_event *
  1561. rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
  1562. unsigned long length, unsigned long tail,
  1563. struct buffer_page *tail_page, u64 ts)
  1564. {
  1565. struct buffer_page *commit_page = cpu_buffer->commit_page;
  1566. struct ring_buffer *buffer = cpu_buffer->buffer;
  1567. struct buffer_page *next_page;
  1568. int ret;
  1569. next_page = tail_page;
  1570. rb_inc_page(cpu_buffer, &next_page);
  1571. /*
  1572. * If for some reason, we had an interrupt storm that made
  1573. * it all the way around the buffer, bail, and warn
  1574. * about it.
  1575. */
  1576. if (unlikely(next_page == commit_page)) {
  1577. local_inc(&cpu_buffer->commit_overrun);
  1578. goto out_reset;
  1579. }
  1580. /*
  1581. * This is where the fun begins!
  1582. *
  1583. * We are fighting against races between a reader that
  1584. * could be on another CPU trying to swap its reader
  1585. * page with the buffer head.
  1586. *
  1587. * We are also fighting against interrupts coming in and
  1588. * moving the head or tail on us as well.
  1589. *
  1590. * If the next page is the head page then we have filled
  1591. * the buffer, unless the commit page is still on the
  1592. * reader page.
  1593. */
  1594. if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
  1595. /*
  1596. * If the commit is not on the reader page, then
  1597. * move the header page.
  1598. */
  1599. if (!rb_is_reader_page(cpu_buffer->commit_page)) {
  1600. /*
  1601. * If we are not in overwrite mode,
  1602. * this is easy, just stop here.
  1603. */
  1604. if (!(buffer->flags & RB_FL_OVERWRITE))
  1605. goto out_reset;
  1606. ret = rb_handle_head_page(cpu_buffer,
  1607. tail_page,
  1608. next_page);
  1609. if (ret < 0)
  1610. goto out_reset;
  1611. if (ret)
  1612. goto out_again;
  1613. } else {
  1614. /*
  1615. * We need to be careful here too. The
  1616. * commit page could still be on the reader
  1617. * page. We could have a small buffer, and
  1618. * have filled up the buffer with events
  1619. * from interrupts and such, and wrapped.
  1620. *
  1621. * Note, if the tail page is also the on the
  1622. * reader_page, we let it move out.
  1623. */
  1624. if (unlikely((cpu_buffer->commit_page !=
  1625. cpu_buffer->tail_page) &&
  1626. (cpu_buffer->commit_page ==
  1627. cpu_buffer->reader_page))) {
  1628. local_inc(&cpu_buffer->commit_overrun);
  1629. goto out_reset;
  1630. }
  1631. }
  1632. }
  1633. ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
  1634. if (ret) {
  1635. /*
  1636. * Nested commits always have zero deltas, so
  1637. * just reread the time stamp
  1638. */
  1639. ts = rb_time_stamp(buffer);
  1640. next_page->page->time_stamp = ts;
  1641. }
  1642. out_again:
  1643. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1644. /* fail and let the caller try again */
  1645. return ERR_PTR(-EAGAIN);
  1646. out_reset:
  1647. /* reset write */
  1648. rb_reset_tail(cpu_buffer, tail_page, tail, length);
  1649. return NULL;
  1650. }
  1651. static struct ring_buffer_event *
  1652. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  1653. unsigned type, unsigned long length, u64 ts)
  1654. {
  1655. struct buffer_page *tail_page;
  1656. struct ring_buffer_event *event;
  1657. unsigned long tail, write;
  1658. tail_page = cpu_buffer->tail_page;
  1659. write = local_add_return(length, &tail_page->write);
  1660. /* set write to only the index of the write */
  1661. write &= RB_WRITE_MASK;
  1662. tail = write - length;
  1663. /* See if we shot pass the end of this buffer page */
  1664. if (unlikely(write > BUF_PAGE_SIZE))
  1665. return rb_move_tail(cpu_buffer, length, tail,
  1666. tail_page, ts);
  1667. /* We reserved something on the buffer */
  1668. event = __rb_page_index(tail_page, tail);
  1669. kmemcheck_annotate_bitfield(event, bitfield);
  1670. rb_update_event(event, type, length);
  1671. /* The passed in type is zero for DATA */
  1672. if (likely(!type))
  1673. local_inc(&tail_page->entries);
  1674. /*
  1675. * If this is the first commit on the page, then update
  1676. * its timestamp.
  1677. */
  1678. if (!tail)
  1679. tail_page->page->time_stamp = ts;
  1680. return event;
  1681. }
  1682. static inline int
  1683. rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
  1684. struct ring_buffer_event *event)
  1685. {
  1686. unsigned long new_index, old_index;
  1687. struct buffer_page *bpage;
  1688. unsigned long index;
  1689. unsigned long addr;
  1690. new_index = rb_event_index(event);
  1691. old_index = new_index + rb_event_length(event);
  1692. addr = (unsigned long)event;
  1693. addr &= PAGE_MASK;
  1694. bpage = cpu_buffer->tail_page;
  1695. if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
  1696. unsigned long write_mask =
  1697. local_read(&bpage->write) & ~RB_WRITE_MASK;
  1698. /*
  1699. * This is on the tail page. It is possible that
  1700. * a write could come in and move the tail page
  1701. * and write to the next page. That is fine
  1702. * because we just shorten what is on this page.
  1703. */
  1704. old_index += write_mask;
  1705. new_index += write_mask;
  1706. index = local_cmpxchg(&bpage->write, old_index, new_index);
  1707. if (index == old_index)
  1708. return 1;
  1709. }
  1710. /* could not discard */
  1711. return 0;
  1712. }
  1713. static int
  1714. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1715. u64 ts, u64 *delta)
  1716. {
  1717. struct ring_buffer_event *event;
  1718. int ret;
  1719. WARN_ONCE(*delta > (1ULL << 59),
  1720. KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n",
  1721. (unsigned long long)*delta,
  1722. (unsigned long long)ts,
  1723. (unsigned long long)cpu_buffer->write_stamp);
  1724. /*
  1725. * The delta is too big, we to add a
  1726. * new timestamp.
  1727. */
  1728. event = __rb_reserve_next(cpu_buffer,
  1729. RINGBUF_TYPE_TIME_EXTEND,
  1730. RB_LEN_TIME_EXTEND,
  1731. ts);
  1732. if (!event)
  1733. return -EBUSY;
  1734. if (PTR_ERR(event) == -EAGAIN)
  1735. return -EAGAIN;
  1736. /* Only a commited time event can update the write stamp */
  1737. if (rb_event_is_commit(cpu_buffer, event)) {
  1738. /*
  1739. * If this is the first on the page, then it was
  1740. * updated with the page itself. Try to discard it
  1741. * and if we can't just make it zero.
  1742. */
  1743. if (rb_event_index(event)) {
  1744. event->time_delta = *delta & TS_MASK;
  1745. event->array[0] = *delta >> TS_SHIFT;
  1746. } else {
  1747. /* try to discard, since we do not need this */
  1748. if (!rb_try_to_discard(cpu_buffer, event)) {
  1749. /* nope, just zero it */
  1750. event->time_delta = 0;
  1751. event->array[0] = 0;
  1752. }
  1753. }
  1754. cpu_buffer->write_stamp = ts;
  1755. /* let the caller know this was the commit */
  1756. ret = 1;
  1757. } else {
  1758. /* Try to discard the event */
  1759. if (!rb_try_to_discard(cpu_buffer, event)) {
  1760. /* Darn, this is just wasted space */
  1761. event->time_delta = 0;
  1762. event->array[0] = 0;
  1763. }
  1764. ret = 0;
  1765. }
  1766. *delta = 0;
  1767. return ret;
  1768. }
  1769. static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1770. {
  1771. local_inc(&cpu_buffer->committing);
  1772. local_inc(&cpu_buffer->commits);
  1773. }
  1774. static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
  1775. {
  1776. unsigned long commits;
  1777. if (RB_WARN_ON(cpu_buffer,
  1778. !local_read(&cpu_buffer->committing)))
  1779. return;
  1780. again:
  1781. commits = local_read(&cpu_buffer->commits);
  1782. /* synchronize with interrupts */
  1783. barrier();
  1784. if (local_read(&cpu_buffer->committing) == 1)
  1785. rb_set_commit_to_write(cpu_buffer);
  1786. local_dec(&cpu_buffer->committing);
  1787. /* synchronize with interrupts */
  1788. barrier();
  1789. /*
  1790. * Need to account for interrupts coming in between the
  1791. * updating of the commit page and the clearing of the
  1792. * committing counter.
  1793. */
  1794. if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
  1795. !local_read(&cpu_buffer->committing)) {
  1796. local_inc(&cpu_buffer->committing);
  1797. goto again;
  1798. }
  1799. }
  1800. static struct ring_buffer_event *
  1801. rb_reserve_next_event(struct ring_buffer *buffer,
  1802. struct ring_buffer_per_cpu *cpu_buffer,
  1803. unsigned long length)
  1804. {
  1805. struct ring_buffer_event *event;
  1806. u64 ts, delta = 0;
  1807. int commit = 0;
  1808. int nr_loops = 0;
  1809. rb_start_commit(cpu_buffer);
  1810. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  1811. /*
  1812. * Due to the ability to swap a cpu buffer from a buffer
  1813. * it is possible it was swapped before we committed.
  1814. * (committing stops a swap). We check for it here and
  1815. * if it happened, we have to fail the write.
  1816. */
  1817. barrier();
  1818. if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
  1819. local_dec(&cpu_buffer->committing);
  1820. local_dec(&cpu_buffer->commits);
  1821. return NULL;
  1822. }
  1823. #endif
  1824. length = rb_calculate_event_length(length);
  1825. again:
  1826. /*
  1827. * We allow for interrupts to reenter here and do a trace.
  1828. * If one does, it will cause this original code to loop
  1829. * back here. Even with heavy interrupts happening, this
  1830. * should only happen a few times in a row. If this happens
  1831. * 1000 times in a row, there must be either an interrupt
  1832. * storm or we have something buggy.
  1833. * Bail!
  1834. */
  1835. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  1836. goto out_fail;
  1837. ts = rb_time_stamp(cpu_buffer->buffer);
  1838. /*
  1839. * Only the first commit can update the timestamp.
  1840. * Yes there is a race here. If an interrupt comes in
  1841. * just after the conditional and it traces too, then it
  1842. * will also check the deltas. More than one timestamp may
  1843. * also be made. But only the entry that did the actual
  1844. * commit will be something other than zero.
  1845. */
  1846. if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
  1847. rb_page_write(cpu_buffer->tail_page) ==
  1848. rb_commit_index(cpu_buffer))) {
  1849. u64 diff;
  1850. diff = ts - cpu_buffer->write_stamp;
  1851. /* make sure this diff is calculated here */
  1852. barrier();
  1853. /* Did the write stamp get updated already? */
  1854. if (unlikely(ts < cpu_buffer->write_stamp))
  1855. goto get_event;
  1856. delta = diff;
  1857. if (unlikely(test_time_stamp(delta))) {
  1858. commit = rb_add_time_stamp(cpu_buffer, ts, &delta);
  1859. if (commit == -EBUSY)
  1860. goto out_fail;
  1861. if (commit == -EAGAIN)
  1862. goto again;
  1863. RB_WARN_ON(cpu_buffer, commit < 0);
  1864. }
  1865. }
  1866. get_event:
  1867. event = __rb_reserve_next(cpu_buffer, 0, length, ts);
  1868. if (unlikely(PTR_ERR(event) == -EAGAIN))
  1869. goto again;
  1870. if (!event)
  1871. goto out_fail;
  1872. if (!rb_event_is_commit(cpu_buffer, event))
  1873. delta = 0;
  1874. event->time_delta = delta;
  1875. return event;
  1876. out_fail:
  1877. rb_end_commit(cpu_buffer);
  1878. return NULL;
  1879. }
  1880. #ifdef CONFIG_TRACING
  1881. #define TRACE_RECURSIVE_DEPTH 16
  1882. static int trace_recursive_lock(void)
  1883. {
  1884. current->trace_recursion++;
  1885. if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
  1886. return 0;
  1887. /* Disable all tracing before we do anything else */
  1888. tracing_off_permanent();
  1889. printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
  1890. "HC[%lu]:SC[%lu]:NMI[%lu]\n",
  1891. current->trace_recursion,
  1892. hardirq_count() >> HARDIRQ_SHIFT,
  1893. softirq_count() >> SOFTIRQ_SHIFT,
  1894. in_nmi());
  1895. WARN_ON_ONCE(1);
  1896. return -1;
  1897. }
  1898. static void trace_recursive_unlock(void)
  1899. {
  1900. WARN_ON_ONCE(!current->trace_recursion);
  1901. current->trace_recursion--;
  1902. }
  1903. #else
  1904. #define trace_recursive_lock() (0)
  1905. #define trace_recursive_unlock() do { } while (0)
  1906. #endif
  1907. /**
  1908. * ring_buffer_lock_reserve - reserve a part of the buffer
  1909. * @buffer: the ring buffer to reserve from
  1910. * @length: the length of the data to reserve (excluding event header)
  1911. *
  1912. * Returns a reseverd event on the ring buffer to copy directly to.
  1913. * The user of this interface will need to get the body to write into
  1914. * and can use the ring_buffer_event_data() interface.
  1915. *
  1916. * The length is the length of the data needed, not the event length
  1917. * which also includes the event header.
  1918. *
  1919. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  1920. * If NULL is returned, then nothing has been allocated or locked.
  1921. */
  1922. struct ring_buffer_event *
  1923. ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
  1924. {
  1925. struct ring_buffer_per_cpu *cpu_buffer;
  1926. struct ring_buffer_event *event;
  1927. int cpu;
  1928. if (ring_buffer_flags != RB_BUFFERS_ON)
  1929. return NULL;
  1930. /* If we are tracing schedule, we don't want to recurse */
  1931. preempt_disable_notrace();
  1932. if (atomic_read(&buffer->record_disabled))
  1933. goto out_nocheck;
  1934. if (trace_recursive_lock())
  1935. goto out_nocheck;
  1936. cpu = raw_smp_processor_id();
  1937. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1938. goto out;
  1939. cpu_buffer = buffer->buffers[cpu];
  1940. if (atomic_read(&cpu_buffer->record_disabled))
  1941. goto out;
  1942. if (length > BUF_MAX_DATA_SIZE)
  1943. goto out;
  1944. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  1945. if (!event)
  1946. goto out;
  1947. return event;
  1948. out:
  1949. trace_recursive_unlock();
  1950. out_nocheck:
  1951. preempt_enable_notrace();
  1952. return NULL;
  1953. }
  1954. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  1955. static void
  1956. rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1957. struct ring_buffer_event *event)
  1958. {
  1959. /*
  1960. * The event first in the commit queue updates the
  1961. * time stamp.
  1962. */
  1963. if (rb_event_is_commit(cpu_buffer, event))
  1964. cpu_buffer->write_stamp += event->time_delta;
  1965. }
  1966. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1967. struct ring_buffer_event *event)
  1968. {
  1969. local_inc(&cpu_buffer->entries);
  1970. rb_update_write_stamp(cpu_buffer, event);
  1971. rb_end_commit(cpu_buffer);
  1972. }
  1973. /**
  1974. * ring_buffer_unlock_commit - commit a reserved
  1975. * @buffer: The buffer to commit to
  1976. * @event: The event pointer to commit.
  1977. *
  1978. * This commits the data to the ring buffer, and releases any locks held.
  1979. *
  1980. * Must be paired with ring_buffer_lock_reserve.
  1981. */
  1982. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  1983. struct ring_buffer_event *event)
  1984. {
  1985. struct ring_buffer_per_cpu *cpu_buffer;
  1986. int cpu = raw_smp_processor_id();
  1987. cpu_buffer = buffer->buffers[cpu];
  1988. rb_commit(cpu_buffer, event);
  1989. trace_recursive_unlock();
  1990. preempt_enable_notrace();
  1991. return 0;
  1992. }
  1993. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  1994. static inline void rb_event_discard(struct ring_buffer_event *event)
  1995. {
  1996. /* array[0] holds the actual length for the discarded event */
  1997. event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
  1998. event->type_len = RINGBUF_TYPE_PADDING;
  1999. /* time delta must be non zero */
  2000. if (!event->time_delta)
  2001. event->time_delta = 1;
  2002. }
  2003. /*
  2004. * Decrement the entries to the page that an event is on.
  2005. * The event does not even need to exist, only the pointer
  2006. * to the page it is on. This may only be called before the commit
  2007. * takes place.
  2008. */
  2009. static inline void
  2010. rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
  2011. struct ring_buffer_event *event)
  2012. {
  2013. unsigned long addr = (unsigned long)event;
  2014. struct buffer_page *bpage = cpu_buffer->commit_page;
  2015. struct buffer_page *start;
  2016. addr &= PAGE_MASK;
  2017. /* Do the likely case first */
  2018. if (likely(bpage->page == (void *)addr)) {
  2019. local_dec(&bpage->entries);
  2020. return;
  2021. }
  2022. /*
  2023. * Because the commit page may be on the reader page we
  2024. * start with the next page and check the end loop there.
  2025. */
  2026. rb_inc_page(cpu_buffer, &bpage);
  2027. start = bpage;
  2028. do {
  2029. if (bpage->page == (void *)addr) {
  2030. local_dec(&bpage->entries);
  2031. return;
  2032. }
  2033. rb_inc_page(cpu_buffer, &bpage);
  2034. } while (bpage != start);
  2035. /* commit not part of this buffer?? */
  2036. RB_WARN_ON(cpu_buffer, 1);
  2037. }
  2038. /**
  2039. * ring_buffer_commit_discard - discard an event that has not been committed
  2040. * @buffer: the ring buffer
  2041. * @event: non committed event to discard
  2042. *
  2043. * Sometimes an event that is in the ring buffer needs to be ignored.
  2044. * This function lets the user discard an event in the ring buffer
  2045. * and then that event will not be read later.
  2046. *
  2047. * This function only works if it is called before the the item has been
  2048. * committed. It will try to free the event from the ring buffer
  2049. * if another event has not been added behind it.
  2050. *
  2051. * If another event has been added behind it, it will set the event
  2052. * up as discarded, and perform the commit.
  2053. *
  2054. * If this function is called, do not call ring_buffer_unlock_commit on
  2055. * the event.
  2056. */
  2057. void ring_buffer_discard_commit(struct ring_buffer *buffer,
  2058. struct ring_buffer_event *event)
  2059. {
  2060. struct ring_buffer_per_cpu *cpu_buffer;
  2061. int cpu;
  2062. /* The event is discarded regardless */
  2063. rb_event_discard(event);
  2064. cpu = smp_processor_id();
  2065. cpu_buffer = buffer->buffers[cpu];
  2066. /*
  2067. * This must only be called if the event has not been
  2068. * committed yet. Thus we can assume that preemption
  2069. * is still disabled.
  2070. */
  2071. RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
  2072. rb_decrement_entry(cpu_buffer, event);
  2073. if (rb_try_to_discard(cpu_buffer, event))
  2074. goto out;
  2075. /*
  2076. * The commit is still visible by the reader, so we
  2077. * must still update the timestamp.
  2078. */
  2079. rb_update_write_stamp(cpu_buffer, event);
  2080. out:
  2081. rb_end_commit(cpu_buffer);
  2082. trace_recursive_unlock();
  2083. preempt_enable_notrace();
  2084. }
  2085. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  2086. /**
  2087. * ring_buffer_write - write data to the buffer without reserving
  2088. * @buffer: The ring buffer to write to.
  2089. * @length: The length of the data being written (excluding the event header)
  2090. * @data: The data to write to the buffer.
  2091. *
  2092. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  2093. * one function. If you already have the data to write to the buffer, it
  2094. * may be easier to simply call this function.
  2095. *
  2096. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  2097. * and not the length of the event which would hold the header.
  2098. */
  2099. int ring_buffer_write(struct ring_buffer *buffer,
  2100. unsigned long length,
  2101. void *data)
  2102. {
  2103. struct ring_buffer_per_cpu *cpu_buffer;
  2104. struct ring_buffer_event *event;
  2105. void *body;
  2106. int ret = -EBUSY;
  2107. int cpu;
  2108. if (ring_buffer_flags != RB_BUFFERS_ON)
  2109. return -EBUSY;
  2110. preempt_disable_notrace();
  2111. if (atomic_read(&buffer->record_disabled))
  2112. goto out;
  2113. cpu = raw_smp_processor_id();
  2114. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2115. goto out;
  2116. cpu_buffer = buffer->buffers[cpu];
  2117. if (atomic_read(&cpu_buffer->record_disabled))
  2118. goto out;
  2119. if (length > BUF_MAX_DATA_SIZE)
  2120. goto out;
  2121. event = rb_reserve_next_event(buffer, cpu_buffer, length);
  2122. if (!event)
  2123. goto out;
  2124. body = rb_event_data(event);
  2125. memcpy(body, data, length);
  2126. rb_commit(cpu_buffer, event);
  2127. ret = 0;
  2128. out:
  2129. preempt_enable_notrace();
  2130. return ret;
  2131. }
  2132. EXPORT_SYMBOL_GPL(ring_buffer_write);
  2133. static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  2134. {
  2135. struct buffer_page *reader = cpu_buffer->reader_page;
  2136. struct buffer_page *head = rb_set_head_page(cpu_buffer);
  2137. struct buffer_page *commit = cpu_buffer->commit_page;
  2138. /* In case of error, head will be NULL */
  2139. if (unlikely(!head))
  2140. return 1;
  2141. return reader->read == rb_page_commit(reader) &&
  2142. (commit == reader ||
  2143. (commit == head &&
  2144. head->read == rb_page_commit(commit)));
  2145. }
  2146. /**
  2147. * ring_buffer_record_disable - stop all writes into the buffer
  2148. * @buffer: The ring buffer to stop writes to.
  2149. *
  2150. * This prevents all writes to the buffer. Any attempt to write
  2151. * to the buffer after this will fail and return NULL.
  2152. *
  2153. * The caller should call synchronize_sched() after this.
  2154. */
  2155. void ring_buffer_record_disable(struct ring_buffer *buffer)
  2156. {
  2157. atomic_inc(&buffer->record_disabled);
  2158. }
  2159. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  2160. /**
  2161. * ring_buffer_record_enable - enable writes to the buffer
  2162. * @buffer: The ring buffer to enable writes
  2163. *
  2164. * Note, multiple disables will need the same number of enables
  2165. * to truly enable the writing (much like preempt_disable).
  2166. */
  2167. void ring_buffer_record_enable(struct ring_buffer *buffer)
  2168. {
  2169. atomic_dec(&buffer->record_disabled);
  2170. }
  2171. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  2172. /**
  2173. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  2174. * @buffer: The ring buffer to stop writes to.
  2175. * @cpu: The CPU buffer to stop
  2176. *
  2177. * This prevents all writes to the buffer. Any attempt to write
  2178. * to the buffer after this will fail and return NULL.
  2179. *
  2180. * The caller should call synchronize_sched() after this.
  2181. */
  2182. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
  2183. {
  2184. struct ring_buffer_per_cpu *cpu_buffer;
  2185. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2186. return;
  2187. cpu_buffer = buffer->buffers[cpu];
  2188. atomic_inc(&cpu_buffer->record_disabled);
  2189. }
  2190. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  2191. /**
  2192. * ring_buffer_record_enable_cpu - enable writes to the buffer
  2193. * @buffer: The ring buffer to enable writes
  2194. * @cpu: The CPU to enable.
  2195. *
  2196. * Note, multiple disables will need the same number of enables
  2197. * to truly enable the writing (much like preempt_disable).
  2198. */
  2199. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
  2200. {
  2201. struct ring_buffer_per_cpu *cpu_buffer;
  2202. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2203. return;
  2204. cpu_buffer = buffer->buffers[cpu];
  2205. atomic_dec(&cpu_buffer->record_disabled);
  2206. }
  2207. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  2208. /**
  2209. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  2210. * @buffer: The ring buffer
  2211. * @cpu: The per CPU buffer to get the entries from.
  2212. */
  2213. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
  2214. {
  2215. struct ring_buffer_per_cpu *cpu_buffer;
  2216. unsigned long ret;
  2217. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2218. return 0;
  2219. cpu_buffer = buffer->buffers[cpu];
  2220. ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
  2221. - cpu_buffer->read;
  2222. return ret;
  2223. }
  2224. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  2225. /**
  2226. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  2227. * @buffer: The ring buffer
  2228. * @cpu: The per CPU buffer to get the number of overruns from
  2229. */
  2230. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  2231. {
  2232. struct ring_buffer_per_cpu *cpu_buffer;
  2233. unsigned long ret;
  2234. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2235. return 0;
  2236. cpu_buffer = buffer->buffers[cpu];
  2237. ret = local_read(&cpu_buffer->overrun);
  2238. return ret;
  2239. }
  2240. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  2241. /**
  2242. * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
  2243. * @buffer: The ring buffer
  2244. * @cpu: The per CPU buffer to get the number of overruns from
  2245. */
  2246. unsigned long
  2247. ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
  2248. {
  2249. struct ring_buffer_per_cpu *cpu_buffer;
  2250. unsigned long ret;
  2251. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2252. return 0;
  2253. cpu_buffer = buffer->buffers[cpu];
  2254. ret = local_read(&cpu_buffer->commit_overrun);
  2255. return ret;
  2256. }
  2257. EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
  2258. /**
  2259. * ring_buffer_entries - get the number of entries in a buffer
  2260. * @buffer: The ring buffer
  2261. *
  2262. * Returns the total number of entries in the ring buffer
  2263. * (all CPU entries)
  2264. */
  2265. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  2266. {
  2267. struct ring_buffer_per_cpu *cpu_buffer;
  2268. unsigned long entries = 0;
  2269. int cpu;
  2270. /* if you care about this being correct, lock the buffer */
  2271. for_each_buffer_cpu(buffer, cpu) {
  2272. cpu_buffer = buffer->buffers[cpu];
  2273. entries += (local_read(&cpu_buffer->entries) -
  2274. local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
  2275. }
  2276. return entries;
  2277. }
  2278. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  2279. /**
  2280. * ring_buffer_overruns - get the number of overruns in buffer
  2281. * @buffer: The ring buffer
  2282. *
  2283. * Returns the total number of overruns in the ring buffer
  2284. * (all CPU entries)
  2285. */
  2286. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  2287. {
  2288. struct ring_buffer_per_cpu *cpu_buffer;
  2289. unsigned long overruns = 0;
  2290. int cpu;
  2291. /* if you care about this being correct, lock the buffer */
  2292. for_each_buffer_cpu(buffer, cpu) {
  2293. cpu_buffer = buffer->buffers[cpu];
  2294. overruns += local_read(&cpu_buffer->overrun);
  2295. }
  2296. return overruns;
  2297. }
  2298. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  2299. static void rb_iter_reset(struct ring_buffer_iter *iter)
  2300. {
  2301. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2302. /* Iterator usage is expected to have record disabled */
  2303. if (list_empty(&cpu_buffer->reader_page->list)) {
  2304. iter->head_page = rb_set_head_page(cpu_buffer);
  2305. if (unlikely(!iter->head_page))
  2306. return;
  2307. iter->head = iter->head_page->read;
  2308. } else {
  2309. iter->head_page = cpu_buffer->reader_page;
  2310. iter->head = cpu_buffer->reader_page->read;
  2311. }
  2312. if (iter->head)
  2313. iter->read_stamp = cpu_buffer->read_stamp;
  2314. else
  2315. iter->read_stamp = iter->head_page->page->time_stamp;
  2316. iter->cache_reader_page = cpu_buffer->reader_page;
  2317. iter->cache_read = cpu_buffer->read;
  2318. }
  2319. /**
  2320. * ring_buffer_iter_reset - reset an iterator
  2321. * @iter: The iterator to reset
  2322. *
  2323. * Resets the iterator, so that it will start from the beginning
  2324. * again.
  2325. */
  2326. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  2327. {
  2328. struct ring_buffer_per_cpu *cpu_buffer;
  2329. unsigned long flags;
  2330. if (!iter)
  2331. return;
  2332. cpu_buffer = iter->cpu_buffer;
  2333. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2334. rb_iter_reset(iter);
  2335. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2336. }
  2337. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  2338. /**
  2339. * ring_buffer_iter_empty - check if an iterator has no more to read
  2340. * @iter: The iterator to check
  2341. */
  2342. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  2343. {
  2344. struct ring_buffer_per_cpu *cpu_buffer;
  2345. cpu_buffer = iter->cpu_buffer;
  2346. return iter->head_page == cpu_buffer->commit_page &&
  2347. iter->head == rb_commit_index(cpu_buffer);
  2348. }
  2349. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  2350. static void
  2351. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  2352. struct ring_buffer_event *event)
  2353. {
  2354. u64 delta;
  2355. switch (event->type_len) {
  2356. case RINGBUF_TYPE_PADDING:
  2357. return;
  2358. case RINGBUF_TYPE_TIME_EXTEND:
  2359. delta = event->array[0];
  2360. delta <<= TS_SHIFT;
  2361. delta += event->time_delta;
  2362. cpu_buffer->read_stamp += delta;
  2363. return;
  2364. case RINGBUF_TYPE_TIME_STAMP:
  2365. /* FIXME: not implemented */
  2366. return;
  2367. case RINGBUF_TYPE_DATA:
  2368. cpu_buffer->read_stamp += event->time_delta;
  2369. return;
  2370. default:
  2371. BUG();
  2372. }
  2373. return;
  2374. }
  2375. static void
  2376. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  2377. struct ring_buffer_event *event)
  2378. {
  2379. u64 delta;
  2380. switch (event->type_len) {
  2381. case RINGBUF_TYPE_PADDING:
  2382. return;
  2383. case RINGBUF_TYPE_TIME_EXTEND:
  2384. delta = event->array[0];
  2385. delta <<= TS_SHIFT;
  2386. delta += event->time_delta;
  2387. iter->read_stamp += delta;
  2388. return;
  2389. case RINGBUF_TYPE_TIME_STAMP:
  2390. /* FIXME: not implemented */
  2391. return;
  2392. case RINGBUF_TYPE_DATA:
  2393. iter->read_stamp += event->time_delta;
  2394. return;
  2395. default:
  2396. BUG();
  2397. }
  2398. return;
  2399. }
  2400. static struct buffer_page *
  2401. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  2402. {
  2403. struct buffer_page *reader = NULL;
  2404. unsigned long overwrite;
  2405. unsigned long flags;
  2406. int nr_loops = 0;
  2407. int ret;
  2408. local_irq_save(flags);
  2409. arch_spin_lock(&cpu_buffer->lock);
  2410. again:
  2411. /*
  2412. * This should normally only loop twice. But because the
  2413. * start of the reader inserts an empty page, it causes
  2414. * a case where we will loop three times. There should be no
  2415. * reason to loop four times (that I know of).
  2416. */
  2417. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  2418. reader = NULL;
  2419. goto out;
  2420. }
  2421. reader = cpu_buffer->reader_page;
  2422. /* If there's more to read, return this page */
  2423. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  2424. goto out;
  2425. /* Never should we have an index greater than the size */
  2426. if (RB_WARN_ON(cpu_buffer,
  2427. cpu_buffer->reader_page->read > rb_page_size(reader)))
  2428. goto out;
  2429. /* check if we caught up to the tail */
  2430. reader = NULL;
  2431. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  2432. goto out;
  2433. /*
  2434. * Reset the reader page to size zero.
  2435. */
  2436. local_set(&cpu_buffer->reader_page->write, 0);
  2437. local_set(&cpu_buffer->reader_page->entries, 0);
  2438. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2439. cpu_buffer->reader_page->real_end = 0;
  2440. spin:
  2441. /*
  2442. * Splice the empty reader page into the list around the head.
  2443. */
  2444. reader = rb_set_head_page(cpu_buffer);
  2445. cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
  2446. cpu_buffer->reader_page->list.prev = reader->list.prev;
  2447. /*
  2448. * cpu_buffer->pages just needs to point to the buffer, it
  2449. * has no specific buffer page to point to. Lets move it out
  2450. * of our way so we don't accidently swap it.
  2451. */
  2452. cpu_buffer->pages = reader->list.prev;
  2453. /* The reader page will be pointing to the new head */
  2454. rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
  2455. /*
  2456. * We want to make sure we read the overruns after we set up our
  2457. * pointers to the next object. The writer side does a
  2458. * cmpxchg to cross pages which acts as the mb on the writer
  2459. * side. Note, the reader will constantly fail the swap
  2460. * while the writer is updating the pointers, so this
  2461. * guarantees that the overwrite recorded here is the one we
  2462. * want to compare with the last_overrun.
  2463. */
  2464. smp_mb();
  2465. overwrite = local_read(&(cpu_buffer->overrun));
  2466. /*
  2467. * Here's the tricky part.
  2468. *
  2469. * We need to move the pointer past the header page.
  2470. * But we can only do that if a writer is not currently
  2471. * moving it. The page before the header page has the
  2472. * flag bit '1' set if it is pointing to the page we want.
  2473. * but if the writer is in the process of moving it
  2474. * than it will be '2' or already moved '0'.
  2475. */
  2476. ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
  2477. /*
  2478. * If we did not convert it, then we must try again.
  2479. */
  2480. if (!ret)
  2481. goto spin;
  2482. /*
  2483. * Yeah! We succeeded in replacing the page.
  2484. *
  2485. * Now make the new head point back to the reader page.
  2486. */
  2487. rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
  2488. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  2489. /* Finally update the reader page to the new head */
  2490. cpu_buffer->reader_page = reader;
  2491. rb_reset_reader_page(cpu_buffer);
  2492. if (overwrite != cpu_buffer->last_overrun) {
  2493. cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
  2494. cpu_buffer->last_overrun = overwrite;
  2495. }
  2496. goto again;
  2497. out:
  2498. arch_spin_unlock(&cpu_buffer->lock);
  2499. local_irq_restore(flags);
  2500. return reader;
  2501. }
  2502. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  2503. {
  2504. struct ring_buffer_event *event;
  2505. struct buffer_page *reader;
  2506. unsigned length;
  2507. reader = rb_get_reader_page(cpu_buffer);
  2508. /* This function should not be called when buffer is empty */
  2509. if (RB_WARN_ON(cpu_buffer, !reader))
  2510. return;
  2511. event = rb_reader_event(cpu_buffer);
  2512. if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
  2513. cpu_buffer->read++;
  2514. rb_update_read_stamp(cpu_buffer, event);
  2515. length = rb_event_length(event);
  2516. cpu_buffer->reader_page->read += length;
  2517. }
  2518. static void rb_advance_iter(struct ring_buffer_iter *iter)
  2519. {
  2520. struct ring_buffer_per_cpu *cpu_buffer;
  2521. struct ring_buffer_event *event;
  2522. unsigned length;
  2523. cpu_buffer = iter->cpu_buffer;
  2524. /*
  2525. * Check if we are at the end of the buffer.
  2526. */
  2527. if (iter->head >= rb_page_size(iter->head_page)) {
  2528. /* discarded commits can make the page empty */
  2529. if (iter->head_page == cpu_buffer->commit_page)
  2530. return;
  2531. rb_inc_iter(iter);
  2532. return;
  2533. }
  2534. event = rb_iter_head_event(iter);
  2535. length = rb_event_length(event);
  2536. /*
  2537. * This should not be called to advance the header if we are
  2538. * at the tail of the buffer.
  2539. */
  2540. if (RB_WARN_ON(cpu_buffer,
  2541. (iter->head_page == cpu_buffer->commit_page) &&
  2542. (iter->head + length > rb_commit_index(cpu_buffer))))
  2543. return;
  2544. rb_update_iter_read_stamp(iter, event);
  2545. iter->head += length;
  2546. /* check for end of page padding */
  2547. if ((iter->head >= rb_page_size(iter->head_page)) &&
  2548. (iter->head_page != cpu_buffer->commit_page))
  2549. rb_advance_iter(iter);
  2550. }
  2551. static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
  2552. {
  2553. return cpu_buffer->lost_events;
  2554. }
  2555. static struct ring_buffer_event *
  2556. rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
  2557. unsigned long *lost_events)
  2558. {
  2559. struct ring_buffer_event *event;
  2560. struct buffer_page *reader;
  2561. int nr_loops = 0;
  2562. again:
  2563. /*
  2564. * We repeat when a timestamp is encountered. It is possible
  2565. * to get multiple timestamps from an interrupt entering just
  2566. * as one timestamp is about to be written, or from discarded
  2567. * commits. The most that we can have is the number on a single page.
  2568. */
  2569. if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
  2570. return NULL;
  2571. reader = rb_get_reader_page(cpu_buffer);
  2572. if (!reader)
  2573. return NULL;
  2574. event = rb_reader_event(cpu_buffer);
  2575. switch (event->type_len) {
  2576. case RINGBUF_TYPE_PADDING:
  2577. if (rb_null_event(event))
  2578. RB_WARN_ON(cpu_buffer, 1);
  2579. /*
  2580. * Because the writer could be discarding every
  2581. * event it creates (which would probably be bad)
  2582. * if we were to go back to "again" then we may never
  2583. * catch up, and will trigger the warn on, or lock
  2584. * the box. Return the padding, and we will release
  2585. * the current locks, and try again.
  2586. */
  2587. return event;
  2588. case RINGBUF_TYPE_TIME_EXTEND:
  2589. /* Internal data, OK to advance */
  2590. rb_advance_reader(cpu_buffer);
  2591. goto again;
  2592. case RINGBUF_TYPE_TIME_STAMP:
  2593. /* FIXME: not implemented */
  2594. rb_advance_reader(cpu_buffer);
  2595. goto again;
  2596. case RINGBUF_TYPE_DATA:
  2597. if (ts) {
  2598. *ts = cpu_buffer->read_stamp + event->time_delta;
  2599. ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
  2600. cpu_buffer->cpu, ts);
  2601. }
  2602. if (lost_events)
  2603. *lost_events = rb_lost_events(cpu_buffer);
  2604. return event;
  2605. default:
  2606. BUG();
  2607. }
  2608. return NULL;
  2609. }
  2610. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  2611. static struct ring_buffer_event *
  2612. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2613. {
  2614. struct ring_buffer *buffer;
  2615. struct ring_buffer_per_cpu *cpu_buffer;
  2616. struct ring_buffer_event *event;
  2617. int nr_loops = 0;
  2618. cpu_buffer = iter->cpu_buffer;
  2619. buffer = cpu_buffer->buffer;
  2620. /*
  2621. * Check if someone performed a consuming read to
  2622. * the buffer. A consuming read invalidates the iterator
  2623. * and we need to reset the iterator in this case.
  2624. */
  2625. if (unlikely(iter->cache_read != cpu_buffer->read ||
  2626. iter->cache_reader_page != cpu_buffer->reader_page))
  2627. rb_iter_reset(iter);
  2628. again:
  2629. if (ring_buffer_iter_empty(iter))
  2630. return NULL;
  2631. /*
  2632. * We repeat when a timestamp is encountered.
  2633. * We can get multiple timestamps by nested interrupts or also
  2634. * if filtering is on (discarding commits). Since discarding
  2635. * commits can be frequent we can get a lot of timestamps.
  2636. * But we limit them by not adding timestamps if they begin
  2637. * at the start of a page.
  2638. */
  2639. if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
  2640. return NULL;
  2641. if (rb_per_cpu_empty(cpu_buffer))
  2642. return NULL;
  2643. if (iter->head >= local_read(&iter->head_page->page->commit)) {
  2644. rb_inc_iter(iter);
  2645. goto again;
  2646. }
  2647. event = rb_iter_head_event(iter);
  2648. switch (event->type_len) {
  2649. case RINGBUF_TYPE_PADDING:
  2650. if (rb_null_event(event)) {
  2651. rb_inc_iter(iter);
  2652. goto again;
  2653. }
  2654. rb_advance_iter(iter);
  2655. return event;
  2656. case RINGBUF_TYPE_TIME_EXTEND:
  2657. /* Internal data, OK to advance */
  2658. rb_advance_iter(iter);
  2659. goto again;
  2660. case RINGBUF_TYPE_TIME_STAMP:
  2661. /* FIXME: not implemented */
  2662. rb_advance_iter(iter);
  2663. goto again;
  2664. case RINGBUF_TYPE_DATA:
  2665. if (ts) {
  2666. *ts = iter->read_stamp + event->time_delta;
  2667. ring_buffer_normalize_time_stamp(buffer,
  2668. cpu_buffer->cpu, ts);
  2669. }
  2670. return event;
  2671. default:
  2672. BUG();
  2673. }
  2674. return NULL;
  2675. }
  2676. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  2677. static inline int rb_ok_to_lock(void)
  2678. {
  2679. /*
  2680. * If an NMI die dumps out the content of the ring buffer
  2681. * do not grab locks. We also permanently disable the ring
  2682. * buffer too. A one time deal is all you get from reading
  2683. * the ring buffer from an NMI.
  2684. */
  2685. if (likely(!in_nmi()))
  2686. return 1;
  2687. tracing_off_permanent();
  2688. return 0;
  2689. }
  2690. /**
  2691. * ring_buffer_peek - peek at the next event to be read
  2692. * @buffer: The ring buffer to read
  2693. * @cpu: The cpu to peak at
  2694. * @ts: The timestamp counter of this event.
  2695. * @lost_events: a variable to store if events were lost (may be NULL)
  2696. *
  2697. * This will return the event that will be read next, but does
  2698. * not consume the data.
  2699. */
  2700. struct ring_buffer_event *
  2701. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
  2702. unsigned long *lost_events)
  2703. {
  2704. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2705. struct ring_buffer_event *event;
  2706. unsigned long flags;
  2707. int dolock;
  2708. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2709. return NULL;
  2710. dolock = rb_ok_to_lock();
  2711. again:
  2712. local_irq_save(flags);
  2713. if (dolock)
  2714. spin_lock(&cpu_buffer->reader_lock);
  2715. event = rb_buffer_peek(cpu_buffer, ts, lost_events);
  2716. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2717. rb_advance_reader(cpu_buffer);
  2718. if (dolock)
  2719. spin_unlock(&cpu_buffer->reader_lock);
  2720. local_irq_restore(flags);
  2721. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2722. goto again;
  2723. return event;
  2724. }
  2725. /**
  2726. * ring_buffer_iter_peek - peek at the next event to be read
  2727. * @iter: The ring buffer iterator
  2728. * @ts: The timestamp counter of this event.
  2729. *
  2730. * This will return the event that will be read next, but does
  2731. * not increment the iterator.
  2732. */
  2733. struct ring_buffer_event *
  2734. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2735. {
  2736. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2737. struct ring_buffer_event *event;
  2738. unsigned long flags;
  2739. again:
  2740. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2741. event = rb_iter_peek(iter, ts);
  2742. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2743. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2744. goto again;
  2745. return event;
  2746. }
  2747. /**
  2748. * ring_buffer_consume - return an event and consume it
  2749. * @buffer: The ring buffer to get the next event from
  2750. * @cpu: the cpu to read the buffer from
  2751. * @ts: a variable to store the timestamp (may be NULL)
  2752. * @lost_events: a variable to store if events were lost (may be NULL)
  2753. *
  2754. * Returns the next event in the ring buffer, and that event is consumed.
  2755. * Meaning, that sequential reads will keep returning a different event,
  2756. * and eventually empty the ring buffer if the producer is slower.
  2757. */
  2758. struct ring_buffer_event *
  2759. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
  2760. unsigned long *lost_events)
  2761. {
  2762. struct ring_buffer_per_cpu *cpu_buffer;
  2763. struct ring_buffer_event *event = NULL;
  2764. unsigned long flags;
  2765. int dolock;
  2766. dolock = rb_ok_to_lock();
  2767. again:
  2768. /* might be called in atomic */
  2769. preempt_disable();
  2770. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2771. goto out;
  2772. cpu_buffer = buffer->buffers[cpu];
  2773. local_irq_save(flags);
  2774. if (dolock)
  2775. spin_lock(&cpu_buffer->reader_lock);
  2776. event = rb_buffer_peek(cpu_buffer, ts, lost_events);
  2777. if (event) {
  2778. cpu_buffer->lost_events = 0;
  2779. rb_advance_reader(cpu_buffer);
  2780. }
  2781. if (dolock)
  2782. spin_unlock(&cpu_buffer->reader_lock);
  2783. local_irq_restore(flags);
  2784. out:
  2785. preempt_enable();
  2786. if (event && event->type_len == RINGBUF_TYPE_PADDING)
  2787. goto again;
  2788. return event;
  2789. }
  2790. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  2791. /**
  2792. * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
  2793. * @buffer: The ring buffer to read from
  2794. * @cpu: The cpu buffer to iterate over
  2795. *
  2796. * This performs the initial preparations necessary to iterate
  2797. * through the buffer. Memory is allocated, buffer recording
  2798. * is disabled, and the iterator pointer is returned to the caller.
  2799. *
  2800. * Disabling buffer recordng prevents the reading from being
  2801. * corrupted. This is not a consuming read, so a producer is not
  2802. * expected.
  2803. *
  2804. * After a sequence of ring_buffer_read_prepare calls, the user is
  2805. * expected to make at least one call to ring_buffer_prepare_sync.
  2806. * Afterwards, ring_buffer_read_start is invoked to get things going
  2807. * for real.
  2808. *
  2809. * This overall must be paired with ring_buffer_finish.
  2810. */
  2811. struct ring_buffer_iter *
  2812. ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
  2813. {
  2814. struct ring_buffer_per_cpu *cpu_buffer;
  2815. struct ring_buffer_iter *iter;
  2816. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2817. return NULL;
  2818. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2819. if (!iter)
  2820. return NULL;
  2821. cpu_buffer = buffer->buffers[cpu];
  2822. iter->cpu_buffer = cpu_buffer;
  2823. atomic_inc(&cpu_buffer->record_disabled);
  2824. return iter;
  2825. }
  2826. EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
  2827. /**
  2828. * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
  2829. *
  2830. * All previously invoked ring_buffer_read_prepare calls to prepare
  2831. * iterators will be synchronized. Afterwards, read_buffer_read_start
  2832. * calls on those iterators are allowed.
  2833. */
  2834. void
  2835. ring_buffer_read_prepare_sync(void)
  2836. {
  2837. synchronize_sched();
  2838. }
  2839. EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
  2840. /**
  2841. * ring_buffer_read_start - start a non consuming read of the buffer
  2842. * @iter: The iterator returned by ring_buffer_read_prepare
  2843. *
  2844. * This finalizes the startup of an iteration through the buffer.
  2845. * The iterator comes from a call to ring_buffer_read_prepare and
  2846. * an intervening ring_buffer_read_prepare_sync must have been
  2847. * performed.
  2848. *
  2849. * Must be paired with ring_buffer_finish.
  2850. */
  2851. void
  2852. ring_buffer_read_start(struct ring_buffer_iter *iter)
  2853. {
  2854. struct ring_buffer_per_cpu *cpu_buffer;
  2855. unsigned long flags;
  2856. if (!iter)
  2857. return;
  2858. cpu_buffer = iter->cpu_buffer;
  2859. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2860. arch_spin_lock(&cpu_buffer->lock);
  2861. rb_iter_reset(iter);
  2862. arch_spin_unlock(&cpu_buffer->lock);
  2863. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2864. }
  2865. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  2866. /**
  2867. * ring_buffer_finish - finish reading the iterator of the buffer
  2868. * @iter: The iterator retrieved by ring_buffer_start
  2869. *
  2870. * This re-enables the recording to the buffer, and frees the
  2871. * iterator.
  2872. */
  2873. void
  2874. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  2875. {
  2876. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2877. atomic_dec(&cpu_buffer->record_disabled);
  2878. kfree(iter);
  2879. }
  2880. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  2881. /**
  2882. * ring_buffer_read - read the next item in the ring buffer by the iterator
  2883. * @iter: The ring buffer iterator
  2884. * @ts: The time stamp of the event read.
  2885. *
  2886. * This reads the next event in the ring buffer and increments the iterator.
  2887. */
  2888. struct ring_buffer_event *
  2889. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  2890. {
  2891. struct ring_buffer_event *event;
  2892. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2893. unsigned long flags;
  2894. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2895. again:
  2896. event = rb_iter_peek(iter, ts);
  2897. if (!event)
  2898. goto out;
  2899. if (event->type_len == RINGBUF_TYPE_PADDING)
  2900. goto again;
  2901. rb_advance_iter(iter);
  2902. out:
  2903. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2904. return event;
  2905. }
  2906. EXPORT_SYMBOL_GPL(ring_buffer_read);
  2907. /**
  2908. * ring_buffer_size - return the size of the ring buffer (in bytes)
  2909. * @buffer: The ring buffer.
  2910. */
  2911. unsigned long ring_buffer_size(struct ring_buffer *buffer)
  2912. {
  2913. return BUF_PAGE_SIZE * buffer->pages;
  2914. }
  2915. EXPORT_SYMBOL_GPL(ring_buffer_size);
  2916. static void
  2917. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  2918. {
  2919. rb_head_page_deactivate(cpu_buffer);
  2920. cpu_buffer->head_page
  2921. = list_entry(cpu_buffer->pages, struct buffer_page, list);
  2922. local_set(&cpu_buffer->head_page->write, 0);
  2923. local_set(&cpu_buffer->head_page->entries, 0);
  2924. local_set(&cpu_buffer->head_page->page->commit, 0);
  2925. cpu_buffer->head_page->read = 0;
  2926. cpu_buffer->tail_page = cpu_buffer->head_page;
  2927. cpu_buffer->commit_page = cpu_buffer->head_page;
  2928. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2929. local_set(&cpu_buffer->reader_page->write, 0);
  2930. local_set(&cpu_buffer->reader_page->entries, 0);
  2931. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2932. cpu_buffer->reader_page->read = 0;
  2933. local_set(&cpu_buffer->commit_overrun, 0);
  2934. local_set(&cpu_buffer->overrun, 0);
  2935. local_set(&cpu_buffer->entries, 0);
  2936. local_set(&cpu_buffer->committing, 0);
  2937. local_set(&cpu_buffer->commits, 0);
  2938. cpu_buffer->read = 0;
  2939. cpu_buffer->write_stamp = 0;
  2940. cpu_buffer->read_stamp = 0;
  2941. cpu_buffer->lost_events = 0;
  2942. cpu_buffer->last_overrun = 0;
  2943. rb_head_page_activate(cpu_buffer);
  2944. }
  2945. /**
  2946. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  2947. * @buffer: The ring buffer to reset a per cpu buffer of
  2948. * @cpu: The CPU buffer to be reset
  2949. */
  2950. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  2951. {
  2952. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2953. unsigned long flags;
  2954. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2955. return;
  2956. atomic_inc(&cpu_buffer->record_disabled);
  2957. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2958. if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
  2959. goto out;
  2960. arch_spin_lock(&cpu_buffer->lock);
  2961. rb_reset_cpu(cpu_buffer);
  2962. arch_spin_unlock(&cpu_buffer->lock);
  2963. out:
  2964. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2965. atomic_dec(&cpu_buffer->record_disabled);
  2966. }
  2967. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  2968. /**
  2969. * ring_buffer_reset - reset a ring buffer
  2970. * @buffer: The ring buffer to reset all cpu buffers
  2971. */
  2972. void ring_buffer_reset(struct ring_buffer *buffer)
  2973. {
  2974. int cpu;
  2975. for_each_buffer_cpu(buffer, cpu)
  2976. ring_buffer_reset_cpu(buffer, cpu);
  2977. }
  2978. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  2979. /**
  2980. * rind_buffer_empty - is the ring buffer empty?
  2981. * @buffer: The ring buffer to test
  2982. */
  2983. int ring_buffer_empty(struct ring_buffer *buffer)
  2984. {
  2985. struct ring_buffer_per_cpu *cpu_buffer;
  2986. unsigned long flags;
  2987. int dolock;
  2988. int cpu;
  2989. int ret;
  2990. dolock = rb_ok_to_lock();
  2991. /* yes this is racy, but if you don't like the race, lock the buffer */
  2992. for_each_buffer_cpu(buffer, cpu) {
  2993. cpu_buffer = buffer->buffers[cpu];
  2994. local_irq_save(flags);
  2995. if (dolock)
  2996. spin_lock(&cpu_buffer->reader_lock);
  2997. ret = rb_per_cpu_empty(cpu_buffer);
  2998. if (dolock)
  2999. spin_unlock(&cpu_buffer->reader_lock);
  3000. local_irq_restore(flags);
  3001. if (!ret)
  3002. return 0;
  3003. }
  3004. return 1;
  3005. }
  3006. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  3007. /**
  3008. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  3009. * @buffer: The ring buffer
  3010. * @cpu: The CPU buffer to test
  3011. */
  3012. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  3013. {
  3014. struct ring_buffer_per_cpu *cpu_buffer;
  3015. unsigned long flags;
  3016. int dolock;
  3017. int ret;
  3018. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3019. return 1;
  3020. dolock = rb_ok_to_lock();
  3021. cpu_buffer = buffer->buffers[cpu];
  3022. local_irq_save(flags);
  3023. if (dolock)
  3024. spin_lock(&cpu_buffer->reader_lock);
  3025. ret = rb_per_cpu_empty(cpu_buffer);
  3026. if (dolock)
  3027. spin_unlock(&cpu_buffer->reader_lock);
  3028. local_irq_restore(flags);
  3029. return ret;
  3030. }
  3031. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  3032. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  3033. /**
  3034. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  3035. * @buffer_a: One buffer to swap with
  3036. * @buffer_b: The other buffer to swap with
  3037. *
  3038. * This function is useful for tracers that want to take a "snapshot"
  3039. * of a CPU buffer and has another back up buffer lying around.
  3040. * it is expected that the tracer handles the cpu buffer not being
  3041. * used at the moment.
  3042. */
  3043. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  3044. struct ring_buffer *buffer_b, int cpu)
  3045. {
  3046. struct ring_buffer_per_cpu *cpu_buffer_a;
  3047. struct ring_buffer_per_cpu *cpu_buffer_b;
  3048. int ret = -EINVAL;
  3049. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  3050. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  3051. goto out;
  3052. /* At least make sure the two buffers are somewhat the same */
  3053. if (buffer_a->pages != buffer_b->pages)
  3054. goto out;
  3055. ret = -EAGAIN;
  3056. if (ring_buffer_flags != RB_BUFFERS_ON)
  3057. goto out;
  3058. if (atomic_read(&buffer_a->record_disabled))
  3059. goto out;
  3060. if (atomic_read(&buffer_b->record_disabled))
  3061. goto out;
  3062. cpu_buffer_a = buffer_a->buffers[cpu];
  3063. cpu_buffer_b = buffer_b->buffers[cpu];
  3064. if (atomic_read(&cpu_buffer_a->record_disabled))
  3065. goto out;
  3066. if (atomic_read(&cpu_buffer_b->record_disabled))
  3067. goto out;
  3068. /*
  3069. * We can't do a synchronize_sched here because this
  3070. * function can be called in atomic context.
  3071. * Normally this will be called from the same CPU as cpu.
  3072. * If not it's up to the caller to protect this.
  3073. */
  3074. atomic_inc(&cpu_buffer_a->record_disabled);
  3075. atomic_inc(&cpu_buffer_b->record_disabled);
  3076. ret = -EBUSY;
  3077. if (local_read(&cpu_buffer_a->committing))
  3078. goto out_dec;
  3079. if (local_read(&cpu_buffer_b->committing))
  3080. goto out_dec;
  3081. buffer_a->buffers[cpu] = cpu_buffer_b;
  3082. buffer_b->buffers[cpu] = cpu_buffer_a;
  3083. cpu_buffer_b->buffer = buffer_a;
  3084. cpu_buffer_a->buffer = buffer_b;
  3085. ret = 0;
  3086. out_dec:
  3087. atomic_dec(&cpu_buffer_a->record_disabled);
  3088. atomic_dec(&cpu_buffer_b->record_disabled);
  3089. out:
  3090. return ret;
  3091. }
  3092. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  3093. #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
  3094. /**
  3095. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  3096. * @buffer: the buffer to allocate for.
  3097. *
  3098. * This function is used in conjunction with ring_buffer_read_page.
  3099. * When reading a full page from the ring buffer, these functions
  3100. * can be used to speed up the process. The calling function should
  3101. * allocate a few pages first with this function. Then when it
  3102. * needs to get pages from the ring buffer, it passes the result
  3103. * of this function into ring_buffer_read_page, which will swap
  3104. * the page that was allocated, with the read page of the buffer.
  3105. *
  3106. * Returns:
  3107. * The page allocated, or NULL on error.
  3108. */
  3109. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
  3110. {
  3111. struct buffer_data_page *bpage;
  3112. unsigned long addr;
  3113. addr = __get_free_page(GFP_KERNEL);
  3114. if (!addr)
  3115. return NULL;
  3116. bpage = (void *)addr;
  3117. rb_init_page(bpage);
  3118. return bpage;
  3119. }
  3120. EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  3121. /**
  3122. * ring_buffer_free_read_page - free an allocated read page
  3123. * @buffer: the buffer the page was allocate for
  3124. * @data: the page to free
  3125. *
  3126. * Free a page allocated from ring_buffer_alloc_read_page.
  3127. */
  3128. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  3129. {
  3130. free_page((unsigned long)data);
  3131. }
  3132. EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
  3133. /**
  3134. * ring_buffer_read_page - extract a page from the ring buffer
  3135. * @buffer: buffer to extract from
  3136. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  3137. * @len: amount to extract
  3138. * @cpu: the cpu of the buffer to extract
  3139. * @full: should the extraction only happen when the page is full.
  3140. *
  3141. * This function will pull out a page from the ring buffer and consume it.
  3142. * @data_page must be the address of the variable that was returned
  3143. * from ring_buffer_alloc_read_page. This is because the page might be used
  3144. * to swap with a page in the ring buffer.
  3145. *
  3146. * for example:
  3147. * rpage = ring_buffer_alloc_read_page(buffer);
  3148. * if (!rpage)
  3149. * return error;
  3150. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  3151. * if (ret >= 0)
  3152. * process_page(rpage, ret);
  3153. *
  3154. * When @full is set, the function will not return true unless
  3155. * the writer is off the reader page.
  3156. *
  3157. * Note: it is up to the calling functions to handle sleeps and wakeups.
  3158. * The ring buffer can be used anywhere in the kernel and can not
  3159. * blindly call wake_up. The layer that uses the ring buffer must be
  3160. * responsible for that.
  3161. *
  3162. * Returns:
  3163. * >=0 if data has been transferred, returns the offset of consumed data.
  3164. * <0 if no data has been transferred.
  3165. */
  3166. int ring_buffer_read_page(struct ring_buffer *buffer,
  3167. void **data_page, size_t len, int cpu, int full)
  3168. {
  3169. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  3170. struct ring_buffer_event *event;
  3171. struct buffer_data_page *bpage;
  3172. struct buffer_page *reader;
  3173. unsigned long missed_events;
  3174. unsigned long flags;
  3175. unsigned int commit;
  3176. unsigned int read;
  3177. u64 save_timestamp;
  3178. int ret = -1;
  3179. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  3180. goto out;
  3181. /*
  3182. * If len is not big enough to hold the page header, then
  3183. * we can not copy anything.
  3184. */
  3185. if (len <= BUF_PAGE_HDR_SIZE)
  3186. goto out;
  3187. len -= BUF_PAGE_HDR_SIZE;
  3188. if (!data_page)
  3189. goto out;
  3190. bpage = *data_page;
  3191. if (!bpage)
  3192. goto out;
  3193. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  3194. reader = rb_get_reader_page(cpu_buffer);
  3195. if (!reader)
  3196. goto out_unlock;
  3197. event = rb_reader_event(cpu_buffer);
  3198. read = reader->read;
  3199. commit = rb_page_commit(reader);
  3200. /* Check if any events were dropped */
  3201. missed_events = cpu_buffer->lost_events;
  3202. /*
  3203. * If this page has been partially read or
  3204. * if len is not big enough to read the rest of the page or
  3205. * a writer is still on the page, then
  3206. * we must copy the data from the page to the buffer.
  3207. * Otherwise, we can simply swap the page with the one passed in.
  3208. */
  3209. if (read || (len < (commit - read)) ||
  3210. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  3211. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  3212. unsigned int rpos = read;
  3213. unsigned int pos = 0;
  3214. unsigned int size;
  3215. if (full)
  3216. goto out_unlock;
  3217. if (len > (commit - read))
  3218. len = (commit - read);
  3219. size = rb_event_length(event);
  3220. if (len < size)
  3221. goto out_unlock;
  3222. /* save the current timestamp, since the user will need it */
  3223. save_timestamp = cpu_buffer->read_stamp;
  3224. /* Need to copy one event at a time */
  3225. do {
  3226. memcpy(bpage->data + pos, rpage->data + rpos, size);
  3227. len -= size;
  3228. rb_advance_reader(cpu_buffer);
  3229. rpos = reader->read;
  3230. pos += size;
  3231. if (rpos >= commit)
  3232. break;
  3233. event = rb_reader_event(cpu_buffer);
  3234. size = rb_event_length(event);
  3235. } while (len > size);
  3236. /* update bpage */
  3237. local_set(&bpage->commit, pos);
  3238. bpage->time_stamp = save_timestamp;
  3239. /* we copied everything to the beginning */
  3240. read = 0;
  3241. } else {
  3242. /* update the entry counter */
  3243. cpu_buffer->read += rb_page_entries(reader);
  3244. /* swap the pages */
  3245. rb_init_page(bpage);
  3246. bpage = reader->page;
  3247. reader->page = *data_page;
  3248. local_set(&reader->write, 0);
  3249. local_set(&reader->entries, 0);
  3250. reader->read = 0;
  3251. *data_page = bpage;
  3252. /*
  3253. * Use the real_end for the data size,
  3254. * This gives us a chance to store the lost events
  3255. * on the page.
  3256. */
  3257. if (reader->real_end)
  3258. local_set(&bpage->commit, reader->real_end);
  3259. }
  3260. ret = read;
  3261. cpu_buffer->lost_events = 0;
  3262. commit = local_read(&bpage->commit);
  3263. /*
  3264. * Set a flag in the commit field if we lost events
  3265. */
  3266. if (missed_events) {
  3267. /* If there is room at the end of the page to save the
  3268. * missed events, then record it there.
  3269. */
  3270. if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
  3271. memcpy(&bpage->data[commit], &missed_events,
  3272. sizeof(missed_events));
  3273. local_add(RB_MISSED_STORED, &bpage->commit);
  3274. commit += sizeof(missed_events);
  3275. }
  3276. local_add(RB_MISSED_EVENTS, &bpage->commit);
  3277. }
  3278. /*
  3279. * This page may be off to user land. Zero it out here.
  3280. */
  3281. if (commit < BUF_PAGE_SIZE)
  3282. memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
  3283. out_unlock:
  3284. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  3285. out:
  3286. return ret;
  3287. }
  3288. EXPORT_SYMBOL_GPL(ring_buffer_read_page);
  3289. #ifdef CONFIG_TRACING
  3290. static ssize_t
  3291. rb_simple_read(struct file *filp, char __user *ubuf,
  3292. size_t cnt, loff_t *ppos)
  3293. {
  3294. unsigned long *p = filp->private_data;
  3295. char buf[64];
  3296. int r;
  3297. if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
  3298. r = sprintf(buf, "permanently disabled\n");
  3299. else
  3300. r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
  3301. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3302. }
  3303. static ssize_t
  3304. rb_simple_write(struct file *filp, const char __user *ubuf,
  3305. size_t cnt, loff_t *ppos)
  3306. {
  3307. unsigned long *p = filp->private_data;
  3308. char buf[64];
  3309. unsigned long val;
  3310. int ret;
  3311. if (cnt >= sizeof(buf))
  3312. return -EINVAL;
  3313. if (copy_from_user(&buf, ubuf, cnt))
  3314. return -EFAULT;
  3315. buf[cnt] = 0;
  3316. ret = strict_strtoul(buf, 10, &val);
  3317. if (ret < 0)
  3318. return ret;
  3319. if (val)
  3320. set_bit(RB_BUFFERS_ON_BIT, p);
  3321. else
  3322. clear_bit(RB_BUFFERS_ON_BIT, p);
  3323. (*ppos)++;
  3324. return cnt;
  3325. }
  3326. static const struct file_operations rb_simple_fops = {
  3327. .open = tracing_open_generic,
  3328. .read = rb_simple_read,
  3329. .write = rb_simple_write,
  3330. };
  3331. static __init int rb_init_debugfs(void)
  3332. {
  3333. struct dentry *d_tracer;
  3334. d_tracer = tracing_init_dentry();
  3335. trace_create_file("tracing_on", 0644, d_tracer,
  3336. &ring_buffer_flags, &rb_simple_fops);
  3337. return 0;
  3338. }
  3339. fs_initcall(rb_init_debugfs);
  3340. #endif
  3341. #ifdef CONFIG_HOTPLUG_CPU
  3342. static int rb_cpu_notify(struct notifier_block *self,
  3343. unsigned long action, void *hcpu)
  3344. {
  3345. struct ring_buffer *buffer =
  3346. container_of(self, struct ring_buffer, cpu_notify);
  3347. long cpu = (long)hcpu;
  3348. switch (action) {
  3349. case CPU_UP_PREPARE:
  3350. case CPU_UP_PREPARE_FROZEN:
  3351. if (cpumask_test_cpu(cpu, buffer->cpumask))
  3352. return NOTIFY_OK;
  3353. buffer->buffers[cpu] =
  3354. rb_allocate_cpu_buffer(buffer, cpu);
  3355. if (!buffer->buffers[cpu]) {
  3356. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  3357. cpu);
  3358. return NOTIFY_OK;
  3359. }
  3360. smp_wmb();
  3361. cpumask_set_cpu(cpu, buffer->cpumask);
  3362. break;
  3363. case CPU_DOWN_PREPARE:
  3364. case CPU_DOWN_PREPARE_FROZEN:
  3365. /*
  3366. * Do nothing.
  3367. * If we were to free the buffer, then the user would
  3368. * lose any trace that was in the buffer.
  3369. */
  3370. break;
  3371. default:
  3372. break;
  3373. }
  3374. return NOTIFY_OK;
  3375. }
  3376. #endif