ring_buffer.c 97 KB

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