ring_buffer.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072
  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/module.h>
  14. #include <linux/percpu.h>
  15. #include <linux/mutex.h>
  16. #include <linux/init.h>
  17. #include <linux/hash.h>
  18. #include <linux/list.h>
  19. #include <linux/cpu.h>
  20. #include <linux/fs.h>
  21. #include "trace.h"
  22. /*
  23. * The ring buffer header is special. We must manually up keep it.
  24. */
  25. int ring_buffer_print_entry_header(struct trace_seq *s)
  26. {
  27. int ret;
  28. ret = trace_seq_printf(s, "\ttype : 2 bits\n");
  29. ret = trace_seq_printf(s, "\tlen : 3 bits\n");
  30. ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
  31. ret = trace_seq_printf(s, "\tarray : 32 bits\n");
  32. ret = trace_seq_printf(s, "\n");
  33. ret = trace_seq_printf(s, "\tpadding : type == %d\n",
  34. RINGBUF_TYPE_PADDING);
  35. ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
  36. RINGBUF_TYPE_TIME_EXTEND);
  37. ret = trace_seq_printf(s, "\tdata : type == %d\n",
  38. RINGBUF_TYPE_DATA);
  39. return ret;
  40. }
  41. /*
  42. * The ring buffer is made up of a list of pages. A separate list of pages is
  43. * allocated for each CPU. A writer may only write to a buffer that is
  44. * associated with the CPU it is currently executing on. A reader may read
  45. * from any per cpu buffer.
  46. *
  47. * The reader is special. For each per cpu buffer, the reader has its own
  48. * reader page. When a reader has read the entire reader page, this reader
  49. * page is swapped with another page in the ring buffer.
  50. *
  51. * Now, as long as the writer is off the reader page, the reader can do what
  52. * ever it wants with that page. The writer will never write to that page
  53. * again (as long as it is out of the ring buffer).
  54. *
  55. * Here's some silly ASCII art.
  56. *
  57. * +------+
  58. * |reader| RING BUFFER
  59. * |page |
  60. * +------+ +---+ +---+ +---+
  61. * | |-->| |-->| |
  62. * +---+ +---+ +---+
  63. * ^ |
  64. * | |
  65. * +---------------+
  66. *
  67. *
  68. * +------+
  69. * |reader| RING BUFFER
  70. * |page |------------------v
  71. * +------+ +---+ +---+ +---+
  72. * | |-->| |-->| |
  73. * +---+ +---+ +---+
  74. * ^ |
  75. * | |
  76. * +---------------+
  77. *
  78. *
  79. * +------+
  80. * |reader| RING BUFFER
  81. * |page |------------------v
  82. * +------+ +---+ +---+ +---+
  83. * ^ | |-->| |-->| |
  84. * | +---+ +---+ +---+
  85. * | |
  86. * | |
  87. * +------------------------------+
  88. *
  89. *
  90. * +------+
  91. * |buffer| RING BUFFER
  92. * |page |------------------v
  93. * +------+ +---+ +---+ +---+
  94. * ^ | | | |-->| |
  95. * | New +---+ +---+ +---+
  96. * | Reader------^ |
  97. * | page |
  98. * +------------------------------+
  99. *
  100. *
  101. * After we make this swap, the reader can hand this page off to the splice
  102. * code and be done with it. It can even allocate a new page if it needs to
  103. * and swap that into the ring buffer.
  104. *
  105. * We will be using cmpxchg soon to make all this lockless.
  106. *
  107. */
  108. /*
  109. * A fast way to enable or disable all ring buffers is to
  110. * call tracing_on or tracing_off. Turning off the ring buffers
  111. * prevents all ring buffers from being recorded to.
  112. * Turning this switch on, makes it OK to write to the
  113. * ring buffer, if the ring buffer is enabled itself.
  114. *
  115. * There's three layers that must be on in order to write
  116. * to the ring buffer.
  117. *
  118. * 1) This global flag must be set.
  119. * 2) The ring buffer must be enabled for recording.
  120. * 3) The per cpu buffer must be enabled for recording.
  121. *
  122. * In case of an anomaly, this global flag has a bit set that
  123. * will permantly disable all ring buffers.
  124. */
  125. /*
  126. * Global flag to disable all recording to ring buffers
  127. * This has two bits: ON, DISABLED
  128. *
  129. * ON DISABLED
  130. * ---- ----------
  131. * 0 0 : ring buffers are off
  132. * 1 0 : ring buffers are on
  133. * X 1 : ring buffers are permanently disabled
  134. */
  135. enum {
  136. RB_BUFFERS_ON_BIT = 0,
  137. RB_BUFFERS_DISABLED_BIT = 1,
  138. };
  139. enum {
  140. RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
  141. RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
  142. };
  143. static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
  144. #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
  145. /**
  146. * tracing_on - enable all tracing buffers
  147. *
  148. * This function enables all tracing buffers that may have been
  149. * disabled with tracing_off.
  150. */
  151. void tracing_on(void)
  152. {
  153. set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  154. }
  155. EXPORT_SYMBOL_GPL(tracing_on);
  156. /**
  157. * tracing_off - turn off all tracing buffers
  158. *
  159. * This function stops all tracing buffers from recording data.
  160. * It does not disable any overhead the tracers themselves may
  161. * be causing. This function simply causes all recording to
  162. * the ring buffers to fail.
  163. */
  164. void tracing_off(void)
  165. {
  166. clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
  167. }
  168. EXPORT_SYMBOL_GPL(tracing_off);
  169. /**
  170. * tracing_off_permanent - permanently disable ring buffers
  171. *
  172. * This function, once called, will disable all ring buffers
  173. * permanently.
  174. */
  175. void tracing_off_permanent(void)
  176. {
  177. set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
  178. }
  179. /**
  180. * tracing_is_on - show state of ring buffers enabled
  181. */
  182. int tracing_is_on(void)
  183. {
  184. return ring_buffer_flags == RB_BUFFERS_ON;
  185. }
  186. EXPORT_SYMBOL_GPL(tracing_is_on);
  187. #include "trace.h"
  188. #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
  189. #define RB_ALIGNMENT 4U
  190. #define RB_MAX_SMALL_DATA 28
  191. enum {
  192. RB_LEN_TIME_EXTEND = 8,
  193. RB_LEN_TIME_STAMP = 16,
  194. };
  195. static inline int rb_null_event(struct ring_buffer_event *event)
  196. {
  197. return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
  198. }
  199. static inline int rb_discarded_event(struct ring_buffer_event *event)
  200. {
  201. return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
  202. }
  203. static void rb_event_set_padding(struct ring_buffer_event *event)
  204. {
  205. event->type = 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->len)
  213. length = event->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) {
  223. case RINGBUF_TYPE_PADDING:
  224. if (rb_null_event(event))
  225. /* undefined */
  226. return -1;
  227. return rb_event_data_length(event);
  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 != RINGBUF_TYPE_DATA)
  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 != RINGBUF_TYPE_DATA);
  260. /* If length is in len field, then array[0] has the data */
  261. if (event->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. struct buffer_page {
  286. local_t write; /* index for next write */
  287. unsigned read; /* index for next read */
  288. struct list_head list; /* list of free pages */
  289. struct buffer_data_page *page; /* Actual data page */
  290. };
  291. static void rb_init_page(struct buffer_data_page *bpage)
  292. {
  293. local_set(&bpage->commit, 0);
  294. }
  295. /**
  296. * ring_buffer_page_len - the size of data on the page.
  297. * @page: The page to read
  298. *
  299. * Returns the amount of data on the page, including buffer page header.
  300. */
  301. size_t ring_buffer_page_len(void *page)
  302. {
  303. return local_read(&((struct buffer_data_page *)page)->commit)
  304. + BUF_PAGE_HDR_SIZE;
  305. }
  306. /*
  307. * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
  308. * this issue out.
  309. */
  310. static void free_buffer_page(struct buffer_page *bpage)
  311. {
  312. free_page((unsigned long)bpage->page);
  313. kfree(bpage);
  314. }
  315. /*
  316. * We need to fit the time_stamp delta into 27 bits.
  317. */
  318. static inline int test_time_stamp(u64 delta)
  319. {
  320. if (delta & TS_DELTA_TEST)
  321. return 1;
  322. return 0;
  323. }
  324. #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
  325. int ring_buffer_print_page_header(struct trace_seq *s)
  326. {
  327. struct buffer_data_page field;
  328. int ret;
  329. ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
  330. "offset:0;\tsize:%u;\n",
  331. (unsigned int)sizeof(field.time_stamp));
  332. ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
  333. "offset:%u;\tsize:%u;\n",
  334. (unsigned int)offsetof(typeof(field), commit),
  335. (unsigned int)sizeof(field.commit));
  336. ret = trace_seq_printf(s, "\tfield: char data;\t"
  337. "offset:%u;\tsize:%u;\n",
  338. (unsigned int)offsetof(typeof(field), data),
  339. (unsigned int)BUF_PAGE_SIZE);
  340. return ret;
  341. }
  342. /*
  343. * head_page == tail_page && head == tail then buffer is empty.
  344. */
  345. struct ring_buffer_per_cpu {
  346. int cpu;
  347. struct ring_buffer *buffer;
  348. spinlock_t reader_lock; /* serialize readers */
  349. raw_spinlock_t lock;
  350. struct lock_class_key lock_key;
  351. struct list_head pages;
  352. struct buffer_page *head_page; /* read from head */
  353. struct buffer_page *tail_page; /* write to tail */
  354. struct buffer_page *commit_page; /* committed pages */
  355. struct buffer_page *reader_page;
  356. unsigned long overrun;
  357. unsigned long entries;
  358. u64 write_stamp;
  359. u64 read_stamp;
  360. atomic_t record_disabled;
  361. };
  362. struct ring_buffer {
  363. unsigned pages;
  364. unsigned flags;
  365. int cpus;
  366. atomic_t record_disabled;
  367. cpumask_var_t cpumask;
  368. struct mutex mutex;
  369. struct ring_buffer_per_cpu **buffers;
  370. #ifdef CONFIG_HOTPLUG_CPU
  371. struct notifier_block cpu_notify;
  372. #endif
  373. u64 (*clock)(void);
  374. };
  375. struct ring_buffer_iter {
  376. struct ring_buffer_per_cpu *cpu_buffer;
  377. unsigned long head;
  378. struct buffer_page *head_page;
  379. u64 read_stamp;
  380. };
  381. /* buffer may be either ring_buffer or ring_buffer_per_cpu */
  382. #define RB_WARN_ON(buffer, cond) \
  383. ({ \
  384. int _____ret = unlikely(cond); \
  385. if (_____ret) { \
  386. atomic_inc(&buffer->record_disabled); \
  387. WARN_ON(1); \
  388. } \
  389. _____ret; \
  390. })
  391. /* Up this if you want to test the TIME_EXTENTS and normalization */
  392. #define DEBUG_SHIFT 0
  393. u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
  394. {
  395. u64 time;
  396. preempt_disable_notrace();
  397. /* shift to debug/test normalization and TIME_EXTENTS */
  398. time = buffer->clock() << DEBUG_SHIFT;
  399. preempt_enable_no_resched_notrace();
  400. return time;
  401. }
  402. EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
  403. void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
  404. int cpu, u64 *ts)
  405. {
  406. /* Just stupid testing the normalize function and deltas */
  407. *ts >>= DEBUG_SHIFT;
  408. }
  409. EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
  410. /**
  411. * check_pages - integrity check of buffer pages
  412. * @cpu_buffer: CPU buffer with pages to test
  413. *
  414. * As a safety measure we check to make sure the data pages have not
  415. * been corrupted.
  416. */
  417. static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
  418. {
  419. struct list_head *head = &cpu_buffer->pages;
  420. struct buffer_page *bpage, *tmp;
  421. if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
  422. return -1;
  423. if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
  424. return -1;
  425. list_for_each_entry_safe(bpage, tmp, head, list) {
  426. if (RB_WARN_ON(cpu_buffer,
  427. bpage->list.next->prev != &bpage->list))
  428. return -1;
  429. if (RB_WARN_ON(cpu_buffer,
  430. bpage->list.prev->next != &bpage->list))
  431. return -1;
  432. }
  433. return 0;
  434. }
  435. static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
  436. unsigned nr_pages)
  437. {
  438. struct list_head *head = &cpu_buffer->pages;
  439. struct buffer_page *bpage, *tmp;
  440. unsigned long addr;
  441. LIST_HEAD(pages);
  442. unsigned i;
  443. for (i = 0; i < nr_pages; i++) {
  444. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  445. GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
  446. if (!bpage)
  447. goto free_pages;
  448. list_add(&bpage->list, &pages);
  449. addr = __get_free_page(GFP_KERNEL);
  450. if (!addr)
  451. goto free_pages;
  452. bpage->page = (void *)addr;
  453. rb_init_page(bpage->page);
  454. }
  455. list_splice(&pages, head);
  456. rb_check_pages(cpu_buffer);
  457. return 0;
  458. free_pages:
  459. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  460. list_del_init(&bpage->list);
  461. free_buffer_page(bpage);
  462. }
  463. return -ENOMEM;
  464. }
  465. static struct ring_buffer_per_cpu *
  466. rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
  467. {
  468. struct ring_buffer_per_cpu *cpu_buffer;
  469. struct buffer_page *bpage;
  470. unsigned long addr;
  471. int ret;
  472. cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
  473. GFP_KERNEL, cpu_to_node(cpu));
  474. if (!cpu_buffer)
  475. return NULL;
  476. cpu_buffer->cpu = cpu;
  477. cpu_buffer->buffer = buffer;
  478. spin_lock_init(&cpu_buffer->reader_lock);
  479. cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  480. INIT_LIST_HEAD(&cpu_buffer->pages);
  481. bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
  482. GFP_KERNEL, cpu_to_node(cpu));
  483. if (!bpage)
  484. goto fail_free_buffer;
  485. cpu_buffer->reader_page = bpage;
  486. addr = __get_free_page(GFP_KERNEL);
  487. if (!addr)
  488. goto fail_free_reader;
  489. bpage->page = (void *)addr;
  490. rb_init_page(bpage->page);
  491. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  492. ret = rb_allocate_pages(cpu_buffer, buffer->pages);
  493. if (ret < 0)
  494. goto fail_free_reader;
  495. cpu_buffer->head_page
  496. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  497. cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
  498. return cpu_buffer;
  499. fail_free_reader:
  500. free_buffer_page(cpu_buffer->reader_page);
  501. fail_free_buffer:
  502. kfree(cpu_buffer);
  503. return NULL;
  504. }
  505. static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
  506. {
  507. struct list_head *head = &cpu_buffer->pages;
  508. struct buffer_page *bpage, *tmp;
  509. free_buffer_page(cpu_buffer->reader_page);
  510. list_for_each_entry_safe(bpage, tmp, head, list) {
  511. list_del_init(&bpage->list);
  512. free_buffer_page(bpage);
  513. }
  514. kfree(cpu_buffer);
  515. }
  516. /*
  517. * Causes compile errors if the struct buffer_page gets bigger
  518. * than the struct page.
  519. */
  520. extern int ring_buffer_page_too_big(void);
  521. #ifdef CONFIG_HOTPLUG_CPU
  522. static int rb_cpu_notify(struct notifier_block *self,
  523. unsigned long action, void *hcpu);
  524. #endif
  525. /**
  526. * ring_buffer_alloc - allocate a new ring_buffer
  527. * @size: the size in bytes per cpu that is needed.
  528. * @flags: attributes to set for the ring buffer.
  529. *
  530. * Currently the only flag that is available is the RB_FL_OVERWRITE
  531. * flag. This flag means that the buffer will overwrite old data
  532. * when the buffer wraps. If this flag is not set, the buffer will
  533. * drop data when the tail hits the head.
  534. */
  535. struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
  536. {
  537. struct ring_buffer *buffer;
  538. int bsize;
  539. int cpu;
  540. /* Paranoid! Optimizes out when all is well */
  541. if (sizeof(struct buffer_page) > sizeof(struct page))
  542. ring_buffer_page_too_big();
  543. /* keep it in its own cache line */
  544. buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
  545. GFP_KERNEL);
  546. if (!buffer)
  547. return NULL;
  548. if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
  549. goto fail_free_buffer;
  550. buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  551. buffer->flags = flags;
  552. buffer->clock = trace_clock_local;
  553. /* need at least two pages */
  554. if (buffer->pages == 1)
  555. buffer->pages++;
  556. /*
  557. * In case of non-hotplug cpu, if the ring-buffer is allocated
  558. * in early initcall, it will not be notified of secondary cpus.
  559. * In that off case, we need to allocate for all possible cpus.
  560. */
  561. #ifdef CONFIG_HOTPLUG_CPU
  562. get_online_cpus();
  563. cpumask_copy(buffer->cpumask, cpu_online_mask);
  564. #else
  565. cpumask_copy(buffer->cpumask, cpu_possible_mask);
  566. #endif
  567. buffer->cpus = nr_cpu_ids;
  568. bsize = sizeof(void *) * nr_cpu_ids;
  569. buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
  570. GFP_KERNEL);
  571. if (!buffer->buffers)
  572. goto fail_free_cpumask;
  573. for_each_buffer_cpu(buffer, cpu) {
  574. buffer->buffers[cpu] =
  575. rb_allocate_cpu_buffer(buffer, cpu);
  576. if (!buffer->buffers[cpu])
  577. goto fail_free_buffers;
  578. }
  579. #ifdef CONFIG_HOTPLUG_CPU
  580. buffer->cpu_notify.notifier_call = rb_cpu_notify;
  581. buffer->cpu_notify.priority = 0;
  582. register_cpu_notifier(&buffer->cpu_notify);
  583. #endif
  584. put_online_cpus();
  585. mutex_init(&buffer->mutex);
  586. return buffer;
  587. fail_free_buffers:
  588. for_each_buffer_cpu(buffer, cpu) {
  589. if (buffer->buffers[cpu])
  590. rb_free_cpu_buffer(buffer->buffers[cpu]);
  591. }
  592. kfree(buffer->buffers);
  593. fail_free_cpumask:
  594. free_cpumask_var(buffer->cpumask);
  595. put_online_cpus();
  596. fail_free_buffer:
  597. kfree(buffer);
  598. return NULL;
  599. }
  600. EXPORT_SYMBOL_GPL(ring_buffer_alloc);
  601. /**
  602. * ring_buffer_free - free a ring buffer.
  603. * @buffer: the buffer to free.
  604. */
  605. void
  606. ring_buffer_free(struct ring_buffer *buffer)
  607. {
  608. int cpu;
  609. get_online_cpus();
  610. #ifdef CONFIG_HOTPLUG_CPU
  611. unregister_cpu_notifier(&buffer->cpu_notify);
  612. #endif
  613. for_each_buffer_cpu(buffer, cpu)
  614. rb_free_cpu_buffer(buffer->buffers[cpu]);
  615. put_online_cpus();
  616. free_cpumask_var(buffer->cpumask);
  617. kfree(buffer);
  618. }
  619. EXPORT_SYMBOL_GPL(ring_buffer_free);
  620. void ring_buffer_set_clock(struct ring_buffer *buffer,
  621. u64 (*clock)(void))
  622. {
  623. buffer->clock = clock;
  624. }
  625. static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
  626. static void
  627. rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
  628. {
  629. struct buffer_page *bpage;
  630. struct list_head *p;
  631. unsigned i;
  632. atomic_inc(&cpu_buffer->record_disabled);
  633. synchronize_sched();
  634. for (i = 0; i < nr_pages; i++) {
  635. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  636. return;
  637. p = cpu_buffer->pages.next;
  638. bpage = list_entry(p, struct buffer_page, list);
  639. list_del_init(&bpage->list);
  640. free_buffer_page(bpage);
  641. }
  642. if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
  643. return;
  644. rb_reset_cpu(cpu_buffer);
  645. rb_check_pages(cpu_buffer);
  646. atomic_dec(&cpu_buffer->record_disabled);
  647. }
  648. static void
  649. rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
  650. struct list_head *pages, unsigned nr_pages)
  651. {
  652. struct buffer_page *bpage;
  653. struct list_head *p;
  654. unsigned i;
  655. atomic_inc(&cpu_buffer->record_disabled);
  656. synchronize_sched();
  657. for (i = 0; i < nr_pages; i++) {
  658. if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
  659. return;
  660. p = pages->next;
  661. bpage = list_entry(p, struct buffer_page, list);
  662. list_del_init(&bpage->list);
  663. list_add_tail(&bpage->list, &cpu_buffer->pages);
  664. }
  665. rb_reset_cpu(cpu_buffer);
  666. rb_check_pages(cpu_buffer);
  667. atomic_dec(&cpu_buffer->record_disabled);
  668. }
  669. /**
  670. * ring_buffer_resize - resize the ring buffer
  671. * @buffer: the buffer to resize.
  672. * @size: the new size.
  673. *
  674. * The tracer is responsible for making sure that the buffer is
  675. * not being used while changing the size.
  676. * Note: We may be able to change the above requirement by using
  677. * RCU synchronizations.
  678. *
  679. * Minimum size is 2 * BUF_PAGE_SIZE.
  680. *
  681. * Returns -1 on failure.
  682. */
  683. int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
  684. {
  685. struct ring_buffer_per_cpu *cpu_buffer;
  686. unsigned nr_pages, rm_pages, new_pages;
  687. struct buffer_page *bpage, *tmp;
  688. unsigned long buffer_size;
  689. unsigned long addr;
  690. LIST_HEAD(pages);
  691. int i, cpu;
  692. /*
  693. * Always succeed at resizing a non-existent buffer:
  694. */
  695. if (!buffer)
  696. return size;
  697. size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  698. size *= BUF_PAGE_SIZE;
  699. buffer_size = buffer->pages * BUF_PAGE_SIZE;
  700. /* we need a minimum of two pages */
  701. if (size < BUF_PAGE_SIZE * 2)
  702. size = BUF_PAGE_SIZE * 2;
  703. if (size == buffer_size)
  704. return size;
  705. mutex_lock(&buffer->mutex);
  706. get_online_cpus();
  707. nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
  708. if (size < buffer_size) {
  709. /* easy case, just free pages */
  710. if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
  711. goto out_fail;
  712. rm_pages = buffer->pages - nr_pages;
  713. for_each_buffer_cpu(buffer, cpu) {
  714. cpu_buffer = buffer->buffers[cpu];
  715. rb_remove_pages(cpu_buffer, rm_pages);
  716. }
  717. goto out;
  718. }
  719. /*
  720. * This is a bit more difficult. We only want to add pages
  721. * when we can allocate enough for all CPUs. We do this
  722. * by allocating all the pages and storing them on a local
  723. * link list. If we succeed in our allocation, then we
  724. * add these pages to the cpu_buffers. Otherwise we just free
  725. * them all and return -ENOMEM;
  726. */
  727. if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
  728. goto out_fail;
  729. new_pages = nr_pages - buffer->pages;
  730. for_each_buffer_cpu(buffer, cpu) {
  731. for (i = 0; i < new_pages; i++) {
  732. bpage = kzalloc_node(ALIGN(sizeof(*bpage),
  733. cache_line_size()),
  734. GFP_KERNEL, cpu_to_node(cpu));
  735. if (!bpage)
  736. goto free_pages;
  737. list_add(&bpage->list, &pages);
  738. addr = __get_free_page(GFP_KERNEL);
  739. if (!addr)
  740. goto free_pages;
  741. bpage->page = (void *)addr;
  742. rb_init_page(bpage->page);
  743. }
  744. }
  745. for_each_buffer_cpu(buffer, cpu) {
  746. cpu_buffer = buffer->buffers[cpu];
  747. rb_insert_pages(cpu_buffer, &pages, new_pages);
  748. }
  749. if (RB_WARN_ON(buffer, !list_empty(&pages)))
  750. goto out_fail;
  751. out:
  752. buffer->pages = nr_pages;
  753. put_online_cpus();
  754. mutex_unlock(&buffer->mutex);
  755. return size;
  756. free_pages:
  757. list_for_each_entry_safe(bpage, tmp, &pages, list) {
  758. list_del_init(&bpage->list);
  759. free_buffer_page(bpage);
  760. }
  761. put_online_cpus();
  762. mutex_unlock(&buffer->mutex);
  763. return -ENOMEM;
  764. /*
  765. * Something went totally wrong, and we are too paranoid
  766. * to even clean up the mess.
  767. */
  768. out_fail:
  769. put_online_cpus();
  770. mutex_unlock(&buffer->mutex);
  771. return -1;
  772. }
  773. EXPORT_SYMBOL_GPL(ring_buffer_resize);
  774. static inline void *
  775. __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
  776. {
  777. return bpage->data + index;
  778. }
  779. static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
  780. {
  781. return bpage->page->data + index;
  782. }
  783. static inline struct ring_buffer_event *
  784. rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
  785. {
  786. return __rb_page_index(cpu_buffer->reader_page,
  787. cpu_buffer->reader_page->read);
  788. }
  789. static inline struct ring_buffer_event *
  790. rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
  791. {
  792. return __rb_page_index(cpu_buffer->head_page,
  793. cpu_buffer->head_page->read);
  794. }
  795. static inline struct ring_buffer_event *
  796. rb_iter_head_event(struct ring_buffer_iter *iter)
  797. {
  798. return __rb_page_index(iter->head_page, iter->head);
  799. }
  800. static inline unsigned rb_page_write(struct buffer_page *bpage)
  801. {
  802. return local_read(&bpage->write);
  803. }
  804. static inline unsigned rb_page_commit(struct buffer_page *bpage)
  805. {
  806. return local_read(&bpage->page->commit);
  807. }
  808. /* Size is determined by what has been commited */
  809. static inline unsigned rb_page_size(struct buffer_page *bpage)
  810. {
  811. return rb_page_commit(bpage);
  812. }
  813. static inline unsigned
  814. rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
  815. {
  816. return rb_page_commit(cpu_buffer->commit_page);
  817. }
  818. static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
  819. {
  820. return rb_page_commit(cpu_buffer->head_page);
  821. }
  822. /*
  823. * When the tail hits the head and the buffer is in overwrite mode,
  824. * the head jumps to the next page and all content on the previous
  825. * page is discarded. But before doing so, we update the overrun
  826. * variable of the buffer.
  827. */
  828. static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
  829. {
  830. struct ring_buffer_event *event;
  831. unsigned long head;
  832. for (head = 0; head < rb_head_size(cpu_buffer);
  833. head += rb_event_length(event)) {
  834. event = __rb_page_index(cpu_buffer->head_page, head);
  835. if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
  836. return;
  837. /* Only count data entries */
  838. if (event->type != RINGBUF_TYPE_DATA)
  839. continue;
  840. cpu_buffer->overrun++;
  841. cpu_buffer->entries--;
  842. }
  843. }
  844. static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
  845. struct buffer_page **bpage)
  846. {
  847. struct list_head *p = (*bpage)->list.next;
  848. if (p == &cpu_buffer->pages)
  849. p = p->next;
  850. *bpage = list_entry(p, struct buffer_page, list);
  851. }
  852. static inline unsigned
  853. rb_event_index(struct ring_buffer_event *event)
  854. {
  855. unsigned long addr = (unsigned long)event;
  856. return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
  857. }
  858. static int
  859. rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
  860. struct ring_buffer_event *event)
  861. {
  862. unsigned long addr = (unsigned long)event;
  863. unsigned long index;
  864. index = rb_event_index(event);
  865. addr &= PAGE_MASK;
  866. return cpu_buffer->commit_page->page == (void *)addr &&
  867. rb_commit_index(cpu_buffer) == index;
  868. }
  869. static void
  870. rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
  871. struct ring_buffer_event *event)
  872. {
  873. unsigned long addr = (unsigned long)event;
  874. unsigned long index;
  875. index = rb_event_index(event);
  876. addr &= PAGE_MASK;
  877. while (cpu_buffer->commit_page->page != (void *)addr) {
  878. if (RB_WARN_ON(cpu_buffer,
  879. cpu_buffer->commit_page == cpu_buffer->tail_page))
  880. return;
  881. cpu_buffer->commit_page->page->commit =
  882. cpu_buffer->commit_page->write;
  883. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  884. cpu_buffer->write_stamp =
  885. cpu_buffer->commit_page->page->time_stamp;
  886. }
  887. /* Now set the commit to the event's index */
  888. local_set(&cpu_buffer->commit_page->page->commit, index);
  889. }
  890. static void
  891. rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
  892. {
  893. /*
  894. * We only race with interrupts and NMIs on this CPU.
  895. * If we own the commit event, then we can commit
  896. * all others that interrupted us, since the interruptions
  897. * are in stack format (they finish before they come
  898. * back to us). This allows us to do a simple loop to
  899. * assign the commit to the tail.
  900. */
  901. again:
  902. while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
  903. cpu_buffer->commit_page->page->commit =
  904. cpu_buffer->commit_page->write;
  905. rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
  906. cpu_buffer->write_stamp =
  907. cpu_buffer->commit_page->page->time_stamp;
  908. /* add barrier to keep gcc from optimizing too much */
  909. barrier();
  910. }
  911. while (rb_commit_index(cpu_buffer) !=
  912. rb_page_write(cpu_buffer->commit_page)) {
  913. cpu_buffer->commit_page->page->commit =
  914. cpu_buffer->commit_page->write;
  915. barrier();
  916. }
  917. /* again, keep gcc from optimizing */
  918. barrier();
  919. /*
  920. * If an interrupt came in just after the first while loop
  921. * and pushed the tail page forward, we will be left with
  922. * a dangling commit that will never go forward.
  923. */
  924. if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
  925. goto again;
  926. }
  927. static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  928. {
  929. cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
  930. cpu_buffer->reader_page->read = 0;
  931. }
  932. static void rb_inc_iter(struct ring_buffer_iter *iter)
  933. {
  934. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  935. /*
  936. * The iterator could be on the reader page (it starts there).
  937. * But the head could have moved, since the reader was
  938. * found. Check for this case and assign the iterator
  939. * to the head page instead of next.
  940. */
  941. if (iter->head_page == cpu_buffer->reader_page)
  942. iter->head_page = cpu_buffer->head_page;
  943. else
  944. rb_inc_page(cpu_buffer, &iter->head_page);
  945. iter->read_stamp = iter->head_page->page->time_stamp;
  946. iter->head = 0;
  947. }
  948. /**
  949. * ring_buffer_update_event - update event type and data
  950. * @event: the even to update
  951. * @type: the type of event
  952. * @length: the size of the event field in the ring buffer
  953. *
  954. * Update the type and data fields of the event. The length
  955. * is the actual size that is written to the ring buffer,
  956. * and with this, we can determine what to place into the
  957. * data field.
  958. */
  959. static void
  960. rb_update_event(struct ring_buffer_event *event,
  961. unsigned type, unsigned length)
  962. {
  963. event->type = type;
  964. switch (type) {
  965. case RINGBUF_TYPE_PADDING:
  966. break;
  967. case RINGBUF_TYPE_TIME_EXTEND:
  968. event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
  969. break;
  970. case RINGBUF_TYPE_TIME_STAMP:
  971. event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
  972. break;
  973. case RINGBUF_TYPE_DATA:
  974. length -= RB_EVNT_HDR_SIZE;
  975. if (length > RB_MAX_SMALL_DATA) {
  976. event->len = 0;
  977. event->array[0] = length;
  978. } else
  979. event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
  980. break;
  981. default:
  982. BUG();
  983. }
  984. }
  985. static unsigned rb_calculate_event_length(unsigned length)
  986. {
  987. struct ring_buffer_event event; /* Used only for sizeof array */
  988. /* zero length can cause confusions */
  989. if (!length)
  990. length = 1;
  991. if (length > RB_MAX_SMALL_DATA)
  992. length += sizeof(event.array[0]);
  993. length += RB_EVNT_HDR_SIZE;
  994. length = ALIGN(length, RB_ALIGNMENT);
  995. return length;
  996. }
  997. static struct ring_buffer_event *
  998. __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
  999. unsigned type, unsigned long length, u64 *ts)
  1000. {
  1001. struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
  1002. unsigned long tail, write;
  1003. struct ring_buffer *buffer = cpu_buffer->buffer;
  1004. struct ring_buffer_event *event;
  1005. unsigned long flags;
  1006. bool lock_taken = false;
  1007. commit_page = cpu_buffer->commit_page;
  1008. /* we just need to protect against interrupts */
  1009. barrier();
  1010. tail_page = cpu_buffer->tail_page;
  1011. write = local_add_return(length, &tail_page->write);
  1012. tail = write - length;
  1013. /* See if we shot pass the end of this buffer page */
  1014. if (write > BUF_PAGE_SIZE) {
  1015. struct buffer_page *next_page = tail_page;
  1016. local_irq_save(flags);
  1017. /*
  1018. * Since the write to the buffer is still not
  1019. * fully lockless, we must be careful with NMIs.
  1020. * The locks in the writers are taken when a write
  1021. * crosses to a new page. The locks protect against
  1022. * races with the readers (this will soon be fixed
  1023. * with a lockless solution).
  1024. *
  1025. * Because we can not protect against NMIs, and we
  1026. * want to keep traces reentrant, we need to manage
  1027. * what happens when we are in an NMI.
  1028. *
  1029. * NMIs can happen after we take the lock.
  1030. * If we are in an NMI, only take the lock
  1031. * if it is not already taken. Otherwise
  1032. * simply fail.
  1033. */
  1034. if (unlikely(in_nmi())) {
  1035. if (!__raw_spin_trylock(&cpu_buffer->lock))
  1036. goto out_reset;
  1037. } else
  1038. __raw_spin_lock(&cpu_buffer->lock);
  1039. lock_taken = true;
  1040. rb_inc_page(cpu_buffer, &next_page);
  1041. head_page = cpu_buffer->head_page;
  1042. reader_page = cpu_buffer->reader_page;
  1043. /* we grabbed the lock before incrementing */
  1044. if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
  1045. goto out_reset;
  1046. /*
  1047. * If for some reason, we had an interrupt storm that made
  1048. * it all the way around the buffer, bail, and warn
  1049. * about it.
  1050. */
  1051. if (unlikely(next_page == commit_page)) {
  1052. WARN_ON_ONCE(1);
  1053. goto out_reset;
  1054. }
  1055. if (next_page == head_page) {
  1056. if (!(buffer->flags & RB_FL_OVERWRITE))
  1057. goto out_reset;
  1058. /* tail_page has not moved yet? */
  1059. if (tail_page == cpu_buffer->tail_page) {
  1060. /* count overflows */
  1061. rb_update_overflow(cpu_buffer);
  1062. rb_inc_page(cpu_buffer, &head_page);
  1063. cpu_buffer->head_page = head_page;
  1064. cpu_buffer->head_page->read = 0;
  1065. }
  1066. }
  1067. /*
  1068. * If the tail page is still the same as what we think
  1069. * it is, then it is up to us to update the tail
  1070. * pointer.
  1071. */
  1072. if (tail_page == cpu_buffer->tail_page) {
  1073. local_set(&next_page->write, 0);
  1074. local_set(&next_page->page->commit, 0);
  1075. cpu_buffer->tail_page = next_page;
  1076. /* reread the time stamp */
  1077. *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
  1078. cpu_buffer->tail_page->page->time_stamp = *ts;
  1079. }
  1080. /*
  1081. * The actual tail page has moved forward.
  1082. */
  1083. if (tail < BUF_PAGE_SIZE) {
  1084. /* Mark the rest of the page with padding */
  1085. event = __rb_page_index(tail_page, tail);
  1086. rb_event_set_padding(event);
  1087. }
  1088. if (tail <= BUF_PAGE_SIZE)
  1089. /* Set the write back to the previous setting */
  1090. local_set(&tail_page->write, tail);
  1091. /*
  1092. * If this was a commit entry that failed,
  1093. * increment that too
  1094. */
  1095. if (tail_page == cpu_buffer->commit_page &&
  1096. tail == rb_commit_index(cpu_buffer)) {
  1097. rb_set_commit_to_write(cpu_buffer);
  1098. }
  1099. __raw_spin_unlock(&cpu_buffer->lock);
  1100. local_irq_restore(flags);
  1101. /* fail and let the caller try again */
  1102. return ERR_PTR(-EAGAIN);
  1103. }
  1104. /* We reserved something on the buffer */
  1105. if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
  1106. return NULL;
  1107. event = __rb_page_index(tail_page, tail);
  1108. rb_update_event(event, type, length);
  1109. /*
  1110. * If this is a commit and the tail is zero, then update
  1111. * this page's time stamp.
  1112. */
  1113. if (!tail && rb_is_commit(cpu_buffer, event))
  1114. cpu_buffer->commit_page->page->time_stamp = *ts;
  1115. return event;
  1116. out_reset:
  1117. /* reset write */
  1118. if (tail <= BUF_PAGE_SIZE)
  1119. local_set(&tail_page->write, tail);
  1120. if (likely(lock_taken))
  1121. __raw_spin_unlock(&cpu_buffer->lock);
  1122. local_irq_restore(flags);
  1123. return NULL;
  1124. }
  1125. static int
  1126. rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1127. u64 *ts, u64 *delta)
  1128. {
  1129. struct ring_buffer_event *event;
  1130. static int once;
  1131. int ret;
  1132. if (unlikely(*delta > (1ULL << 59) && !once++)) {
  1133. printk(KERN_WARNING "Delta way too big! %llu"
  1134. " ts=%llu write stamp = %llu\n",
  1135. (unsigned long long)*delta,
  1136. (unsigned long long)*ts,
  1137. (unsigned long long)cpu_buffer->write_stamp);
  1138. WARN_ON(1);
  1139. }
  1140. /*
  1141. * The delta is too big, we to add a
  1142. * new timestamp.
  1143. */
  1144. event = __rb_reserve_next(cpu_buffer,
  1145. RINGBUF_TYPE_TIME_EXTEND,
  1146. RB_LEN_TIME_EXTEND,
  1147. ts);
  1148. if (!event)
  1149. return -EBUSY;
  1150. if (PTR_ERR(event) == -EAGAIN)
  1151. return -EAGAIN;
  1152. /* Only a commited time event can update the write stamp */
  1153. if (rb_is_commit(cpu_buffer, event)) {
  1154. /*
  1155. * If this is the first on the page, then we need to
  1156. * update the page itself, and just put in a zero.
  1157. */
  1158. if (rb_event_index(event)) {
  1159. event->time_delta = *delta & TS_MASK;
  1160. event->array[0] = *delta >> TS_SHIFT;
  1161. } else {
  1162. cpu_buffer->commit_page->page->time_stamp = *ts;
  1163. event->time_delta = 0;
  1164. event->array[0] = 0;
  1165. }
  1166. cpu_buffer->write_stamp = *ts;
  1167. /* let the caller know this was the commit */
  1168. ret = 1;
  1169. } else {
  1170. /* Darn, this is just wasted space */
  1171. event->time_delta = 0;
  1172. event->array[0] = 0;
  1173. ret = 0;
  1174. }
  1175. *delta = 0;
  1176. return ret;
  1177. }
  1178. static struct ring_buffer_event *
  1179. rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
  1180. unsigned type, unsigned long length)
  1181. {
  1182. struct ring_buffer_event *event;
  1183. u64 ts, delta;
  1184. int commit = 0;
  1185. int nr_loops = 0;
  1186. again:
  1187. /*
  1188. * We allow for interrupts to reenter here and do a trace.
  1189. * If one does, it will cause this original code to loop
  1190. * back here. Even with heavy interrupts happening, this
  1191. * should only happen a few times in a row. If this happens
  1192. * 1000 times in a row, there must be either an interrupt
  1193. * storm or we have something buggy.
  1194. * Bail!
  1195. */
  1196. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
  1197. return NULL;
  1198. ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
  1199. /*
  1200. * Only the first commit can update the timestamp.
  1201. * Yes there is a race here. If an interrupt comes in
  1202. * just after the conditional and it traces too, then it
  1203. * will also check the deltas. More than one timestamp may
  1204. * also be made. But only the entry that did the actual
  1205. * commit will be something other than zero.
  1206. */
  1207. if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
  1208. rb_page_write(cpu_buffer->tail_page) ==
  1209. rb_commit_index(cpu_buffer)) {
  1210. delta = ts - cpu_buffer->write_stamp;
  1211. /* make sure this delta is calculated here */
  1212. barrier();
  1213. /* Did the write stamp get updated already? */
  1214. if (unlikely(ts < cpu_buffer->write_stamp))
  1215. delta = 0;
  1216. if (test_time_stamp(delta)) {
  1217. commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
  1218. if (commit == -EBUSY)
  1219. return NULL;
  1220. if (commit == -EAGAIN)
  1221. goto again;
  1222. RB_WARN_ON(cpu_buffer, commit < 0);
  1223. }
  1224. } else
  1225. /* Non commits have zero deltas */
  1226. delta = 0;
  1227. event = __rb_reserve_next(cpu_buffer, type, length, &ts);
  1228. if (PTR_ERR(event) == -EAGAIN)
  1229. goto again;
  1230. if (!event) {
  1231. if (unlikely(commit))
  1232. /*
  1233. * Ouch! We needed a timestamp and it was commited. But
  1234. * we didn't get our event reserved.
  1235. */
  1236. rb_set_commit_to_write(cpu_buffer);
  1237. return NULL;
  1238. }
  1239. /*
  1240. * If the timestamp was commited, make the commit our entry
  1241. * now so that we will update it when needed.
  1242. */
  1243. if (commit)
  1244. rb_set_commit_event(cpu_buffer, event);
  1245. else if (!rb_is_commit(cpu_buffer, event))
  1246. delta = 0;
  1247. event->time_delta = delta;
  1248. return event;
  1249. }
  1250. static int trace_irq_level(void)
  1251. {
  1252. return hardirq_count() + softirq_count() + in_nmi();
  1253. }
  1254. static int trace_recursive_lock(void)
  1255. {
  1256. int level;
  1257. level = trace_irq_level();
  1258. if (unlikely(current->trace_recursion & (1 << level))) {
  1259. /* Disable all tracing before we do anything else */
  1260. tracing_off_permanent();
  1261. printk_once(KERN_WARNING "Tracing recursion: "
  1262. "HC[%lu]:SC[%lu]:NMI[%lu]\n",
  1263. hardirq_count() >> HARDIRQ_SHIFT,
  1264. softirq_count() >> SOFTIRQ_SHIFT,
  1265. in_nmi());
  1266. WARN_ON_ONCE(1);
  1267. return -1;
  1268. }
  1269. current->trace_recursion |= 1 << level;
  1270. return 0;
  1271. }
  1272. static void trace_recursive_unlock(void)
  1273. {
  1274. int level;
  1275. level = trace_irq_level();
  1276. WARN_ON_ONCE(!current->trace_recursion & (1 << level));
  1277. current->trace_recursion &= ~(1 << level);
  1278. }
  1279. static DEFINE_PER_CPU(int, rb_need_resched);
  1280. /**
  1281. * ring_buffer_lock_reserve - reserve a part of the buffer
  1282. * @buffer: the ring buffer to reserve from
  1283. * @length: the length of the data to reserve (excluding event header)
  1284. *
  1285. * Returns a reseverd event on the ring buffer to copy directly to.
  1286. * The user of this interface will need to get the body to write into
  1287. * and can use the ring_buffer_event_data() interface.
  1288. *
  1289. * The length is the length of the data needed, not the event length
  1290. * which also includes the event header.
  1291. *
  1292. * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
  1293. * If NULL is returned, then nothing has been allocated or locked.
  1294. */
  1295. struct ring_buffer_event *
  1296. ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
  1297. {
  1298. struct ring_buffer_per_cpu *cpu_buffer;
  1299. struct ring_buffer_event *event;
  1300. int cpu, resched;
  1301. if (ring_buffer_flags != RB_BUFFERS_ON)
  1302. return NULL;
  1303. if (atomic_read(&buffer->record_disabled))
  1304. return NULL;
  1305. /* If we are tracing schedule, we don't want to recurse */
  1306. resched = ftrace_preempt_disable();
  1307. if (trace_recursive_lock())
  1308. goto out_nocheck;
  1309. cpu = raw_smp_processor_id();
  1310. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1311. goto out;
  1312. cpu_buffer = buffer->buffers[cpu];
  1313. if (atomic_read(&cpu_buffer->record_disabled))
  1314. goto out;
  1315. length = rb_calculate_event_length(length);
  1316. if (length > BUF_PAGE_SIZE)
  1317. goto out;
  1318. event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
  1319. if (!event)
  1320. goto out;
  1321. /*
  1322. * Need to store resched state on this cpu.
  1323. * Only the first needs to.
  1324. */
  1325. if (preempt_count() == 1)
  1326. per_cpu(rb_need_resched, cpu) = resched;
  1327. return event;
  1328. out:
  1329. trace_recursive_unlock();
  1330. out_nocheck:
  1331. ftrace_preempt_enable(resched);
  1332. return NULL;
  1333. }
  1334. EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
  1335. static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
  1336. struct ring_buffer_event *event)
  1337. {
  1338. cpu_buffer->entries++;
  1339. /* Only process further if we own the commit */
  1340. if (!rb_is_commit(cpu_buffer, event))
  1341. return;
  1342. cpu_buffer->write_stamp += event->time_delta;
  1343. rb_set_commit_to_write(cpu_buffer);
  1344. }
  1345. /**
  1346. * ring_buffer_unlock_commit - commit a reserved
  1347. * @buffer: The buffer to commit to
  1348. * @event: The event pointer to commit.
  1349. *
  1350. * This commits the data to the ring buffer, and releases any locks held.
  1351. *
  1352. * Must be paired with ring_buffer_lock_reserve.
  1353. */
  1354. int ring_buffer_unlock_commit(struct ring_buffer *buffer,
  1355. struct ring_buffer_event *event)
  1356. {
  1357. struct ring_buffer_per_cpu *cpu_buffer;
  1358. int cpu = raw_smp_processor_id();
  1359. cpu_buffer = buffer->buffers[cpu];
  1360. rb_commit(cpu_buffer, event);
  1361. trace_recursive_unlock();
  1362. /*
  1363. * Only the last preempt count needs to restore preemption.
  1364. */
  1365. if (preempt_count() == 1)
  1366. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1367. else
  1368. preempt_enable_no_resched_notrace();
  1369. return 0;
  1370. }
  1371. EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
  1372. /**
  1373. * ring_buffer_event_discard - discard any event in the ring buffer
  1374. * @event: the event to discard
  1375. *
  1376. * Sometimes a event that is in the ring buffer needs to be ignored.
  1377. * This function lets the user discard an event in the ring buffer
  1378. * and then that event will not be read later.
  1379. *
  1380. * Note, it is up to the user to be careful with this, and protect
  1381. * against races. If the user discards an event that has been consumed
  1382. * it is possible that it could corrupt the ring buffer.
  1383. */
  1384. void ring_buffer_event_discard(struct ring_buffer_event *event)
  1385. {
  1386. event->type = RINGBUF_TYPE_PADDING;
  1387. /* time delta must be non zero */
  1388. if (!event->time_delta)
  1389. event->time_delta = 1;
  1390. }
  1391. EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
  1392. /**
  1393. * ring_buffer_commit_discard - discard an event that has not been committed
  1394. * @buffer: the ring buffer
  1395. * @event: non committed event to discard
  1396. *
  1397. * This is similar to ring_buffer_event_discard but must only be
  1398. * performed on an event that has not been committed yet. The difference
  1399. * is that this will also try to free the event from the ring buffer
  1400. * if another event has not been added behind it.
  1401. *
  1402. * If another event has been added behind it, it will set the event
  1403. * up as discarded, and perform the commit.
  1404. *
  1405. * If this function is called, do not call ring_buffer_unlock_commit on
  1406. * the event.
  1407. */
  1408. void ring_buffer_discard_commit(struct ring_buffer *buffer,
  1409. struct ring_buffer_event *event)
  1410. {
  1411. struct ring_buffer_per_cpu *cpu_buffer;
  1412. unsigned long new_index, old_index;
  1413. struct buffer_page *bpage;
  1414. unsigned long index;
  1415. unsigned long addr;
  1416. int cpu;
  1417. /* The event is discarded regardless */
  1418. ring_buffer_event_discard(event);
  1419. /*
  1420. * This must only be called if the event has not been
  1421. * committed yet. Thus we can assume that preemption
  1422. * is still disabled.
  1423. */
  1424. RB_WARN_ON(buffer, !preempt_count());
  1425. cpu = smp_processor_id();
  1426. cpu_buffer = buffer->buffers[cpu];
  1427. new_index = rb_event_index(event);
  1428. old_index = new_index + rb_event_length(event);
  1429. addr = (unsigned long)event;
  1430. addr &= PAGE_MASK;
  1431. bpage = cpu_buffer->tail_page;
  1432. if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
  1433. /*
  1434. * This is on the tail page. It is possible that
  1435. * a write could come in and move the tail page
  1436. * and write to the next page. That is fine
  1437. * because we just shorten what is on this page.
  1438. */
  1439. index = local_cmpxchg(&bpage->write, old_index, new_index);
  1440. if (index == old_index)
  1441. goto out;
  1442. }
  1443. /*
  1444. * The commit is still visible by the reader, so we
  1445. * must increment entries.
  1446. */
  1447. cpu_buffer->entries++;
  1448. out:
  1449. /*
  1450. * If a write came in and pushed the tail page
  1451. * we still need to update the commit pointer
  1452. * if we were the commit.
  1453. */
  1454. if (rb_is_commit(cpu_buffer, event))
  1455. rb_set_commit_to_write(cpu_buffer);
  1456. /*
  1457. * Only the last preempt count needs to restore preemption.
  1458. */
  1459. if (preempt_count() == 1)
  1460. ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
  1461. else
  1462. preempt_enable_no_resched_notrace();
  1463. }
  1464. EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
  1465. /**
  1466. * ring_buffer_write - write data to the buffer without reserving
  1467. * @buffer: The ring buffer to write to.
  1468. * @length: The length of the data being written (excluding the event header)
  1469. * @data: The data to write to the buffer.
  1470. *
  1471. * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
  1472. * one function. If you already have the data to write to the buffer, it
  1473. * may be easier to simply call this function.
  1474. *
  1475. * Note, like ring_buffer_lock_reserve, the length is the length of the data
  1476. * and not the length of the event which would hold the header.
  1477. */
  1478. int ring_buffer_write(struct ring_buffer *buffer,
  1479. unsigned long length,
  1480. void *data)
  1481. {
  1482. struct ring_buffer_per_cpu *cpu_buffer;
  1483. struct ring_buffer_event *event;
  1484. unsigned long event_length;
  1485. void *body;
  1486. int ret = -EBUSY;
  1487. int cpu, resched;
  1488. if (ring_buffer_flags != RB_BUFFERS_ON)
  1489. return -EBUSY;
  1490. if (atomic_read(&buffer->record_disabled))
  1491. return -EBUSY;
  1492. resched = ftrace_preempt_disable();
  1493. cpu = raw_smp_processor_id();
  1494. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1495. goto out;
  1496. cpu_buffer = buffer->buffers[cpu];
  1497. if (atomic_read(&cpu_buffer->record_disabled))
  1498. goto out;
  1499. event_length = rb_calculate_event_length(length);
  1500. event = rb_reserve_next_event(cpu_buffer,
  1501. RINGBUF_TYPE_DATA, event_length);
  1502. if (!event)
  1503. goto out;
  1504. body = rb_event_data(event);
  1505. memcpy(body, data, length);
  1506. rb_commit(cpu_buffer, event);
  1507. ret = 0;
  1508. out:
  1509. ftrace_preempt_enable(resched);
  1510. return ret;
  1511. }
  1512. EXPORT_SYMBOL_GPL(ring_buffer_write);
  1513. static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
  1514. {
  1515. struct buffer_page *reader = cpu_buffer->reader_page;
  1516. struct buffer_page *head = cpu_buffer->head_page;
  1517. struct buffer_page *commit = cpu_buffer->commit_page;
  1518. return reader->read == rb_page_commit(reader) &&
  1519. (commit == reader ||
  1520. (commit == head &&
  1521. head->read == rb_page_commit(commit)));
  1522. }
  1523. /**
  1524. * ring_buffer_record_disable - stop all writes into the buffer
  1525. * @buffer: The ring buffer to stop writes to.
  1526. *
  1527. * This prevents all writes to the buffer. Any attempt to write
  1528. * to the buffer after this will fail and return NULL.
  1529. *
  1530. * The caller should call synchronize_sched() after this.
  1531. */
  1532. void ring_buffer_record_disable(struct ring_buffer *buffer)
  1533. {
  1534. atomic_inc(&buffer->record_disabled);
  1535. }
  1536. EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
  1537. /**
  1538. * ring_buffer_record_enable - enable writes to the buffer
  1539. * @buffer: The ring buffer to enable writes
  1540. *
  1541. * Note, multiple disables will need the same number of enables
  1542. * to truely enable the writing (much like preempt_disable).
  1543. */
  1544. void ring_buffer_record_enable(struct ring_buffer *buffer)
  1545. {
  1546. atomic_dec(&buffer->record_disabled);
  1547. }
  1548. EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
  1549. /**
  1550. * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
  1551. * @buffer: The ring buffer to stop writes to.
  1552. * @cpu: The CPU buffer to stop
  1553. *
  1554. * This prevents all writes to the buffer. Any attempt to write
  1555. * to the buffer after this will fail and return NULL.
  1556. *
  1557. * The caller should call synchronize_sched() after this.
  1558. */
  1559. void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
  1560. {
  1561. struct ring_buffer_per_cpu *cpu_buffer;
  1562. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1563. return;
  1564. cpu_buffer = buffer->buffers[cpu];
  1565. atomic_inc(&cpu_buffer->record_disabled);
  1566. }
  1567. EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
  1568. /**
  1569. * ring_buffer_record_enable_cpu - enable writes to the buffer
  1570. * @buffer: The ring buffer to enable writes
  1571. * @cpu: The CPU to enable.
  1572. *
  1573. * Note, multiple disables will need the same number of enables
  1574. * to truely enable the writing (much like preempt_disable).
  1575. */
  1576. void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
  1577. {
  1578. struct ring_buffer_per_cpu *cpu_buffer;
  1579. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1580. return;
  1581. cpu_buffer = buffer->buffers[cpu];
  1582. atomic_dec(&cpu_buffer->record_disabled);
  1583. }
  1584. EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
  1585. /**
  1586. * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
  1587. * @buffer: The ring buffer
  1588. * @cpu: The per CPU buffer to get the entries from.
  1589. */
  1590. unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
  1591. {
  1592. struct ring_buffer_per_cpu *cpu_buffer;
  1593. unsigned long ret;
  1594. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1595. return 0;
  1596. cpu_buffer = buffer->buffers[cpu];
  1597. ret = cpu_buffer->entries;
  1598. return ret;
  1599. }
  1600. EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
  1601. /**
  1602. * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
  1603. * @buffer: The ring buffer
  1604. * @cpu: The per CPU buffer to get the number of overruns from
  1605. */
  1606. unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
  1607. {
  1608. struct ring_buffer_per_cpu *cpu_buffer;
  1609. unsigned long ret;
  1610. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1611. return 0;
  1612. cpu_buffer = buffer->buffers[cpu];
  1613. ret = cpu_buffer->overrun;
  1614. return ret;
  1615. }
  1616. EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
  1617. /**
  1618. * ring_buffer_entries - get the number of entries in a buffer
  1619. * @buffer: The ring buffer
  1620. *
  1621. * Returns the total number of entries in the ring buffer
  1622. * (all CPU entries)
  1623. */
  1624. unsigned long ring_buffer_entries(struct ring_buffer *buffer)
  1625. {
  1626. struct ring_buffer_per_cpu *cpu_buffer;
  1627. unsigned long entries = 0;
  1628. int cpu;
  1629. /* if you care about this being correct, lock the buffer */
  1630. for_each_buffer_cpu(buffer, cpu) {
  1631. cpu_buffer = buffer->buffers[cpu];
  1632. entries += cpu_buffer->entries;
  1633. }
  1634. return entries;
  1635. }
  1636. EXPORT_SYMBOL_GPL(ring_buffer_entries);
  1637. /**
  1638. * ring_buffer_overrun_cpu - get the number of overruns in buffer
  1639. * @buffer: The ring buffer
  1640. *
  1641. * Returns the total number of overruns in the ring buffer
  1642. * (all CPU entries)
  1643. */
  1644. unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
  1645. {
  1646. struct ring_buffer_per_cpu *cpu_buffer;
  1647. unsigned long overruns = 0;
  1648. int cpu;
  1649. /* if you care about this being correct, lock the buffer */
  1650. for_each_buffer_cpu(buffer, cpu) {
  1651. cpu_buffer = buffer->buffers[cpu];
  1652. overruns += cpu_buffer->overrun;
  1653. }
  1654. return overruns;
  1655. }
  1656. EXPORT_SYMBOL_GPL(ring_buffer_overruns);
  1657. static void rb_iter_reset(struct ring_buffer_iter *iter)
  1658. {
  1659. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  1660. /* Iterator usage is expected to have record disabled */
  1661. if (list_empty(&cpu_buffer->reader_page->list)) {
  1662. iter->head_page = cpu_buffer->head_page;
  1663. iter->head = cpu_buffer->head_page->read;
  1664. } else {
  1665. iter->head_page = cpu_buffer->reader_page;
  1666. iter->head = cpu_buffer->reader_page->read;
  1667. }
  1668. if (iter->head)
  1669. iter->read_stamp = cpu_buffer->read_stamp;
  1670. else
  1671. iter->read_stamp = iter->head_page->page->time_stamp;
  1672. }
  1673. /**
  1674. * ring_buffer_iter_reset - reset an iterator
  1675. * @iter: The iterator to reset
  1676. *
  1677. * Resets the iterator, so that it will start from the beginning
  1678. * again.
  1679. */
  1680. void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
  1681. {
  1682. struct ring_buffer_per_cpu *cpu_buffer;
  1683. unsigned long flags;
  1684. if (!iter)
  1685. return;
  1686. cpu_buffer = iter->cpu_buffer;
  1687. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1688. rb_iter_reset(iter);
  1689. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1690. }
  1691. EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
  1692. /**
  1693. * ring_buffer_iter_empty - check if an iterator has no more to read
  1694. * @iter: The iterator to check
  1695. */
  1696. int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
  1697. {
  1698. struct ring_buffer_per_cpu *cpu_buffer;
  1699. cpu_buffer = iter->cpu_buffer;
  1700. return iter->head_page == cpu_buffer->commit_page &&
  1701. iter->head == rb_commit_index(cpu_buffer);
  1702. }
  1703. EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
  1704. static void
  1705. rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
  1706. struct ring_buffer_event *event)
  1707. {
  1708. u64 delta;
  1709. switch (event->type) {
  1710. case RINGBUF_TYPE_PADDING:
  1711. return;
  1712. case RINGBUF_TYPE_TIME_EXTEND:
  1713. delta = event->array[0];
  1714. delta <<= TS_SHIFT;
  1715. delta += event->time_delta;
  1716. cpu_buffer->read_stamp += delta;
  1717. return;
  1718. case RINGBUF_TYPE_TIME_STAMP:
  1719. /* FIXME: not implemented */
  1720. return;
  1721. case RINGBUF_TYPE_DATA:
  1722. cpu_buffer->read_stamp += event->time_delta;
  1723. return;
  1724. default:
  1725. BUG();
  1726. }
  1727. return;
  1728. }
  1729. static void
  1730. rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
  1731. struct ring_buffer_event *event)
  1732. {
  1733. u64 delta;
  1734. switch (event->type) {
  1735. case RINGBUF_TYPE_PADDING:
  1736. return;
  1737. case RINGBUF_TYPE_TIME_EXTEND:
  1738. delta = event->array[0];
  1739. delta <<= TS_SHIFT;
  1740. delta += event->time_delta;
  1741. iter->read_stamp += delta;
  1742. return;
  1743. case RINGBUF_TYPE_TIME_STAMP:
  1744. /* FIXME: not implemented */
  1745. return;
  1746. case RINGBUF_TYPE_DATA:
  1747. iter->read_stamp += event->time_delta;
  1748. return;
  1749. default:
  1750. BUG();
  1751. }
  1752. return;
  1753. }
  1754. static struct buffer_page *
  1755. rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
  1756. {
  1757. struct buffer_page *reader = NULL;
  1758. unsigned long flags;
  1759. int nr_loops = 0;
  1760. local_irq_save(flags);
  1761. __raw_spin_lock(&cpu_buffer->lock);
  1762. again:
  1763. /*
  1764. * This should normally only loop twice. But because the
  1765. * start of the reader inserts an empty page, it causes
  1766. * a case where we will loop three times. There should be no
  1767. * reason to loop four times (that I know of).
  1768. */
  1769. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
  1770. reader = NULL;
  1771. goto out;
  1772. }
  1773. reader = cpu_buffer->reader_page;
  1774. /* If there's more to read, return this page */
  1775. if (cpu_buffer->reader_page->read < rb_page_size(reader))
  1776. goto out;
  1777. /* Never should we have an index greater than the size */
  1778. if (RB_WARN_ON(cpu_buffer,
  1779. cpu_buffer->reader_page->read > rb_page_size(reader)))
  1780. goto out;
  1781. /* check if we caught up to the tail */
  1782. reader = NULL;
  1783. if (cpu_buffer->commit_page == cpu_buffer->reader_page)
  1784. goto out;
  1785. /*
  1786. * Splice the empty reader page into the list around the head.
  1787. * Reset the reader page to size zero.
  1788. */
  1789. reader = cpu_buffer->head_page;
  1790. cpu_buffer->reader_page->list.next = reader->list.next;
  1791. cpu_buffer->reader_page->list.prev = reader->list.prev;
  1792. local_set(&cpu_buffer->reader_page->write, 0);
  1793. local_set(&cpu_buffer->reader_page->page->commit, 0);
  1794. /* Make the reader page now replace the head */
  1795. reader->list.prev->next = &cpu_buffer->reader_page->list;
  1796. reader->list.next->prev = &cpu_buffer->reader_page->list;
  1797. /*
  1798. * If the tail is on the reader, then we must set the head
  1799. * to the inserted page, otherwise we set it one before.
  1800. */
  1801. cpu_buffer->head_page = cpu_buffer->reader_page;
  1802. if (cpu_buffer->commit_page != reader)
  1803. rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
  1804. /* Finally update the reader page to the new head */
  1805. cpu_buffer->reader_page = reader;
  1806. rb_reset_reader_page(cpu_buffer);
  1807. goto again;
  1808. out:
  1809. __raw_spin_unlock(&cpu_buffer->lock);
  1810. local_irq_restore(flags);
  1811. return reader;
  1812. }
  1813. static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
  1814. {
  1815. struct ring_buffer_event *event;
  1816. struct buffer_page *reader;
  1817. unsigned length;
  1818. reader = rb_get_reader_page(cpu_buffer);
  1819. /* This function should not be called when buffer is empty */
  1820. if (RB_WARN_ON(cpu_buffer, !reader))
  1821. return;
  1822. event = rb_reader_event(cpu_buffer);
  1823. if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
  1824. cpu_buffer->entries--;
  1825. rb_update_read_stamp(cpu_buffer, event);
  1826. length = rb_event_length(event);
  1827. cpu_buffer->reader_page->read += length;
  1828. }
  1829. static void rb_advance_iter(struct ring_buffer_iter *iter)
  1830. {
  1831. struct ring_buffer *buffer;
  1832. struct ring_buffer_per_cpu *cpu_buffer;
  1833. struct ring_buffer_event *event;
  1834. unsigned length;
  1835. cpu_buffer = iter->cpu_buffer;
  1836. buffer = cpu_buffer->buffer;
  1837. /*
  1838. * Check if we are at the end of the buffer.
  1839. */
  1840. if (iter->head >= rb_page_size(iter->head_page)) {
  1841. if (RB_WARN_ON(buffer,
  1842. iter->head_page == cpu_buffer->commit_page))
  1843. return;
  1844. rb_inc_iter(iter);
  1845. return;
  1846. }
  1847. event = rb_iter_head_event(iter);
  1848. length = rb_event_length(event);
  1849. /*
  1850. * This should not be called to advance the header if we are
  1851. * at the tail of the buffer.
  1852. */
  1853. if (RB_WARN_ON(cpu_buffer,
  1854. (iter->head_page == cpu_buffer->commit_page) &&
  1855. (iter->head + length > rb_commit_index(cpu_buffer))))
  1856. return;
  1857. rb_update_iter_read_stamp(iter, event);
  1858. iter->head += length;
  1859. /* check for end of page padding */
  1860. if ((iter->head >= rb_page_size(iter->head_page)) &&
  1861. (iter->head_page != cpu_buffer->commit_page))
  1862. rb_advance_iter(iter);
  1863. }
  1864. static struct ring_buffer_event *
  1865. rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  1866. {
  1867. struct ring_buffer_per_cpu *cpu_buffer;
  1868. struct ring_buffer_event *event;
  1869. struct buffer_page *reader;
  1870. int nr_loops = 0;
  1871. cpu_buffer = buffer->buffers[cpu];
  1872. again:
  1873. /*
  1874. * We repeat when a timestamp is encountered. It is possible
  1875. * to get multiple timestamps from an interrupt entering just
  1876. * as one timestamp is about to be written. The max times
  1877. * that this can happen is the number of nested interrupts we
  1878. * can have. Nesting 10 deep of interrupts is clearly
  1879. * an anomaly.
  1880. */
  1881. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
  1882. return NULL;
  1883. reader = rb_get_reader_page(cpu_buffer);
  1884. if (!reader)
  1885. return NULL;
  1886. event = rb_reader_event(cpu_buffer);
  1887. switch (event->type) {
  1888. case RINGBUF_TYPE_PADDING:
  1889. if (rb_null_event(event))
  1890. RB_WARN_ON(cpu_buffer, 1);
  1891. /*
  1892. * Because the writer could be discarding every
  1893. * event it creates (which would probably be bad)
  1894. * if we were to go back to "again" then we may never
  1895. * catch up, and will trigger the warn on, or lock
  1896. * the box. Return the padding, and we will release
  1897. * the current locks, and try again.
  1898. */
  1899. rb_advance_reader(cpu_buffer);
  1900. return event;
  1901. case RINGBUF_TYPE_TIME_EXTEND:
  1902. /* Internal data, OK to advance */
  1903. rb_advance_reader(cpu_buffer);
  1904. goto again;
  1905. case RINGBUF_TYPE_TIME_STAMP:
  1906. /* FIXME: not implemented */
  1907. rb_advance_reader(cpu_buffer);
  1908. goto again;
  1909. case RINGBUF_TYPE_DATA:
  1910. if (ts) {
  1911. *ts = cpu_buffer->read_stamp + event->time_delta;
  1912. ring_buffer_normalize_time_stamp(buffer,
  1913. cpu_buffer->cpu, ts);
  1914. }
  1915. return event;
  1916. default:
  1917. BUG();
  1918. }
  1919. return NULL;
  1920. }
  1921. EXPORT_SYMBOL_GPL(ring_buffer_peek);
  1922. static struct ring_buffer_event *
  1923. rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  1924. {
  1925. struct ring_buffer *buffer;
  1926. struct ring_buffer_per_cpu *cpu_buffer;
  1927. struct ring_buffer_event *event;
  1928. int nr_loops = 0;
  1929. if (ring_buffer_iter_empty(iter))
  1930. return NULL;
  1931. cpu_buffer = iter->cpu_buffer;
  1932. buffer = cpu_buffer->buffer;
  1933. again:
  1934. /*
  1935. * We repeat when a timestamp is encountered. It is possible
  1936. * to get multiple timestamps from an interrupt entering just
  1937. * as one timestamp is about to be written. The max times
  1938. * that this can happen is the number of nested interrupts we
  1939. * can have. Nesting 10 deep of interrupts is clearly
  1940. * an anomaly.
  1941. */
  1942. if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
  1943. return NULL;
  1944. if (rb_per_cpu_empty(cpu_buffer))
  1945. return NULL;
  1946. event = rb_iter_head_event(iter);
  1947. switch (event->type) {
  1948. case RINGBUF_TYPE_PADDING:
  1949. if (rb_null_event(event)) {
  1950. rb_inc_iter(iter);
  1951. goto again;
  1952. }
  1953. rb_advance_iter(iter);
  1954. return event;
  1955. case RINGBUF_TYPE_TIME_EXTEND:
  1956. /* Internal data, OK to advance */
  1957. rb_advance_iter(iter);
  1958. goto again;
  1959. case RINGBUF_TYPE_TIME_STAMP:
  1960. /* FIXME: not implemented */
  1961. rb_advance_iter(iter);
  1962. goto again;
  1963. case RINGBUF_TYPE_DATA:
  1964. if (ts) {
  1965. *ts = iter->read_stamp + event->time_delta;
  1966. ring_buffer_normalize_time_stamp(buffer,
  1967. cpu_buffer->cpu, ts);
  1968. }
  1969. return event;
  1970. default:
  1971. BUG();
  1972. }
  1973. return NULL;
  1974. }
  1975. EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
  1976. /**
  1977. * ring_buffer_peek - peek at the next event to be read
  1978. * @buffer: The ring buffer to read
  1979. * @cpu: The cpu to peak at
  1980. * @ts: The timestamp counter of this event.
  1981. *
  1982. * This will return the event that will be read next, but does
  1983. * not consume the data.
  1984. */
  1985. struct ring_buffer_event *
  1986. ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
  1987. {
  1988. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  1989. struct ring_buffer_event *event;
  1990. unsigned long flags;
  1991. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  1992. return NULL;
  1993. again:
  1994. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  1995. event = rb_buffer_peek(buffer, cpu, ts);
  1996. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  1997. if (event && event->type == RINGBUF_TYPE_PADDING) {
  1998. cpu_relax();
  1999. goto again;
  2000. }
  2001. return event;
  2002. }
  2003. /**
  2004. * ring_buffer_iter_peek - peek at the next event to be read
  2005. * @iter: The ring buffer iterator
  2006. * @ts: The timestamp counter of this event.
  2007. *
  2008. * This will return the event that will be read next, but does
  2009. * not increment the iterator.
  2010. */
  2011. struct ring_buffer_event *
  2012. ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
  2013. {
  2014. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2015. struct ring_buffer_event *event;
  2016. unsigned long flags;
  2017. again:
  2018. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2019. event = rb_iter_peek(iter, ts);
  2020. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2021. if (event && event->type == RINGBUF_TYPE_PADDING) {
  2022. cpu_relax();
  2023. goto again;
  2024. }
  2025. return event;
  2026. }
  2027. /**
  2028. * ring_buffer_consume - return an event and consume it
  2029. * @buffer: The ring buffer to get the next event from
  2030. *
  2031. * Returns the next event in the ring buffer, and that event is consumed.
  2032. * Meaning, that sequential reads will keep returning a different event,
  2033. * and eventually empty the ring buffer if the producer is slower.
  2034. */
  2035. struct ring_buffer_event *
  2036. ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
  2037. {
  2038. struct ring_buffer_per_cpu *cpu_buffer;
  2039. struct ring_buffer_event *event = NULL;
  2040. unsigned long flags;
  2041. again:
  2042. /* might be called in atomic */
  2043. preempt_disable();
  2044. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2045. goto out;
  2046. cpu_buffer = buffer->buffers[cpu];
  2047. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2048. event = rb_buffer_peek(buffer, cpu, ts);
  2049. if (!event)
  2050. goto out_unlock;
  2051. rb_advance_reader(cpu_buffer);
  2052. out_unlock:
  2053. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2054. out:
  2055. preempt_enable();
  2056. if (event && event->type == RINGBUF_TYPE_PADDING) {
  2057. cpu_relax();
  2058. goto again;
  2059. }
  2060. return event;
  2061. }
  2062. EXPORT_SYMBOL_GPL(ring_buffer_consume);
  2063. /**
  2064. * ring_buffer_read_start - start a non consuming read of the buffer
  2065. * @buffer: The ring buffer to read from
  2066. * @cpu: The cpu buffer to iterate over
  2067. *
  2068. * This starts up an iteration through the buffer. It also disables
  2069. * the recording to the buffer until the reading is finished.
  2070. * This prevents the reading from being corrupted. This is not
  2071. * a consuming read, so a producer is not expected.
  2072. *
  2073. * Must be paired with ring_buffer_finish.
  2074. */
  2075. struct ring_buffer_iter *
  2076. ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
  2077. {
  2078. struct ring_buffer_per_cpu *cpu_buffer;
  2079. struct ring_buffer_iter *iter;
  2080. unsigned long flags;
  2081. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2082. return NULL;
  2083. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2084. if (!iter)
  2085. return NULL;
  2086. cpu_buffer = buffer->buffers[cpu];
  2087. iter->cpu_buffer = cpu_buffer;
  2088. atomic_inc(&cpu_buffer->record_disabled);
  2089. synchronize_sched();
  2090. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2091. __raw_spin_lock(&cpu_buffer->lock);
  2092. rb_iter_reset(iter);
  2093. __raw_spin_unlock(&cpu_buffer->lock);
  2094. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2095. return iter;
  2096. }
  2097. EXPORT_SYMBOL_GPL(ring_buffer_read_start);
  2098. /**
  2099. * ring_buffer_finish - finish reading the iterator of the buffer
  2100. * @iter: The iterator retrieved by ring_buffer_start
  2101. *
  2102. * This re-enables the recording to the buffer, and frees the
  2103. * iterator.
  2104. */
  2105. void
  2106. ring_buffer_read_finish(struct ring_buffer_iter *iter)
  2107. {
  2108. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2109. atomic_dec(&cpu_buffer->record_disabled);
  2110. kfree(iter);
  2111. }
  2112. EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
  2113. /**
  2114. * ring_buffer_read - read the next item in the ring buffer by the iterator
  2115. * @iter: The ring buffer iterator
  2116. * @ts: The time stamp of the event read.
  2117. *
  2118. * This reads the next event in the ring buffer and increments the iterator.
  2119. */
  2120. struct ring_buffer_event *
  2121. ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
  2122. {
  2123. struct ring_buffer_event *event;
  2124. struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
  2125. unsigned long flags;
  2126. again:
  2127. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2128. event = rb_iter_peek(iter, ts);
  2129. if (!event)
  2130. goto out;
  2131. rb_advance_iter(iter);
  2132. out:
  2133. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2134. if (event && event->type == RINGBUF_TYPE_PADDING) {
  2135. cpu_relax();
  2136. goto again;
  2137. }
  2138. return event;
  2139. }
  2140. EXPORT_SYMBOL_GPL(ring_buffer_read);
  2141. /**
  2142. * ring_buffer_size - return the size of the ring buffer (in bytes)
  2143. * @buffer: The ring buffer.
  2144. */
  2145. unsigned long ring_buffer_size(struct ring_buffer *buffer)
  2146. {
  2147. return BUF_PAGE_SIZE * buffer->pages;
  2148. }
  2149. EXPORT_SYMBOL_GPL(ring_buffer_size);
  2150. static void
  2151. rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
  2152. {
  2153. cpu_buffer->head_page
  2154. = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
  2155. local_set(&cpu_buffer->head_page->write, 0);
  2156. local_set(&cpu_buffer->head_page->page->commit, 0);
  2157. cpu_buffer->head_page->read = 0;
  2158. cpu_buffer->tail_page = cpu_buffer->head_page;
  2159. cpu_buffer->commit_page = cpu_buffer->head_page;
  2160. INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
  2161. local_set(&cpu_buffer->reader_page->write, 0);
  2162. local_set(&cpu_buffer->reader_page->page->commit, 0);
  2163. cpu_buffer->reader_page->read = 0;
  2164. cpu_buffer->overrun = 0;
  2165. cpu_buffer->entries = 0;
  2166. cpu_buffer->write_stamp = 0;
  2167. cpu_buffer->read_stamp = 0;
  2168. }
  2169. /**
  2170. * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
  2171. * @buffer: The ring buffer to reset a per cpu buffer of
  2172. * @cpu: The CPU buffer to be reset
  2173. */
  2174. void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  2175. {
  2176. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2177. unsigned long flags;
  2178. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2179. return;
  2180. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2181. __raw_spin_lock(&cpu_buffer->lock);
  2182. rb_reset_cpu(cpu_buffer);
  2183. __raw_spin_unlock(&cpu_buffer->lock);
  2184. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2185. }
  2186. EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
  2187. /**
  2188. * ring_buffer_reset - reset a ring buffer
  2189. * @buffer: The ring buffer to reset all cpu buffers
  2190. */
  2191. void ring_buffer_reset(struct ring_buffer *buffer)
  2192. {
  2193. int cpu;
  2194. for_each_buffer_cpu(buffer, cpu)
  2195. ring_buffer_reset_cpu(buffer, cpu);
  2196. }
  2197. EXPORT_SYMBOL_GPL(ring_buffer_reset);
  2198. /**
  2199. * rind_buffer_empty - is the ring buffer empty?
  2200. * @buffer: The ring buffer to test
  2201. */
  2202. int ring_buffer_empty(struct ring_buffer *buffer)
  2203. {
  2204. struct ring_buffer_per_cpu *cpu_buffer;
  2205. int cpu;
  2206. /* yes this is racy, but if you don't like the race, lock the buffer */
  2207. for_each_buffer_cpu(buffer, cpu) {
  2208. cpu_buffer = buffer->buffers[cpu];
  2209. if (!rb_per_cpu_empty(cpu_buffer))
  2210. return 0;
  2211. }
  2212. return 1;
  2213. }
  2214. EXPORT_SYMBOL_GPL(ring_buffer_empty);
  2215. /**
  2216. * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
  2217. * @buffer: The ring buffer
  2218. * @cpu: The CPU buffer to test
  2219. */
  2220. int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
  2221. {
  2222. struct ring_buffer_per_cpu *cpu_buffer;
  2223. int ret;
  2224. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2225. return 1;
  2226. cpu_buffer = buffer->buffers[cpu];
  2227. ret = rb_per_cpu_empty(cpu_buffer);
  2228. return ret;
  2229. }
  2230. EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
  2231. /**
  2232. * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
  2233. * @buffer_a: One buffer to swap with
  2234. * @buffer_b: The other buffer to swap with
  2235. *
  2236. * This function is useful for tracers that want to take a "snapshot"
  2237. * of a CPU buffer and has another back up buffer lying around.
  2238. * it is expected that the tracer handles the cpu buffer not being
  2239. * used at the moment.
  2240. */
  2241. int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
  2242. struct ring_buffer *buffer_b, int cpu)
  2243. {
  2244. struct ring_buffer_per_cpu *cpu_buffer_a;
  2245. struct ring_buffer_per_cpu *cpu_buffer_b;
  2246. int ret = -EINVAL;
  2247. if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
  2248. !cpumask_test_cpu(cpu, buffer_b->cpumask))
  2249. goto out;
  2250. /* At least make sure the two buffers are somewhat the same */
  2251. if (buffer_a->pages != buffer_b->pages)
  2252. goto out;
  2253. ret = -EAGAIN;
  2254. if (ring_buffer_flags != RB_BUFFERS_ON)
  2255. goto out;
  2256. if (atomic_read(&buffer_a->record_disabled))
  2257. goto out;
  2258. if (atomic_read(&buffer_b->record_disabled))
  2259. goto out;
  2260. cpu_buffer_a = buffer_a->buffers[cpu];
  2261. cpu_buffer_b = buffer_b->buffers[cpu];
  2262. if (atomic_read(&cpu_buffer_a->record_disabled))
  2263. goto out;
  2264. if (atomic_read(&cpu_buffer_b->record_disabled))
  2265. goto out;
  2266. /*
  2267. * We can't do a synchronize_sched here because this
  2268. * function can be called in atomic context.
  2269. * Normally this will be called from the same CPU as cpu.
  2270. * If not it's up to the caller to protect this.
  2271. */
  2272. atomic_inc(&cpu_buffer_a->record_disabled);
  2273. atomic_inc(&cpu_buffer_b->record_disabled);
  2274. buffer_a->buffers[cpu] = cpu_buffer_b;
  2275. buffer_b->buffers[cpu] = cpu_buffer_a;
  2276. cpu_buffer_b->buffer = buffer_a;
  2277. cpu_buffer_a->buffer = buffer_b;
  2278. atomic_dec(&cpu_buffer_a->record_disabled);
  2279. atomic_dec(&cpu_buffer_b->record_disabled);
  2280. ret = 0;
  2281. out:
  2282. return ret;
  2283. }
  2284. EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  2285. static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
  2286. struct buffer_data_page *bpage,
  2287. unsigned int offset)
  2288. {
  2289. struct ring_buffer_event *event;
  2290. unsigned long head;
  2291. __raw_spin_lock(&cpu_buffer->lock);
  2292. for (head = offset; head < local_read(&bpage->commit);
  2293. head += rb_event_length(event)) {
  2294. event = __rb_data_page_index(bpage, head);
  2295. if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
  2296. return;
  2297. /* Only count data entries */
  2298. if (event->type != RINGBUF_TYPE_DATA)
  2299. continue;
  2300. cpu_buffer->entries--;
  2301. }
  2302. __raw_spin_unlock(&cpu_buffer->lock);
  2303. }
  2304. /**
  2305. * ring_buffer_alloc_read_page - allocate a page to read from buffer
  2306. * @buffer: the buffer to allocate for.
  2307. *
  2308. * This function is used in conjunction with ring_buffer_read_page.
  2309. * When reading a full page from the ring buffer, these functions
  2310. * can be used to speed up the process. The calling function should
  2311. * allocate a few pages first with this function. Then when it
  2312. * needs to get pages from the ring buffer, it passes the result
  2313. * of this function into ring_buffer_read_page, which will swap
  2314. * the page that was allocated, with the read page of the buffer.
  2315. *
  2316. * Returns:
  2317. * The page allocated, or NULL on error.
  2318. */
  2319. void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
  2320. {
  2321. struct buffer_data_page *bpage;
  2322. unsigned long addr;
  2323. addr = __get_free_page(GFP_KERNEL);
  2324. if (!addr)
  2325. return NULL;
  2326. bpage = (void *)addr;
  2327. rb_init_page(bpage);
  2328. return bpage;
  2329. }
  2330. /**
  2331. * ring_buffer_free_read_page - free an allocated read page
  2332. * @buffer: the buffer the page was allocate for
  2333. * @data: the page to free
  2334. *
  2335. * Free a page allocated from ring_buffer_alloc_read_page.
  2336. */
  2337. void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
  2338. {
  2339. free_page((unsigned long)data);
  2340. }
  2341. /**
  2342. * ring_buffer_read_page - extract a page from the ring buffer
  2343. * @buffer: buffer to extract from
  2344. * @data_page: the page to use allocated from ring_buffer_alloc_read_page
  2345. * @len: amount to extract
  2346. * @cpu: the cpu of the buffer to extract
  2347. * @full: should the extraction only happen when the page is full.
  2348. *
  2349. * This function will pull out a page from the ring buffer and consume it.
  2350. * @data_page must be the address of the variable that was returned
  2351. * from ring_buffer_alloc_read_page. This is because the page might be used
  2352. * to swap with a page in the ring buffer.
  2353. *
  2354. * for example:
  2355. * rpage = ring_buffer_alloc_read_page(buffer);
  2356. * if (!rpage)
  2357. * return error;
  2358. * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
  2359. * if (ret >= 0)
  2360. * process_page(rpage, ret);
  2361. *
  2362. * When @full is set, the function will not return true unless
  2363. * the writer is off the reader page.
  2364. *
  2365. * Note: it is up to the calling functions to handle sleeps and wakeups.
  2366. * The ring buffer can be used anywhere in the kernel and can not
  2367. * blindly call wake_up. The layer that uses the ring buffer must be
  2368. * responsible for that.
  2369. *
  2370. * Returns:
  2371. * >=0 if data has been transferred, returns the offset of consumed data.
  2372. * <0 if no data has been transferred.
  2373. */
  2374. int ring_buffer_read_page(struct ring_buffer *buffer,
  2375. void **data_page, size_t len, int cpu, int full)
  2376. {
  2377. struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
  2378. struct ring_buffer_event *event;
  2379. struct buffer_data_page *bpage;
  2380. struct buffer_page *reader;
  2381. unsigned long flags;
  2382. unsigned int commit;
  2383. unsigned int read;
  2384. u64 save_timestamp;
  2385. int ret = -1;
  2386. if (!cpumask_test_cpu(cpu, buffer->cpumask))
  2387. goto out;
  2388. /*
  2389. * If len is not big enough to hold the page header, then
  2390. * we can not copy anything.
  2391. */
  2392. if (len <= BUF_PAGE_HDR_SIZE)
  2393. goto out;
  2394. len -= BUF_PAGE_HDR_SIZE;
  2395. if (!data_page)
  2396. goto out;
  2397. bpage = *data_page;
  2398. if (!bpage)
  2399. goto out;
  2400. spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
  2401. reader = rb_get_reader_page(cpu_buffer);
  2402. if (!reader)
  2403. goto out_unlock;
  2404. event = rb_reader_event(cpu_buffer);
  2405. read = reader->read;
  2406. commit = rb_page_commit(reader);
  2407. /*
  2408. * If this page has been partially read or
  2409. * if len is not big enough to read the rest of the page or
  2410. * a writer is still on the page, then
  2411. * we must copy the data from the page to the buffer.
  2412. * Otherwise, we can simply swap the page with the one passed in.
  2413. */
  2414. if (read || (len < (commit - read)) ||
  2415. cpu_buffer->reader_page == cpu_buffer->commit_page) {
  2416. struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
  2417. unsigned int rpos = read;
  2418. unsigned int pos = 0;
  2419. unsigned int size;
  2420. if (full)
  2421. goto out_unlock;
  2422. if (len > (commit - read))
  2423. len = (commit - read);
  2424. size = rb_event_length(event);
  2425. if (len < size)
  2426. goto out_unlock;
  2427. /* save the current timestamp, since the user will need it */
  2428. save_timestamp = cpu_buffer->read_stamp;
  2429. /* Need to copy one event at a time */
  2430. do {
  2431. memcpy(bpage->data + pos, rpage->data + rpos, size);
  2432. len -= size;
  2433. rb_advance_reader(cpu_buffer);
  2434. rpos = reader->read;
  2435. pos += size;
  2436. event = rb_reader_event(cpu_buffer);
  2437. size = rb_event_length(event);
  2438. } while (len > size);
  2439. /* update bpage */
  2440. local_set(&bpage->commit, pos);
  2441. bpage->time_stamp = save_timestamp;
  2442. /* we copied everything to the beginning */
  2443. read = 0;
  2444. } else {
  2445. /* swap the pages */
  2446. rb_init_page(bpage);
  2447. bpage = reader->page;
  2448. reader->page = *data_page;
  2449. local_set(&reader->write, 0);
  2450. reader->read = 0;
  2451. *data_page = bpage;
  2452. /* update the entry counter */
  2453. rb_remove_entries(cpu_buffer, bpage, read);
  2454. }
  2455. ret = read;
  2456. out_unlock:
  2457. spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
  2458. out:
  2459. return ret;
  2460. }
  2461. static ssize_t
  2462. rb_simple_read(struct file *filp, char __user *ubuf,
  2463. size_t cnt, loff_t *ppos)
  2464. {
  2465. unsigned long *p = filp->private_data;
  2466. char buf[64];
  2467. int r;
  2468. if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
  2469. r = sprintf(buf, "permanently disabled\n");
  2470. else
  2471. r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
  2472. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2473. }
  2474. static ssize_t
  2475. rb_simple_write(struct file *filp, const char __user *ubuf,
  2476. size_t cnt, loff_t *ppos)
  2477. {
  2478. unsigned long *p = filp->private_data;
  2479. char buf[64];
  2480. unsigned long val;
  2481. int ret;
  2482. if (cnt >= sizeof(buf))
  2483. return -EINVAL;
  2484. if (copy_from_user(&buf, ubuf, cnt))
  2485. return -EFAULT;
  2486. buf[cnt] = 0;
  2487. ret = strict_strtoul(buf, 10, &val);
  2488. if (ret < 0)
  2489. return ret;
  2490. if (val)
  2491. set_bit(RB_BUFFERS_ON_BIT, p);
  2492. else
  2493. clear_bit(RB_BUFFERS_ON_BIT, p);
  2494. (*ppos)++;
  2495. return cnt;
  2496. }
  2497. static const struct file_operations rb_simple_fops = {
  2498. .open = tracing_open_generic,
  2499. .read = rb_simple_read,
  2500. .write = rb_simple_write,
  2501. };
  2502. static __init int rb_init_debugfs(void)
  2503. {
  2504. struct dentry *d_tracer;
  2505. d_tracer = tracing_init_dentry();
  2506. trace_create_file("tracing_on", 0644, d_tracer,
  2507. &ring_buffer_flags, &rb_simple_fops);
  2508. return 0;
  2509. }
  2510. fs_initcall(rb_init_debugfs);
  2511. #ifdef CONFIG_HOTPLUG_CPU
  2512. static int rb_cpu_notify(struct notifier_block *self,
  2513. unsigned long action, void *hcpu)
  2514. {
  2515. struct ring_buffer *buffer =
  2516. container_of(self, struct ring_buffer, cpu_notify);
  2517. long cpu = (long)hcpu;
  2518. switch (action) {
  2519. case CPU_UP_PREPARE:
  2520. case CPU_UP_PREPARE_FROZEN:
  2521. if (cpu_isset(cpu, *buffer->cpumask))
  2522. return NOTIFY_OK;
  2523. buffer->buffers[cpu] =
  2524. rb_allocate_cpu_buffer(buffer, cpu);
  2525. if (!buffer->buffers[cpu]) {
  2526. WARN(1, "failed to allocate ring buffer on CPU %ld\n",
  2527. cpu);
  2528. return NOTIFY_OK;
  2529. }
  2530. smp_wmb();
  2531. cpu_set(cpu, *buffer->cpumask);
  2532. break;
  2533. case CPU_DOWN_PREPARE:
  2534. case CPU_DOWN_PREPARE_FROZEN:
  2535. /*
  2536. * Do nothing.
  2537. * If we were to free the buffer, then the user would
  2538. * lose any trace that was in the buffer.
  2539. */
  2540. break;
  2541. default:
  2542. break;
  2543. }
  2544. return NOTIFY_OK;
  2545. }
  2546. #endif